009-MailUtils工具类模板
生活随笔
收集整理的這篇文章主要介紹了
009-MailUtils工具类模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
版本一:JavaMail的一個工具類
package ${enclosing_package};import java.security.GeneralSecurityException; import java.util.Properties;import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; 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.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType;import com.sun.mail.util.MailSSLSocketFactory;public class ${primary_type_name} {//email:郵件發給誰 subject:主題 emailMsg:郵件的內容public static void sendMail(String email, String subject, String emailMsg)throws AddressException, MessagingException {// 1.創建一個程序與郵件服務器會話對象 SessionProperties props = new Properties();props.setProperty("mail.transport.protocol", "SMTP");//發郵件的協議props.setProperty("mail.host", "smtp.126.com");//發送郵件的服務器地址props.setProperty("mail.smtp.auth", "true");// 指定驗證為true//開啟SSL加密 ,QQ郵箱必須開啟ssl加密才可以。MailSSLSocketFactory sf = null;try {sf = new MailSSLSocketFactory();} catch (GeneralSecurityException e) {e.printStackTrace();}sf.setTrustAllHosts(true);props.put("mail.smtp.ssl.enable", "true");props.put("mail.smtp.ssl.socketFactory", sf);// 創建驗證器Authenticator auth = new Authenticator() {public PasswordAuthentication getPasswordAuthentication() {//發郵件的賬號的驗證 12345為授權碼,并非密碼。tom可以不用加上@126.comreturn new PasswordAuthentication("tom", "12345");}};//這個是郵箱服務器的session 和之前學的session不是同一個sessionSession session = Session.getInstance(props, auth);// 2.創建一個Message,它相當于是郵件內容Message message = new MimeMessage(session);message.setFrom(new InternetAddress("tom@126.com")); // 設置發送者 message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 設置發送方式與接收者 message.setSubject(subject);//郵件的主題 message.setContent(emailMsg, "text/html;charset=utf-8");// 3.創建 Transport用于將郵件發送 Transport.send(message);} }?一個JavaMail的例子
package com.test.jobs;import java.security.GeneralSecurityException; import java.util.List; import java.util.Properties;import javax.annotation.Resource; import javax.mail.Authenticator; import javax.mail.Message.RecipientType; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;import com.sun.mail.util.MailSSLSocketFactory; import com.test.bos.dao.IWorkbillDao; import com.test.bos.domain.Workbill;/*** 發送郵件的作業* @author zhaoqx**/ public class MailJob {@Resourceprivate IWorkbillDao workbillDao;//屬性可以通過頁面提交獲得,也可以通過spring的配置文件注入private String username; //發件人郵箱private String password; //發郵件的賬號的驗證 12345為授權碼,并非密碼private String smtpServer;//發送郵件的服務器地址private String protocol;//發送右鍵的協議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;}public String getSmtpServer() {return smtpServer;}public void setSmtpServer(String smtpServer) {this.smtpServer = smtpServer;}public String getProtocol() {return protocol;}public void setProtocol(String protocol) {this.protocol = protocol;}public void execute() {System.out.println("要發郵件了。。。");try {//查詢工單類型為新單的所有工單List<Workbill> list = workbillDao.findAll();if(null != list && list.size() > 0){// 創建一個程序與郵件服務器會話對象 Sessionfinal Properties mailProps = new Properties();mailProps.put("mail.transport.protocol", this.getProtocol()); //發送郵件的協議mailProps.put("mail.host", this.getSmtpServer()); //發送郵件的服務器地址mailProps.put("mail.smtp.auth", "true"); // 指定驗證為truemailProps.put("mail.username", this.getUsername());mailProps.put("mail.password", this.getPassword());//開啟SSL加密 ,QQ郵箱必須開啟ssl加密才可以。MailSSLSocketFactory sf = null;try {sf = new MailSSLSocketFactory();} catch (GeneralSecurityException e) {e.printStackTrace();}sf.setTrustAllHosts(true);mailProps.put("mail.smtp.ssl.enable", "true");mailProps.put("mail.smtp.ssl.socketFactory", sf);// 構建授權信息,用于進行SMTP進行身份驗證Authenticator authenticator = new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {// 用戶名、密碼String userName = mailProps.getProperty("mail.username");String password = mailProps.getProperty("mail.password");return new PasswordAuthentication(userName, password);}};// 使用環境屬性和授權信息,創建郵件會話Session mailSession = Session.getInstance(mailProps, authenticator);for(Workbill workbill : list){// 創建郵件消息MimeMessage message = new MimeMessage(mailSession);// 設置發件人InternetAddress from = new InternetAddress(mailProps.getProperty("mail.username"));message.setFrom(from);// 設置收件人InternetAddress to = new InternetAddress("151956669@qq.com");//設置發送方式與接收者 message.setRecipient(RecipientType.TO, to);// 設置郵件標題message.setSubject("激活郵件");// 設置郵件的內容體message.setContent(workbill.toString(), "text/html;charset=UTF-8");// 發送郵件 Transport.send(message);}}} catch (Exception ex) {ex.printStackTrace();}} }
spring的注入方式如下:
<bean name="myJob" class="com.test.jobs.MailJob"><property name="username" value="tom@126.com"/><property name="password" value="123456"/><property name="smtpServer" value="smtp.126.com"/><property name="protocol" value="SMTP"/> </bean>?
轉載于:https://www.cnblogs.com/jepson6669/p/8361958.html
總結
以上是生活随笔為你收集整理的009-MailUtils工具类模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络编程---黏包
- 下一篇: AutoMapper的使用