com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send ano

以下是使用office365出现的错误

我的配置:



#email
#SMTP服务器的名字
spring.mail.host=xxxx
#发送人邮箱名称
spring.mail.username=xxxx
#邮箱密码,这里指的是,在邮箱中打开SMTP/POP3验证之后,会给出一个验证码
spring.mail.password =xxxx
#编码格式
spring.mail.default-encoding=UTF-8
#SMTP服务器开放的端口
spring.mail.port=587
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=true
spring.mail.properties.mail.smtp.starttls.enable=true


使用java调用Office365发送邮件的时候报如下错误:   

  com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at com.paraview.mail.MailSender.sendTextMail(MailSender.java:60)
    at com.paraview.mail.Test.main(Test.java:10)

   邮箱配置如下:

    SMTP 设置

    服务器名称: smtp.outlook.cn

   端口: 587

    加密方法: TLS

  我通过Foxmail配置后是可以正常收发邮件的,为何通过代码不可以呐?

  从邮箱配置上我可以看到加密方法: TLS ,TLS不是SSL  所以我在我的配置上加上了配置   proper.put("mail.smtp.starttls.enable", "true");

 然后继续运行,发现错误不一样了

这个错误又是为何?不能通过TLS建立连接,但是明明是TLS啊。网上有一些blog说必须是jdk1.6不能是jdk1.7虽然搞不明白为何,但是索性也把jdk版本换了

结果错误依旧啊!

这个时候索性我就想干脆把所有的配置都加上,看看什么效果,于是我就又加了如下配置:

proper.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
proper.put("mail.smtp.socketFactory.port","587");
        proper.put("mail.smtp.socketFactory.fallback", "false"); 

这个时候错误又变了:Could not connect to SMTP host: smtp.partner.outlook.cn, port: 587;为何连不上578端口,端口是肯定没问题,一定是 配置有问题

  于是我又加上了配置:

      MailSSLSocketFactory sf = new MailSSLSocketFactory(); 
      sf.setTrustAllHosts(true); 
proper.put("mail.smtp.ssl.socketFactory", sf);

错误依旧啊!这个时候已经无力吐槽了,继续查,错误里面有一个错误是这个:

Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

这个是什么错?

于是我又问了下度娘,结果度娘说 这个配置mail.smtp.socketFactory.fallback要改成 true 。

于是我就改了配置 proper.put("mail.smtp.socketFactory.fallback", "true"); 
 

猜你喜欢

转载自blog.csdn.net/qq_39313596/article/details/103348596