java发送邮件失败:报错org.apache.commons.mail.EmailException: Sending the email to the following server faile

Disconnected from the target VM, address: '127.0.0.1:49693', transport: 'socket'
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.163.com:25
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.163.com:25
	at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1138)
	at org.apache.commons.mail.Email.send(Email.java:1163)
	at selenium_utils.SendEmail.sendToEmail(SendEmail.java:18)
	at selenium_utils.SendEmail.main(SendEmail.java:26)
Caused by: javax.mail.AuthenticationFailedException
	at javax.mail.Service.connect(Service.java:306)
	at javax.mail.Service.connect(Service.java:156)
	at javax.mail.Service.connect(Service.java:105)
	at javax.mail.Transport.send0(Transport.java:168)
	at javax.mail.Transport.send(Transport.java:98)
	at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1128)
	... 3 more

Process finished with exit code 0

1,从上面的错误信息可以得出,是因为授权问题造成不能正常连接到smtp服务器。。首先开启一定要记得把邮箱的pop3.smtp服务开启。。(如:在163邮箱里的设置进去SMTP/POP3/IMAP,开启SMTP/POP3/IMAP服务。。

注:设置的‘授权密码’很可能和‘登录密码’不同。。

所以一定要分清。。


发送邮件需要用到三个第三方jar包:activation-1.1.jar/commons-email-1.1.jar/mail-1.4.jar

package selenium_utils;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SendEmail {
    public static void sendToEmail(String content) {
        SimpleEmail email = new SimpleEmail();
        email.setHostName("smtp.163.com");
        email.setAuthentication("邮箱名@163.com", "注意是‘授权密码");
        try {
            email.setFrom("邮箱名@163.com");
            email.addTo("要发送的邮箱名@xx.com");
            email.setSubject("selenium自动化测试邮件");
            //email.setContent("这是我们发送的测试邮件","text/html;charset=utf-8");
            email.setContent(content, "text/html;charset=utf-8");
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        sendToEmail("我们发着玩~~");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值