熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Javamail寫帶附件eml文件並調用Outlook

2022-06-13   來源: Java核心技術 

   將郵件寫入到文件的代碼

  msgsaveChanges();
File f = new File(d:/testeml);
msgwriteTo(new FileOutputStream(f));

   調用outlook的代碼

  Process p = RuntimegetRuntime()exec(cmd /C start msimnexe /eml:d:/testeml);

   完整的代碼如下

  package codejdkmail;

  import javaioFile;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import javautilDate;
import javautilEnumeration;
import javautilHashMap;
import javautilProperties;
import javautilVector;

  import javaxactivationDataHandler;
import javaxactivationFileDataSource;
import javaxmailAddress;
import javaxmailAuthenticationFailedException;
import javaxmailMessage;
import javaxmailMessagingException;
import javaxmailMultipart;
import javaxmailSession;
import javaxmailTransport;
import javaxmailinternetInternetAddress;
import javaxmailinternetMimeBodyPart;
import javaxmailinternetMimeMessage;
import javaxmailinternetMimeMultipart;
import javaxmailinternetMimeUtility;

  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;

  public void addFile(String filename) {
fileadd(filename);
}

  public String getContentType() {
return contentType;
}

  public void setContentType(String contentType) {
ntentType = contentType;
}

  public String getCharset() {
return charset;
}

  public void setCharset(String charset) {
thischarset = charset;
}

  /**
* 設置SMTP服務器地址
*/
public void setSmtpServer(String smtpServer) {
thissmtpServer = smtpServer;
}

  /**
* 設置發件人的地址
*/
public void setFrom(String from) {
thisfrom = from;
}

  /**
* 設置顯示的名稱
*/
public void setDisplayName(String displayName) {
thisdisplayName = displayName;
}

  /**
* 設置服務器是否需要身份認證
*/
public void setIfAuth(boolean ifAuth) {
thisifAuth = ifAuth;
}

  /**
* 設置Email用戶名
*/
public void setUserName(String username) {
thisusername = username;
}

  /**
* 設置Email密碼
*/
public void setPassword(String password) {
thispassword = password;
}

  /**
* 設置接收者
*/
public void setTo(String to) {
thisto = to;
}

  /**
* 設置主題
*/
public void setSubject(String subject) {
thissubject = subject;
}

  /**
* 設置主體內容
*/
public void setContent(String content) {
ntent = content;
}

  public EmailWriteToFile() {

  }

  private int port = ;

  public int getPort() {
return port;
}

  public void setPort(int port) {
thisport = port;
}

  /**
* 發送郵件
*
* @throws IOException
* @throws FileNotFoundException
*/
public boolean send() throws FileNotFoundException IOException {
HashMap<String String> map = new HashMap<String String>();
mapput(state success);
String message = 郵件發送成功!;
Session session = null;
Properties props = SystemgetProperties();
propsput(mailsmtphost smtpServer);
propsput(mailsmtpport port);
try {

  propsput(mailsmtpauth false);
session = SessiongetDefaultInstance(props null);

  sessionsetDebug(false);
Transport trans = null;
Message msg = new MimeMessage(session);
try {
Address from_address = new InternetAddress(from displayName);
msgsetFrom(from_address);
} catch (javaioUnsupportedEncodingException e) {
eprintStackTrace();
}
InternetAddress[] address = { new InternetAddress(to) };
msgsetRecipients(MessageRecipientTypeTO address);
msgsetSubject(subject);
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbpsetContent(contenttoString() getContentType() + ; charset= + getCharset());
mpaddBodyPart(mbp);
if (!fileisEmpty()) {// 有附件
Enumeration efile = fileelements();
while (efilehasMoreElements()) {
mbp = new MimeBodyPart();
filename = efilenextElement()toString(); // 選擇出每一個附件名
FileDataSource fds = new FileDataSource(filename); // 得到數據源
mbpsetDataHandler(new DataHandler(fds)); // 得到附件本身並至入BodyPart
mbpsetFileName(MimeUtilityencodeText(fdsgetName() getCharset()B)); // 得到文件名同樣至入BodyPart
mpaddBodyPart(mbp);
}
fileremoveAllElements();
}
msgsetContent(mp); // Multipart加入到信件
msgsetSentDate(new Date()); // 設置信件頭的發送日期
// 發送信件
msgsaveChanges();
File f = new File(d:/testeml);
msgwriteTo(new FileOutputStream(f));

  } catch (AuthenticationFailedException e) {
mapput(state failed);
message = 郵件發送失敗!錯誤原因 + 身份驗證錯誤!;
eprintStackTrace();
return false;
} catch (MessagingException e) {
message = 郵件發送失敗!錯誤原因 + egetMessage();
mapput(state failed);
eprintStackTrace();
Exception ex = null;
if ((ex = egetNextException()) != null) {
Systemoutprintln(extoString());
exprintStackTrace();
}
return false;
}
// Systemoutprintln( 提示信息:+message);
mapput(message message);
return true;
}

  public static void main(String[] args) throws FileNotFoundException IOException InterruptedException {
EmailWriteToFile o = new EmailWriteToFile();
osetSmtpServer(localhost);
osetFrom();
osetDisplayName(TOM);
osetTo();
osetSubject(Test Subject);
osetContent(Test Content);
osetCharset(GBK);
oaddFile(e:/讀我txt);
osend();
Process p = RuntimegetRuntime()exec(cmd /C start msimnexe /eml:d:/testeml);

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25757.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.