javaEmail三:发送邮件

  • 添加maven依赖
    在 maven 配置文件 pom.xml 中添加依赖:

    javax.activation
    activation
    1.1


    javax.mail
    mail
    1.4

  • 发送邮件(纯文本)
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
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 java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class MailOper {

    /**
     * 发送邮件(纯文本)
     * 带有抄送人
     *
     * @param emailMultipleRecipientList 收件人(多个收件人)
     * @param emailSubject               邮件主题
     * @param emailContent               邮件内容
     * @param emailUser                  发送邮箱地址
     * @param emailPassword              发送邮箱授权码或者密码
     * @param emailPersonal              发送邮箱中文名称
     * @param emailCCRecipientsList      抄送人(多个抄送人)
     */
    public static String sendTextMail(List emailMultipleRecipientList, String emailSubject,
                                      String emailContent, String emailUser, String emailPassword,
                                      String emailPersonal, List emailCCRecipientsList) throws Exception {
        Transport trans = null;
        try {
            //1.创建参数配置, 用于连接邮件服务器的参数配置
            Properties props = System.getProperties();
            //2.发件人的邮箱的 SMTP 服务器地址
            props.put("mail.smtp.host", MailConstant.host);
            //3.需要请求认证
            props.put("mail.smtp.auth", MailConstant.auth);
            //4.使用的协议(JavaMail规范要求)
            props.setProperty("mail.transport.protocol", MailConstant.protocol);
            //邮箱发送服务器端口,这里设置为465端口
            props.put("mail.smtp.port", MailConstant.port);
            //JavaMail指定了一组可以设置为配置SMTP连接的属性。要使用STARTTLS,必须设置以下属性
            props.put("mail.smtp.starttls.enable", MailConstant.enable);
            //5.验证权限
            MailAuth mailAuth = new MailAuth(emailUser, emailPassword);
            //6.根据配置创建会话对象, 用于和邮件服务器交互
            Session session = Session.getInstance(props, mailAuth);
            //7.设置为debug模式, 可以查看详细的发送 log
            session.setDebug(true);
            //8.创建一封邮件
            MimeMessage message = new MimeMessage(session);
            //9.From: 发件人
            message.setFrom(new InternetAddress(emailUser, emailPersonal, MailConstant.chartSet));
            //10.TO: 收件人(可以增加多个收件人)
            int to_length = emailMultipleRecipientList.size();
            InternetAddress[] to_sendTo = new InternetAddress[to_length];
            for (int i = 0; i < to_length; i++) {
                if (null != emailMultipleRecipientList.get(i)) {
                    String fsAddress = emailMultipleRecipientList.get(i).getSjAddress();
                    String faPersonal = emailMultipleRecipientList.get(i).getSjPersonal();
                    to_sendTo[i] = new InternetAddress(fsAddress, faPersonal, MailConstant.chartSet);
                }
            }
            message.setRecipients(MimeMessage.RecipientType.TO, to_sendTo);
            //11.CC: 抄送收件人(可以增加多个抄送人)
            if (!ListUtil.isNullOrEmpty(emailCCRecipientsList)) {
                int cc_length = emailCCRecipientsList.size();
                InternetAddress[] cc_sendTo = new InternetAddress[cc_length];
                for (int i = 0; i < cc_length; i++) {
                    if (null != emailCCRecipientsList.get(i)) {
                        String ccAddress = emailCCRecipientsList.get(i).getCcAddress();
                        String ccPersonal = emailCCRecipientsList.get(i).getCcPersonal();
                        cc_sendTo[i] = new InternetAddress(ccAddress, ccPersonal, MailConstant.chartSet);
                    }
                }
                message.setRecipients(Message.RecipientType.CC, cc_sendTo);
            }
            //12.Subject: 邮件主题
            message.setSubject(emailSubject, MailConstant.chartSet);
            //13.Content: 邮件正文
            message.setContent(emailContent, MailConstant.contentType);
            //14.设置发件时间
            message.setSentDate(new Date());
            //15.保存设置
            message.saveChanges();
            //16.根据 Session 获取邮件传输对象
            trans = session.getTransport(MailConstant.protocol);
            //17.发送邮件, 发到所有的收件地址
            Transport.send(message);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "failure";
        } finally {
            if (null != trans) {
                trans.close();
            }
        }
    }

    public static class EmailCCRecipients {
        private String ccAddress;
        private String ccPersonal;

        public String getCcAddress() {
            return ccAddress;
        }

        public void setCcAddress(String ccAddress) {
            this.ccAddress = ccAddress;
        }

        public String getCcPersonal() {
            return ccPersonal;
        }

        public void setCcPersonal(String ccPersonal) {
            this.ccPersonal = ccPersonal;
        }
    }

    public static class EmailFSRecipients {
        private String sjAddress;
        private String sjPersonal;

        public String getSjAddress() {
            return sjAddress;
        }

        public void setSjAddress(String sjAddress) {
            this.sjAddress = sjAddress;
        }

        public String getSjPersonal() {
            return sjPersonal;
        }

        public void setSjPersonal(String sjPersonal) {
            this.sjPersonal = sjPersonal;
        }
    }
}
public abstract class MailConstant {

    /**
     * 邮件服务器
     */
    public static final String host = "smtp.server.com";

    /**
     * 
     */
    public static final String protocol = "smtp";

    /**
     * 
     */
    public static final String auth = "true";

    /**
     * 
     */
    public static final String enable = "true";

    /**
     * 
     */
    public static final int port = 465;

    /**
     * 
     */
    public static final String chartSet = "UTF-8";

    /**
     * 
     */
    public static final String contentType = "text/html;charset=UTF-8";
}

import java.util.ArrayList;
import java.util.List;

public class ListUtil {
    
    public static boolean isNullOrEmpty(List list) {
        return (list == null || list.size() == 0);
    }
    
}

你可能感兴趣的:(javaEmail三:发送邮件)