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("我们发着玩~~");
    }
}

你可能感兴趣的:(Java)