版本一: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>