java发送邮件基础方法,可通过重载简化参数

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 javax.mail.internet.MimeUtility;

public class MailUtil {

    /**
     * @Description: 使用QQ邮箱发送不带附件的邮件,收件人邮箱类型不限。
     * @date: 2019年12月17日 下午4:51:01
     * @author: ggwudivs
     * @param subject        邮件标题
     * @param content        邮件内容
     * @param fromUser        发件人邮箱
     * @param fromPass        发件人邮箱密码,应为16位SMTP口令
     * @param tO_Recipients    收件人邮箱,多个收件人用","分隔
     * @param openSSL        是否开启SSL
     * @throws UnsupportedEncodingException
     * @throws MessagingException:
     * @return: void
     */
    public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, boolean openSSL) throws UnsupportedEncodingException, MessagingException{
        sendMessage(subject, content, fromUser, fromPass, null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", null, openSSL);
    }
    
    /**
     * @Description: 使用QQ邮箱发送带附件的邮件,收件人邮箱类型不限。
     * @date: 2019年12月17日 下午5:25:14
     * @author: ggwudivs
     * @param subject                邮件标题
     * @param content                邮件内容
     * @param fromUser                发件人邮箱
     * @param fromPass                发件人邮箱密码,应为16位SMTP口令
     * @param tO_Recipients            收件人邮箱,多个收件人用","分隔
     * @param attachmentFilesPath    邮件附件路径,多个附件用","分隔
     * @param openSSL                是否开启SSL
     * @throws UnsupportedEncodingException
     * @throws MessagingException:
     * @return: void
     */
    public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, String attachmentFilesPath, boolean openSSL) throws UnsupportedEncodingException, MessagingException{
        sendMessage(subject, content, fromUser, fromPass, null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", attachmentFilesPath, openSSL);
    }
    
    /**
     * @Description: smtp发送邮件
     * @date: 2019年12月17日 下午3:22:35
     * @author: ggwudivs
     * @param subject                邮件标题
     * @param content                邮件文本内容
     * @param fromUser                发件人邮箱
     * @param fromPass                发件人邮箱密码,QQ邮箱应为16位SMTP口令
     * @param nickname                发件人昵称
     * @param tO_Recipients            收件人邮箱,多个收件人用","分隔
     * @param cC_Recipients            抄送人邮箱,多个抄送人用","分隔
     * @param bCC_Recipients        密送人邮箱,多个密送人用","分隔
     * @param smtpHost                smtp服务器地址
     * @param smtpPort                smtp服务器端口
     * @param attachmentFilesPath    邮件附件路径,多个附件用","分隔
     * @throws MessagingException
     * @throws UnsupportedEncodingException:
     * @return: void
     */
    public static void sendMessage(String subject, String content, String fromUser, String fromPass, String nickname, String tO_Recipients, String cC_Recipients, String bCC_Recipients, String smtpHost, String smtpPort, String attachmentFilesPath, boolean openSSL) throws MessagingException, UnsupportedEncodingException {  
        //创建Properties类,用于记录邮箱的一些属性
        Properties props = new Properties();
        //表示SMTP发送邮件,必须进行身份验证
        props.put("mail.smtp.auth", "true");
        //SMTP服务器地址
        props.put("mail.smtp.host", smtpHost);
        //是否开启SSL
        if(!openSSL){
            //SMTP服务器端口号
            props.put("mail.smtp.port", smtpPort);
        }else{
            //SMTP服务器ssl端口号
            props.setProperty("mail.smtp.socketFactory.port", smtpPort);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        }

        //构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                //发件人账号,发件人密码(QQ邮箱应为16位SMTP口令)
                return new PasswordAuthentication(fromUser, fromPass);
            }
        };
        
        //使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        
        //创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        
        //设置发件人,有昵称时同时设置昵称
        try {
            message.setFrom((nickname==null||"".equals(nickname))?new InternetAddress(fromUser):new InternetAddress(fromUser, nickname, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //设置一个或多个收件人
        message.setRecipients(RecipientType.TO, tO_Recipients);
        
        //设置一个或多个抄送人
        message.setRecipients(RecipientType.CC, cC_Recipients);
        
        //设置一个或多个密送人
        message.setRecipients(RecipientType.BCC, bCC_Recipients);

        //设置邮件标题
        message.setSubject(subject);

        //设置邮件的内容
        if(attachmentFilesPath == null || "".equals(attachmentFilesPath)){
            //设置邮件的正文文本
            message.setContent(content, "text/html;charset=UTF-8");
        }else{
            //向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
            Multipart multipart = new MimeMultipart();
            
            //添加邮件文本内容
            BodyPart contentBodyPart = new MimeBodyPart();
            contentBodyPart.setContent(content, "text/html;charset=utf-8");
            multipart.addBodyPart(contentBodyPart);
            
            //添加邮件附件内容
            BodyPart attachmentBodyPart = new MimeBodyPart();
            String[] attachmentFiles = attachmentFilesPath.split(",");
            for (String attachmentFile : attachmentFiles) {
                if (attachmentFile != null && !"".equals(attachmentFile)) {
                    attachmentBodyPart = new MimeBodyPart(); 
                    
                    //根据附件路径获取文件,
                    FileDataSource dataSource = new FileDataSource(new File(attachmentFile));
                    attachmentBodyPart.setDataHandler(new DataHandler(dataSource));
                    
                    //MimeUtility.encodeWord可以避免文件名乱码
                    String strFileName=dataSource.getFile().getName();
                    attachmentBodyPart.setFileName(MimeUtility.encodeText(strFileName));  
                    
                    multipart.addBodyPart(attachmentBodyPart);
                }
            }
            
            //设置邮件的正文内容
            message.setContent(multipart);
        }
        
        //发送邮件
        Transport.send(message);
    }  
}
邮箱类型 SMTP服务器地址 普通端口 SSL端口 服务器配置参考地址
QQ邮箱 smtp.qq.com 587 465 https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=167
阿里企业邮箱 smtp.qiye.aliyun.com 25 465 http://mailhelp.mxhichina.com/smartmail/detail.vm?knoId=5871700
网易163免费邮箱 smtp.163.com 25 465/994 https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac22dc0e9af8168582a
网易企业邮箱 smtp.qiye.163.com 25 994 https://qiye.163.com/help/client-profile.html
网易免费企业邮箱 smtp.ym.163.com 25 994 http://app.ym.163.com/ym/help/help.html

 tips:

QQ邮箱需先设置独立密码才能使用smtp功能(https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001220&&id=28)。

QQ邮箱设置方法:设置--账户--账户安全--独立密码。

网易163免费邮箱同QQ邮箱一样,需先设置授权码才能使用smtp功能(https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac24a2130dd2fad05b1

网易163免费邮箱开启授权码方法:https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2cda80145a1742516

网易企业邮箱在开启客户端授权密码的功能时才需要设置客户端授权码:https://qiye.163.com/help/3f85a9.html

网易企业邮箱开启授权码方法:https://qiye.163.com/help/af988e.html