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

时间:2014-06-17 14:22:14

标签: java email

早上好!我在这里搜索了很多寻找解决方案,但我发现的一切都没有用。 我有类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);

    }

}

主要课程:

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();
        }
    }

}

此代码曾用于工作,但它不再起作用了。当我运行它时,我得到了这个例外:

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

请原谅我,如果我犯了一些错误,我是新来的。

更新:我终于找到了错误!邮件服务器可能已更改,现在需要TLS身份验证。我将身份验证更改为TLS并且有效!

1 个答案:

答案 0 :(得分:1)

您正在设置端口587,但是在该方法中启用了SSL(465)

this.setSSL(true);

你的防火墙可能阻塞了端口465.尝试删除那段代码并用于真正的端口587并查看它是否有效。如果是这样,您必须联系您的网络管理员或努力进行网络配置。