简体   繁体   English

使用Java Commons Mail发送电子邮件时出错

[英]Error sending email with Java Commons Mail

Good morning! 早上好! I have searched a lot here to find a solution, but everything I found didn't work. 我在这里进行了很多搜索以找到解决方案,但我发现的所有内容均无效。 I have the class Mail: 我有邮件课:

package model.mail;

import java.nio.charset.Charset;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class Mail extends MultiPartEmail {

    // Default constructor
    public Mail() throws EmailException {

        this( null, null, null, null);

    }

    // My Constructor
    @SuppressWarnings("deprecation")
    public Mail( String receiver, String subject, String message, EmailAttachment[] attachments ) throws EmailException {

        this.addTo( receiver, "" );

        this.setSubject( new String( subject.getBytes( Charset.forName("utf-8") ), Charset.forName("utf-8") ) );

        this.setMsg( new String( message.getBytes( Charset.forName("utf-8") ), Charset.forName("utf-8") ) );

        if( attachments != null ) {
            for ( int i = 0; i < attachments.length; i++ )
                this.attach( attachments[i] );
        }

        this.setSSL(true);

    }

}

And the Main class: 和主类:

package main;

import model.mail.Mail;

public class Main {

    public static void main(String[] args) {

        try {

            Mail email = new Mail( "user@domain.com", "Teste", "teste..", null );
            email.setHostName( "smtp.mail.yahoo.com" );
            email.setSmtpPort( 587 );
            email.setAuthentication( "user@yahoo.com.br", "mypassword" );
            email.setFrom( "user@yahoo.com.br", "My Name" );
            email.send();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

This code used to work, but it's not working anymore. 该代码曾经可以工作,但现在不再工作。 When I run it, I get this exception: 运行它时,出现以下异常:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.mail.yahoo.com:465
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
    at org.apache.commons.mail.Email.send(Email.java:1437)
    at main.Main.main(Main.java:29)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.mail.yahoo.com, 465; timeout 60000;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:367)
    at javax.mail.Service.connect(Service.java:226)
    at javax.mail.Service.connect(Service.java:175)
    at javax.mail.Transport.send0(Transport.java:253)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
    ... 2 more
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:295)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:208)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 9 more

Please, forgive-me if I did some mistakes, I am new here. 请原谅我,如果我犯了一些错误,我是新来的。

UPDATED: I finally found the error! 更新:我终于找到了错误! Probably, the mail server changed and now it requires TLS authentication. 邮件服务器可能已更改,现在需要TLS身份验证。 I changed the authentication to TLS and it worked! 我将身份验证更改为TLS,它起作用了!

You are setting port 587, but then in the method is enabled SSL(465) 您正在设置端口587,但随后在该方法中启用了SSL(465)

this.setSSL(true);

Your firewall may be blocking port 465. Try to remove that piece of code and use for real port 587 and see if it works. 您的防火墙可能阻止了端口465。请尝试删除该段代码,并将其用于实际端口587,以查看其是否有效。 If so, you have to contact your network administrator or struggle with network configuration. 如果是这样,您必须联系您的网络管理员或在网络配置方面遇到困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM