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 | |
阿里企业邮箱 | smtp.qiye.aliyun.com | 25 | 465 | |
网易 163 免费邮箱 | smtp.163.com | 25 | 465/994 | |
网易企业邮箱 | 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 功能 ().
QQ 邮箱设置方法: 设置 -- 账户 -- 账户安全 -- 独立密码.
网易 163 免费邮箱同 QQ 邮箱一样, 需先设置授权码才能使用 smtp 功能 ()
网易 163 免费邮箱开启授权码方法:
网易企业邮箱在开启客户端授权密码的功能时才需要设置客户端授权码: https://qiye.163.com/help/3f85a9.html
网易企业邮箱开启授权码方法: https://qiye.163.com/help/af988e.html
来源: https://www.cnblogs.com/ggwudivs/p/12056122.html