javax mail 使用SSL协议465端口群发操作

javax mail 使用SSL协议465端口群发


GitHub: link. 欢迎star

注意:本篇博客风格(不多比比就是撸代码!!!)

一、maven依赖

		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>

二、MailUtil.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * @author Andon
 * 2021/11/11
 */
public class MailUtil {

    private static Logger LOG = LoggerFactory.getLogger(MailUtil.class);

    public void sendEmail(String mailAddress, String title, String content) {
        String[] split = mailAddress.split(",");
        Address[] addressesArr = new Address[split.length];
        for (int i = 0; i < split.length; i++) {
            try {
                addressesArr[i] = new InternetAddress(split[i]);
            } catch (AddressException e) {
                LOG.error("new InternetAddress failure, error={}", e.getMessage());
            }
        }
        String from = "######@163.com";
        String authorizationCode = "######"; //授权码
        String host = "smtp.163.com";
//        int port = 25;
        Properties properties = System.getProperties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.host", host);
//        properties.put("mail.smtp.port", port);
        // 阿里云服务器 通过JavaMail发送邮箱STMP问题
        // 25端口被禁用 使用SSL协议465端口
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.port", "465");
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("######@163.com", authorizationCode);
            }
        });

        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(from, "Andon", "UTF-8"));
            message.addRecipients(Message.RecipientType.TO, addressesArr);
            message.setSubject(title);
            message.setText(content);
            Transport.send(message);
        } catch (MessagingException | UnsupportedEncodingException e) {
            LOG.error("sendEmailService transport send message failure,{}", e.getMessage());
        }
    }
}

GitHub: link. 欢迎star

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值