JavaMail发送邮件发布出去问题

java发送邮件方法,总是报错,报错信息如下:javax.mail.AuthenticationFailedException

连接不上smtp服务器,原因是stmp服务器没有授权,比如说你用qq发邮件:927xxxxx@qq.com    你就必须对当前邮箱进行授权,授权步骤百度很简单,需要发送一个短信就开了授权。

Transport transport = mailSession.getTransport("smtp");
        transport.connect(smtpHost, "xxxxx@163.com", "密码");//密码是授权码
        transport.sendMessage(testMessage, testMessage.getAllRecipients());
        transport.close();

这样之后就不会报这个错了:javax.mail.AuthenticationFailedException。
我在继续进行测试的时候,发现报另外一个错误

javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:823)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:673)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at com.ab.itws.common.SendHtmlMail.sendMessage(SendHtmlMail.java:43)
    at com.ab.itws.common.SendHtmlMail.main(SendHtmlMail.java:61)

 

发现原来是端口的问题

             // SMTP 服务器的端口 (非 SSL 连接的端口一般默认为 25, 可以不添加, 如果开启了 SSL 连接, //
            /* 需要改为对应邮箱的 SMTP 服务器的端口, 具体可查看对应邮箱服务的帮助, // QQ邮箱的SMTP(SLL)端口为465或587, */
System.out.println("Configuring mail session for: " + smtpHost); 
java.util.Properties props = new java.util.Properties();
props.setProperty("mail.smtp.auth", "true");// 指定是否需要SMTP验证
props.setProperty("mail.smtp.host", smtpHost);
// 指定SMTP服务器
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", "587");
//SMTP的端口号
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
// 是否在控制台显示debug信息

端口改好之后,终于解决了问题。
邮件成功发出

 

转载于:https://www.cnblogs.com/daweige/p/8483979.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值