java

位置:IT落伍者 >> java >> 浏览文章

Javamail写带附件eml文件并调用Outlook


发布日期:2018年07月05日
 
Javamail写带附件eml文件并调用Outlook

将邮件写入到文件的代码

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);

}

               

上一篇:java中的双缓沖技术

下一篇:java的各种排序算法