本文介绍了获取javax.mail.MessagingException和java.net.SocketException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用我的yahoo(smtp.mail.yahoo.com)SMTP的邮件发送到我的Gmail帐户。
I am trying to send a email using SMTP of my yahoo(smtp.mail.yahoo.com) account to my gmail account.
但是我得到以下异常。
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();
}
}
}
推荐答案
smtp.mail.yahoo.net
是指向多个不同邮件服务器的CNAME。雅虎恰好在今天的邮件服务器上遇到一些问题...
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.
现在尝试5次,我有3个失败和2个连接。
Trying 5 times just now I got 3 failures and 2 connects.
这篇关于获取javax.mail.MessagingException和java.net.SocketException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!