问题
Got the below exception while send mails in a loop of 120. First 80/85 mails are success but the remaining got exceptions.
javax.mail.MessagingException: Can't send command to SMTP host; nested exception is: java.net.SocketException: Connection closed by remote host at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1564) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1551) at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1489) at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1054) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:634)
Below is my code for sending mails. From, To, Subject, Body etc. are same for all 120 mails.
try {
Transport tr = session.getTransport("smtp");
tr.connect(this.SMTPServer, this.UserName, this.Password);
System.out.println("Successfully connected to the mail server.");
tr.sendMessage(message, message.getAllRecipients());
} catch (Exception ex) {
ex.printStackTrace();
}
来源:https://stackoverflow.com/questions/10135927/sending-120-mails-by-javamail-using-gmail-account-but-got-exception-after-80-90