我正在使用JavaMail从使用Gmail的服务器发送电子邮件。但我收到以下错误。可能是什么原因呢?我可以通过更改代码来解决此问题,还是需要请组织中的网络管理员进行任何更改?
com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,993;超时-1;嵌套例外是:java.net.UnknownHostException:smtp.gmail.com
下面是代码
Properties properties = System.getProperties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.auth", "true");
props.put("mail.protocol.proxy.host","myserver");
props.put("mail.protocol.proxy.port","myserverport");
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sendergmail", "password");
}
});
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("sendergmail"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("receiveremail"));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
您需要为特定协议设置代理属性您正在使用,例如,
props.put("mail.smtp.proxy.host","myserver");
props.put("mail.smtp.proxy.port","myserverport");