添加配置文件:mail.propertieshtml
#郵件發送smtp
mail.send.host=smtp.sina.com.cn
#發送者
mail.send.from=systematq@sina.com
#郵件發送用戶名
mail.send.user=systematq
#郵件發送密碼
mail.send.password=XXXXXXX
#系統收件人
mail.server.user=systematq@sina.com
# 編碼
mail.smtp.encode=utf-8
#信息編碼
mail.smtp.messageEncode=utf-8
#服務狀態 1爲啓用,0爲禁用
mail.smtp.status=1
#是否驗證身份
mail.send.auth=true
#模板地址
mail.model.address=/template/tool/sendEmailModel.ftl
Java工具類java
package com.ata.task; /** * 郵件發�?�工具類 * * @author xxh * @link * * @version $Revision: 1.00 $ $Date: 2009-12-18 */ import java.io.IOException; import java.util.Date; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class MailUtil { /******* * 郵件發�?�,帶用戶名和密碼驗證,測試經過 * * @param to * @param from * @param title * @param content * @param smtpServer * @param user * @param password * @throws AddressException * @throws MessagingException */ @SuppressWarnings({"unchecked"}) public static void send(String from, String to,String copyTo, String title, String content, String smtpServer, String user, String password,boolean isHTML) throws AddressException, MessagingException { Properties props = new Properties(); Authenticator auth = new MailAuthenticator(user, password); Session sendMailSession; Transport transport; props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.auth", "true"); sendMailSession = Session.getInstance(props, auth); Message newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress(from)); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress( to)); newMessage.addRecipient(Message.RecipientType.CC,new InternetAddress( copyTo)); newMessage.setSubject(title); newMessage.setSentDate(new Date()); if (isHTML) { newMessage.setContent(content, "text/html;charset=UTF-8"); } else { newMessage.setText(content); } transport = sendMailSession.getTransport("smtp"); Transport.send(newMessage); transport.close(); //System.out.println("---------- Mail Send Success ----------"); } /******************* * 郵件發�?�,不帶用戶名和密碼驗證 * @param to * @param from * @param title * @param content * @param smtpServer * @throws AddressException * @throws MessagingException */ @SuppressWarnings("static-access") public static void send(String to, String from, String title, String content, String smtpServer) throws AddressException, MessagingException { Properties props = new Properties(); Authenticator auth = new MailAuthenticator(); Session sendMailSession; Transport transport; sendMailSession = Session.getInstance(props, auth); props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.auth", "true"); Message newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress(from)); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress( to)); newMessage.setSubject(title); newMessage.setSentDate(new Date()); newMessage.setText(content); transport = sendMailSession.getTransport("smtp"); transport.send(newMessage); } /********************* * 帶附件郵件發送,帶用戶名和密碼驗證,測試經過 * @param to * @param from * @param title * @param content * @param smtpServer * @param user * @param password * @param fileNames * @param paths * @throws AddressException * @throws MessagingException */ @SuppressWarnings("static-access") public static void send(String to, String from, String title, String content, String smtpServer, String user, String password, String fileNames[], String[] paths) throws AddressException, MessagingException { try { Properties props = new Properties(); Authenticator auth = new MailAuthenticator(user, password); Session sendMailSession; Transport transport; props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.auth", "true"); sendMailSession = Session.getInstance(props, auth); MimeMessage newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress(from)); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 建立 Multipart 並放入每�? MimeBodyPart Multipart mp = new MimeMultipart(); if (title != null && !title.equals("")) { // 設置郵件主題 newMessage.setSubject(title); } else { newMessage.setSubject("無主�?"); } // 第一部分信息 MimeBodyPart mbp1 = new MimeBodyPart(); if (content != null && !content.equals("")) { mbp1.setText(content, "GBK"); } else { mbp1.setText(new String(), "GBK"); } mp.addBodyPart(mbp1); // // 在第二部分信息中附加文件 // if (paths != null) { // for (int i = 0; i < paths.length; i++) { // MimeBodyPart mbp2 = new MimeBodyPart(); // FileDataSource fds = new FileDataSource(CommonConstants.ROOTPATH // + paths[i]); // mbp2.setDataHandler(new DataHandler(fds)); // mbp2.setFileName(fileNames[i]); // mp.addBodyPart(mbp2); // } // } // 增長 Multipart 到信息體 newMessage.setContent(mp); newMessage.setSentDate(new Date()); transport = sendMailSession.getTransport("smtp"); transport.send(newMessage); } catch (Exception e) { e.printStackTrace(); } } } class MailAuthenticator extends Authenticator { private String user; private String password; public MailAuthenticator() { } public MailAuthenticator(String user, String password) { this.user = user; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }
測試Java類:spring
package com.ata.task; import java.sql.Timestamp; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.ResourceBundle; import javax.mail.MessagingException; import javax.mail.internet.AddressException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.ata.model.sys.SysMailSend; import com.ata.service.sys.MailSendServiceI; /** * 郵件服務 * @author huo * */ @Component public class AutoSendMail { private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("mail"); @Autowired private MailSendServiceI mailSendService; private static final int NotSendStat=0;//未發送 private static final int SendedStat=1;//已發送 private static final int SendFailStat=2;//失敗 public void doJob(){ try { System.out.println("sendMail.................."); List<SysMailSend> list=mailSendService.findSysMailSend(AutoSendMail.NotSendStat); for (Iterator iterator = list.iterator(); iterator.hasNext();) { SysMailSend sysMailSend = (SysMailSend) iterator.next(); sysMailSend.setPlanTime(new Timestamp(System.currentTimeMillis())); sysMailSend=this.sendMail(sysMailSend); mailSendService.updateSysMailSend(sysMailSend); } } catch (Exception e) { e.printStackTrace(); } } /** * @param args */ public SysMailSend sendMail(SysMailSend sysMailSend) { String mailHost = bundle.getString("mail.send.host"); String from = bundle.getString("mail.send.from"); String mailUser =bundle.getString("mail.send.user"); String mailPassword = bundle.getString("mail.send.password"); String to=sysMailSend.getSendTo(); String copyTo=sysMailSend.getCopyTo(); String title=sysMailSend.getMailTitle(); String content=sysMailSend.getMailContent(); sysMailSend.setFromAddr(from); try { sysMailSend.setSendTime(new Timestamp(System.currentTimeMillis())); MailUtil.send( from,to,copyTo, title, content, mailHost, mailUser, mailPassword, false); sysMailSend.setMailStat(AutoSendMail.SendedStat); System.out.println("Mail Send Success"); return sysMailSend; } catch (AddressException e) { System.out.println("AddressException-------" + e.getMessage()); e.printStackTrace(); sysMailSend.setMailStat(AutoSendMail.SendFailStat); return sysMailSend; } catch (MessagingException e) { System.out.println("MessagingException-------" + e.getMessage()); e.printStackTrace(); sysMailSend.setMailStat(AutoSendMail.SendFailStat); return sysMailSend; } } }