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

发布于 2024-11-01 22:27:42 字数 3452 浏览 1 评论 0原文

我正在尝试使用我的 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 [] = {"[email protected]"};
            sende.postMail(senderemailid,"Hi","Come to room","[email protected]");

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

I am trying to send a email using SMTP of my yahoo(smtp.mail.yahoo.com) account to my gmail account.

But am getting following exception.

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.*;

Code:

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 [] = {"[email protected]"};
            sende.postMail(senderemailid,"Hi","Come to room","[email protected]");

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

行雁书 2024-11-08 22:27:42

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 次连接。

smtp.mail.yahoo.net is a CNAME that points to a number of different mail servers. Yahoo happens to be having some problems with their mail servers today...

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.

Trying 5 times just now I got 3 failures and 2 connects.

纸伞微斜 2024-11-08 22:27:42

尝试设置 mail.transport.protocol 属性

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

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

Try to set the mail.transport.protocol property

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

You do need some other piece of code to authentication though. Yahoo requires you to authenticate yourself prior to using their smtp service - they don't allow anybody to use their smtp server for relaying

著墨染雨君画夕 2024-11-08 22:27:42

这是我的代码,以防万一:

 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 = "[email protected]";

          // Sender's email ID needs to be mentioned
          String from = "[email protected]";

          // 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)
       }
    }

This is my code just in case:

 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 = "[email protected]";

          // Sender's email ID needs to be mentioned
          String from = "[email protected]";

          // 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)
       }
    }
时光暖心i 2024-11-08 22:27:42

(2019 年 7 月)

我也遇到了这个异常,为我解决的是:(

我使用多个服务器)

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

  2. 关闭 Eclipse(我正在使用 Eclipse Java EE IDE for Web Developers。版本:Neon.3 Release (4.6.3) Build id: 20170314-1500);

(2.5. 检查并终止与您的项目相关的任何其他 java 进程)

  1. 启动 Eclipse 并以正确的顺序运行服务器和您的类等。

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

(2019 July)

I got this Exception too, what fixed for me is:

(I'm using multiple servers)

  1. Stopped each server (make sure the the server you start last, you run first (check which server depends on which), basically stop servers in DESC running order);

  2. Close Eclipse (I'm using Eclipse Java EE IDE for Web Developers. Version: Neon.3 Release (4.6.3) Build id: 20170314-1500);

(2.5. Check and kill any other java processes related to your project)

  1. Start Eclipse and run servers in corerct order and your classes, etc.

In my case, restart helped me to send email.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文