javaoutlook
㈠ java開發outlook
用java來連接郵件伺服器,這個和outlook無關,官方工具是JavaMail,可以在oracle官網上下載相關jar包和說明文檔,第三方常見的工具是jmail,jmail是歷史悠久的伺服器端收發郵件的工具。網上樣例和資料都很多。至於過濾內容JavaMail應該有提供按發件人或內容下載郵件的api。
㈡ 怎樣在Java ee中調用 outlook
調用 outlook方法:
將郵件寫入到文件的代碼:msg.saveChanges();File f = new File("d:/test.eml");msg.writeTo(new FileOutputStream(f));
調用outlook的代碼:Process p = Runtime.getRuntime().exec("cmd /C start msimn.exe /eml:d:/test.eml")。
㈢ 如何在Java ee項目中如何調用outlook發郵件
java mail調用outlook的方法例子
1 將郵件寫入到文件的代碼
msg.saveChanges();
File f = new File("d:/test.eml");
msg.writeTo(new FileOutputStream(f));
2 調用outlook的代碼
Process p = Runtime.getRuntime().exec("cmd /C start msimn.exe /eml:d:/test.eml");
3 完整的代碼如下
package code.jdk.mail;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.AuthenticationFailedException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class EmailWriteToFile {
// 定義發件人、收件人、SMTP伺服器、用戶名、密碼、主題、內容等
private String displayName;
private String to;
private String from;
private String smtpServer;
private String username;
private String password;
private String subject;
private String content;
private boolean ifAuth; // 伺服器是否要身份認證
private String filename = "";
private Vector file = new Vector(); // 用於保存發送附件的文件名的集合
private String contentType = "text/html";
private String charset = "utf-8";
public void addFile(String filename) {
file.add(filename);
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
/**
* 設置SMTP伺服器地址
*/
public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
}
/**
* 設置發件人的地址
*/
public void setFrom(String from) {
this.from = from;
}
/**
* 設置顯示的名稱
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
/**
* 設置伺服器是否需要身份認證
*/
public void setIfAuth(boolean ifAuth) {
this.ifAuth = ifAuth;
}
/**
* 設置E-mail用戶名
*/
public void setUserName(String username) {
this.username = username;
}
/**
* 設置E-mail密碼
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 設置接收者
*/
public void setTo(String to) {
this.to = to;
}
/**
* 設置主題
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* 設置主體內容
*/
public void setContent(String content) {
this.content = content;
}
public EmailWriteToFile() {
}
private int port = 25;
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
/**
* 發送郵件
*
* @throws IOException
* @throws FileNotFoundException
*/
public boolean send() throws FileNotFoundException, IOException {
HashMap<String, String> map = new HashMap<String, String>();
map.put("state", "success");
String message = "郵件發送成功!";
Session session = null;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", port);
try {
props.put("mail.smtp.auth", "false");
session = Session.getDefaultInstance(props, null);
session.setDebug(false);
Transport trans = null;
Message msg = new MimeMessage(session);
try {
Address from_address = new InternetAddress(from, displayName);
msg.setFrom(from_address);
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content.toString(), getContentType() + ";charset=" + getCharset());
mp.addBodyPart(mbp);
if (!file.isEmpty()) {// 有附件
Enumeration efile = file.elements();
while (efile.hasMoreElements()) {
mbp = new MimeBodyPart();
filename = efile.nextElement().toString(); // 選擇出每一個附件名
FileDataSource fds = new FileDataSource(filename); // 得到數據源
mbp.setDataHandler(new DataHandler(fds)); // 得到附件本身並至入BodyPart
mbp.setFileName(MimeUtility.encodeText(fds.getName(), getCharset(),"B")); // 得到文件名同樣至入BodyPart
mp.addBodyPart(mbp);
}
file.removeAllElements();
}
msg.setContent(mp); // Multipart加入到信件
msg.setSentDate(new Date()); // 設置信件頭的發送日期
// 發送信件
msg.saveChanges();
File f = new File("d:/test.eml");
msg.writeTo(new FileOutputStream(f));
} catch (AuthenticationFailedException e) {
map.put("state", "failed");
message = "郵件發送失敗!錯誤原因: " + "身份驗證錯誤!";
e.printStackTrace();
return false;
} catch (MessagingException e) {
message = "郵件發送失敗!錯誤原因: " + e.getMessage();
map.put("state", "failed");
e.printStackTrace();
Exception ex = null;
if ((ex = e.getNextException()) != null) {
System.out.println(ex.toString());
ex.printStackTrace();
}
return false;
}
// System.out.println(" 提示信息:"+message);
map.put("message", message);
return true;
}
public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
EmailWriteToFile o = new EmailWriteToFile();
o.setSmtpServer("localhost");
o.setFrom("[email protected]");
o.setDisplayName("TOM");
o.setTo("[email protected]");
o.setSubject("Test Subject");
o.setContent("Test Content");
o.setCharset("GBK");
o.addFile("e:/讀我.txt");
o.send();
Process p = Runtime.getRuntime().exec("cmd /C start msimn.exe /eml:d:/test.eml");
}
}
㈣ Java怎麼直接發送郵件,而不通過頁面或者outlook軟體.
1 必須編寫郵件客戶端程序,請使用javamail包
2 編寫一個頁面,觸發一個事件,講發送的內容傳遞給後台的郵件客戶端程序,即可完成你的要求
3 你不會是要求我們替你寫個程序吧???
祝你好運!