使用了一个包:mail.jar
package com.delta.mes.eqm.util; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.BodyPart; 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 com.delta.mes.eqm.Constants; import ch.qos.logback.classic.Logger; public class MailUtil { private Logger log; private MimeMessage mimeMsg; private Session session; private Properties props; private String username = ""; private String password = ""; private MimeMultipart mp; private boolean isCC = false; public MailUtil() { } public MailUtil(Logger log) { this.log = log; setSmtpHost(Constants.SMTP); createMimeMessage(); } public void setSmtpHost(String hostName) { if (props == null) { props = System.getProperties(); } props.put("mail.smtp.host", hostName); } public boolean createMimeMessage() { try { session = Session.getDefaultInstance(props, null); } catch (Exception e) { log.error("get mail session error: " + e); return false; } try { mimeMsg = new MimeMessage(session); mp = new MimeMultipart(); } catch (Exception e) { log.error("create mail mime object error: " + e); return false; } return true; } public void setNeedAuth(boolean need) { if (props == null) { props = System.getProperties(); } if (need) { props.put("mail.smtp.auth", "true"); } else { props.put("mail.smtp.auth", "false"); } } public void setNamePass(String name, String pass) { username = name; password = pass; } public boolean setSubject(String mailSubject) { try { mimeMsg.setSubject(mailSubject); } catch (Exception e) { log.error("set mail theme error: " + e); return false; } return true; } public boolean setBody(String mailBody) { try { BodyPart bp = new MimeBodyPart(); bp.setText(mailBody); mp.addBodyPart(bp); } catch (Exception e) { log.error("set mail body error: " + e); return false; } return true; } public boolean addFileAffix(String filename) { try { BodyPart bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(fileds.getName()); mp.addBodyPart(bp); } catch (Exception e) { log.error("add mail " + filename + " attachment error: " + e); return false; } return true; } public boolean setFrom(String from) { try { mimeMsg.setFrom(new InternetAddress(from)); } catch (Exception e) { log.error("set mail from error: " + e); return false; } return true; } public boolean setTo(String to) { if (to == null) { return false; } try { mimeMsg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(to)); } catch (Exception e) { log.error("set mail to error: " + e); return false; } return true; } public boolean setCopyTo(String copyto) { if (copyto == null) { return false; } try { mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC, InternetAddress.parse(copyto)); } catch (Exception e) { log.error("set mail copy to error: " + e); return false; } return true; } public boolean sendOut() { try { mimeMsg.setContent(mp); mimeMsg.saveChanges(); Session mailSession = Session.getInstance(props, null); Transport transport = mailSession.getTransport("smtp"); transport.connect(props.getProperty("mail.smtp.host"), username, password); transport.sendMessage(mimeMsg, mimeMsg.getRecipients(javax.mail.Message.RecipientType.TO)); if (isCC) { transport.sendMessage(mimeMsg, mimeMsg.getRecipients(javax.mail.Message.RecipientType.CC)); } transport.close(); } catch (Exception e) { e.printStackTrace(); log.error("set mail error: " + e); return false; } return true; } public boolean sendMail(MailVO mailVO) { setSmtpHost(mailVO.getHost()); createMimeMessage(); if (mailVO.isAuth()) { this.setNeedAuth(true); this.setNamePass(mailVO.getUserName(), mailVO.getPassWord()); } this.setSubject(mailVO.getMailTitle()); String[] fileaffix = mailVO.getAttachment(); if (fileaffix != null) { for (String file : fileaffix) { this.addFileAffix(file); } } this.setBody(mailVO.getMailContent()); this.setFrom(mailVO.getMailFrom()); this.setTo(mailVO.getMailTo()); String copy = mailVO.getCopyTo(); if (copy != null) { this.setCopyTo(copy); isCC = true; } return sendOut(); } public static void main(String[] args) { MailVO mailVO = new MailVO(); mailVO.setHost("xx.xx.corp"); mailVO.setAuth(true); mailVO.setUserName("AA"); mailVO.setPassWord("BB"); mailVO.setMailTitle("title"); mailVO.setMailTo("[email protected],[email protected]"); mailVO.setCopyTo("[email protected],[email protected]"); mailVO.setMailFrom("[email protected]"); mailVO.setAttachment(new String[] { "e:/111.txt", "e:/222.txt" }); mailVO.setMailContent("test"); MailUtil themail = new MailUtil(); boolean bol = themail.sendMail(mailVO); System.out.println(bol); } }
package com.delta.mes.eqm.util; import com.delta.mes.eqm.Constants; public class MailVO { // mail host private String host = Constants.SMTP; // mail theme private String mailTitle; // to private String mailTo; // from private String mailFrom; // cc private String copyTo; // attachment private String[] attachment; // mail content private String mailContent; // is Auth private boolean isAuth = false; // username private String userName; // password private String passWord; public MailVO() { } public MailVO(String host, String mailTitle, String mailTo, String mailFrom, String copyTo, String[] attachment, String mailContent, boolean isAuth, String userName, String passWord) { super(); this.host = host; this.mailTitle = mailTitle; this.mailTo = mailTo; this.mailFrom = mailFrom; this.copyTo = copyTo; this.attachment = attachment; this.mailContent = mailContent; this.isAuth = isAuth; this.userName = userName; this.passWord = passWord; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getMailTitle() { return mailTitle; } public void setMailTitle(String mailTitle) { this.mailTitle = mailTitle; } public String getMailTo() { return mailTo; } public void setMailTo(String mailTo) { this.mailTo = mailTo; } public String getMailFrom() { return mailFrom; } public void setMailFrom(String mailFrom) { this.mailFrom = mailFrom; } public String getCopyTo() { return copyTo; } public void setCopyTo(String copyTo) { this.copyTo = copyTo; } public String[] getAttachment() { return attachment; } public void setAttachment(String[] attachment) { this.attachment = attachment; } public String getMailContent() { return mailContent; } public void setMailContent(String mailContent) { this.mailContent = mailContent; } public boolean isAuth() { return isAuth; } public void setAuth(boolean isAuth) { this.isAuth = isAuth; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }