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.创建一个程序与邮件服务器会话对象 Session
        Properties 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.com
                return new PasswordAuthentication("tom", "12345");
            }
        };

        //这个是邮箱服务器的session 和之前学的session不是同一个session
        Session 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 {
    @Resource
    private 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){ // 创建一个程序与邮件服务器会话对象 Session final Properties mailProps = new Properties(); mailProps.put("mail.transport.protocol", this.getProtocol()); //发送邮件的协议 mailProps.put("mail.host", this.getSmtpServer()); //发送邮件的服务器地址 mailProps.put("mail.smtp.auth", "true"); // 指定验证为true mailProps.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