获取javax.mail.MessagingException和java.net.SocketException

时间:2011-04-14 06:16:58

标签: java email socketexception javax.mail

我正在尝试使用我的yahoo(smtp.mail.yahoo.com)帐户的SMTP向我的Gmail帐户发送电子邮件。

但我得到了例外。

javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2153)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1912)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
        at javax.mail.Service.connect(Service.java:295)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:125)
        at javax.mail.Transport.send0(Transport.java:194)
        at javax.mail.Transport.send(Transport.java:124)
        at Sendmail.postMail(Sendmail.java:40)
        at Sendmail.main(Sendmail.java:49)
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
        at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2131)
        ... 9 more
BUILD SUCCESSFUL (total time: 2 seconds)


import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

代码:

public class Sendmail {

    public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException {

        boolean debug = false;

        // Set the host smtp address
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.mail.yahoo.com");

        // create some properties and get the default Session
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);

        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);


        // Optional : You can also set your custom headers in the Email if you Want
        msg.addHeader("MyHeaderName", "myHeaderValue");

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }

    public static void main(String[] args) {

        try{

            Sendmail sende = new Sendmail();
            String senderemailid [] = {"poorna.anu@gmail.com"};
            sende.postMail(senderemailid,"Hi","Come to room","poorna_meenakshi@yahoo.com");

        }catch(MessagingException e){
            e.printStackTrace();
        }        
    }
}

4 个答案:

答案 0 :(得分:1)

smtp.mail.yahoo.net是指向许多不同邮件服务器的CNAME。雅虎今天的邮件服务器碰巧遇到了一些问题......

broach@broach-laptop:~$ telnet smtp.mail.yahoo.com 25
Trying 98.136.185.95...
Connected to smtp.mail.us.am0.yahoodns.net.
Escape character is '^]'.
Connection closed by foreign host.

broach@broach-laptop:~$ telnet smtp.mail.yahoo.com 25
Trying 98.138.84.55...
Connected to smtp.mail.us.am0.yahoodns.net.
Escape character is '^]'.
220 smtp113.mail.ne1.yahoo.com ESMTP

broach@broach-laptop:~$ telnet smtp.mail.yahoo.com 25
Trying 98.139.212.139...
Connected to smtp.mail.us.am0.yahoodns.net.
Escape character is '^]'.
Connection closed by foreign host.

刚刚尝试了5次我有3次失败和2次连接。

答案 1 :(得分:0)

尝试设置mail.transport.protocol属性

props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", "25");

您确实需要其他一些代码进行身份验证。雅虎要求您在使用他们的smtp服务之前对自己进行身份验证 - 他们不允许任何人使用他们的smtp服务器进行中继

答案 2 :(得分:0)

这是我的代码以防万一:

 import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;

    public class SendEmail
    {
       public static void main(String [] args)
       {    
          // Recipient's email ID needs to be mentioned.
          String to = "sameuser@yahoo.com";

          // Sender's email ID needs to be mentioned
          String from = "sameuser@yahoo.com";

          // Assuming you are sending email from localhost
          String host = "localhost";

          // Get system properties
          Properties properties = System.getProperties();

          // Setup mail server
          properties.put("mail.transport.protocol", "smtp");
          properties.put("mail.smtp.port", "25");
          properties.put("mail.smtp.host", "smtp.mail.yahoo.com.net");

          // Get the default Session object.
          Session session = Session.getDefaultInstance(properties);

          try{
             // Create a default MimeMessage object.
             MimeMessage message = new MimeMessage(session);

             // Set From: header field of the header.
             message.setFrom(new InternetAddress(from));

             // Set To: header field of the header.
             message.addRecipient(Message.RecipientType.TO,
                                      new InternetAddress(to));

             // Set Subject: header field
             message.setSubject("This is the Subject Line!");

             // Now set the actual message
             message.setText("This is actual message");

             // Send message
             Transport.send(message);
             System.out.println("Sent message successfully....");
          }catch (MessagingException mex) {
             mex.printStackTrace();
          } //catch(Message)
       }
    }

答案 3 :(得分:0)

(2019年7月)

我也有这个异常,对我来说固定的是:

(我正在使用多台服务器)

  1. 停止每个服务器(确保最后启动的服务器首先运行(检查哪个服务器取决于哪个服务器),基本上按DESC运行顺序停止服务器);

  2. 关闭Eclipse(我正在为Web开发人员使用Eclipse Java EE IDE。版本:Neon.3 Release(4.6.3)Build ID:20170314-1500);

(2.5。检查并杀死与项目相关的任何其他Java进程)

  1. 启动Eclipse并以corerct顺序和类等运行服务器。

就我而言,重新启动有助于我发送电子邮件。