我正在尝试编写一个简单的Java程序,使用JavaMail API从我的hotmail帐户发送电子邮件。这是我的代码:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class HotMailSend {
public static void main(String args[])
{
final String username = HOTMAIL.username;
final String password = HOTMAIL.password;
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty("mail.smtp.host", "smtp.live.com");
props.setProperty("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(HOTMAIL.username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(GMAIL.username));
message.setSubject("Testing Subject");
message.setText("Hey Buddy..!!!,"
+ "nn No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
这是我得到的错误:
线程"main"中的异常 java.lang.RuntimeException: javax.mail.MessagingException: 无法向 SMTP 主机发送命令; 嵌套异常为: java.net.SocketException:连接被远程主机关闭 at HotMailSend.main(HotMailSend.java:45) 原因:javax.mail.Messaging异常:无法向SMTP主机发送命令; 嵌套异常为: java.net.SocketException:连接被远程主机关闭 at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2163) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2150) at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1220) at javax.mail.Transport.send0(Transport.java:197) at javax.mail.Transport.send(Transport.java:124) at HotMailSend.main(HotMailSend.java:40) 原因:java.net.SocketException:远程主机关闭连接 at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1307) at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:43) at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:114) at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65) at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2161) ...5 更多
我以前在Hotmail中使用过SMTP/Javamail,所以它绝对有效。您是否在短时间内发送了很多电子邮件?(我认为当您这样做作为垃圾邮件预防措施时,他们可能会阻止您的邮件或将其发送到垃圾邮件文件夹。
检查您当前使用的电子邮件是否从 Microsoft 到达了某些邮件。
我遇到了这个问题,当我寻找它时,我有一封电子邮件,主题如下:验证您的帐户以使用 Outlook.com 做更多事情。
他们要求我确认我真的是人类,这样我就可以继续发送自动消息。他们给我发了一条消息到我的手机,我设法继续用Java Mail发送电子邮件。