我试图通过gmail smtp
服务器通过java代码发送邮件,但面对java.net.SocketException: Connection Reset
以下是发送邮件的代码:-`
Properties props = new Properties();
props.put("mail.smtp.host",host); //host is smtp.gmail.com
if(host.equalsIgnoreCase("smtp.gmail.com"))
{
props.put("mail.smtp.socketFactory.port",port);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port",port);//port is 465
}
else
{
props.put("mail.smtp.auth", "true");
}
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(uname,pwd);
}
});
try {
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(uname));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(toMail));
message.setSubject(NewFolderName);
System.out.println("Composing message body.");
StringBuilder htmlStreamBuilder = new StringBuilder();
// we create some html string here.....
Multipart multipart = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
MimeBodyPart attachmentPart= new MimeBodyPart();
DataSource source = new FileDataSource(filename);
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName(KMAConstants.TESTNG_ATTACHMENT_FILE);
htmlPart.setContent(htmlStream, "text/html; charset=utf-8");//we give some html stream here ....
multipart.addBodyPart(htmlPart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException e)
{
System.out.println("Issue in message sending, MessagingExceptionn raised.");
e.printStackTrace();
throw e;
}
}
`
它会抛出以下错误: - `
DEBUG:getProvider()返回javax.mail.Provider [TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP:useEhlo true,useAuth true DEBUG SMTP:useEhlo true,useAuth true DEBUG SMTP:试图连接到主机" smtp.gmail.com",端口465,isSSL false DEBUG SMTP:异常阅读响应,THROW: java.net.SocketException:连接重置 在java.net.SocketInputStream.read(SocketInputStream.java:209) 在java.net.SocketInputStream.read(SocketInputStream.java:141) at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124) 在java.io.BufferedInputStream.fill(BufferedInputStream.java:246) 在java.io.BufferedInputStream.read(BufferedInputStream.java:265) at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89) at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2184) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1939) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) 在javax.mail.Service.connect(Service.java:367) 在javax.mail.Service.connect(Service.java:226) 在javax.mail.Service.connect(Service.java:175) 在javax.mail.Transport.send0(Transport.java:253) 在javax.mail.Transport.send(Transport.java:124) 在com.rsi.kma.common.utils.KmaUtil.sendMailNow(KmaUtil.java:1907) 在com.rsi.kma.ui.testconfigureui.FileProcessingTask.call(FileProcessingTask.java:401) 在com.rsi.kma.ui.testconfigureui.FileProcessingTask.call(FileProcessingTask.java:1) 在javafx.concurrent.Task $ TaskCallable.call(Task.java:1423) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 在java.lang.Thread.run(Thread.java:745) 邮件发送问题,MessagingExceptionn引发。
`
这里需要注意的一点是,此代码适用于某些计算机。
请帮帮我, 提前谢谢。
答案 0 :(得分:2)
网络政策阻止了对SMTP服务器的请求。我联系了我的网络管理员,他给了我所需的权限。现在一切正常。