阿里云JavaMail发送邮报错 No appropriate protocol

阿里云JavaMail发送邮报错 No appropriate protocol

错误 Could not connect to SMTP host: smtp.qq.com, port: 465
错误 Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
阿里云把邮件端口25给屏蔽了,需要换端口为465 另外需要ssl加密,但是阿里云的openjdk1.8不支持,需要替换为原始的jdk1.8亲自测试有效

邮件代码

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Properties;
/**
 * @className SendMailUtil
 * @description
 * @author cong.huo
 * @date 2020/10/29 2:48 下午
 * @version V1.0
 **/
@Slf4j
@Component
public class MailUtil {
   
    /**
     * 发件人
     **/
    @Value("${spring.mail.username}")
    private String username;
    /**
     * 邮件发送的服务器host
     **/
    @Value("${spring.mail.host}")
    private String host;
    /**
     * 邮件发送的服务器密码
     **/
    @Value("${spring.mail.password}")
    private String password;
    /**
     * 邮件发送的服务器密码
     **/
    @Value("${spring.mail.port}")
    private String port;
    
    public  boolean sendMailUtil(ByteArrayOutputStream byteArrayOutputStream , String toEmail,String fileName,String subject,String text) {
   
        try {
   
            // 配置发送邮件的环境属性
            final Properties props = new Properties();
            // 表示SMTP发送邮件,需要进行身份验证
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", host);
            // props.put("mail.smtp.port", ALIDM_SMTP_PORT);
            // 如果使用ssl,则去掉使用25端口的配置,进行如下配置,
            props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.port", port);
            props.put("mail.smtp.port", port);
            // 发件人的账号
            props.put("mail.user", username);
            // 访问SMTP服务时需要提供的密码
            props.put("mail.password", password);
            // 构建授权信息,用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
   
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
   
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息,创建邮件会话
            Session mailSession =    Session.getInstance(props, authenticator);


            Message message = new MimeMessage(mailSession);

            //防止邮件被当然垃圾邮件处理,披上Outlook的马甲
            message.addHeader("X-Mailer","Microsoft Outlook Express 6.00.2900.2869");

            message.setFrom(new InternetAddress(username));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));

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

            BodyPart bodyPart = new MimeBodyPart();

            bodyPart.setText(text);

            Multipart multipart = new MimeMultipart();

            multipart.addBodyPart(bodyPart);

            //附件
            bodyPart = new MimeBodyPart()</
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值