我正在使用端口25和主机smtp.gmail.com。我已经将starttls属性设置为true。但我收到错误,无法与主机连接,端口smtp.gmail.com 25,超时-1
防火墙和网络中均允许使用端口。
根据https://javaee.github.io/javamail/FAQ#proxy邮件可以使用来自Javax版本1.6.0的HTTP代理发送,我使用的是1.6.2
到目前为止,我的代码是
String to = "abc@corporate.com";
String subject = "Test subject";
String msg ="email text";
final String from ="abc@corporate.com";
final String password ="password";
Properties props = new Properties();
props.put("mail.smtp.proxy.host", "xx.xxx.xx.xx");
props.put("mail.smtp.proxy.port", "xxxx");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.starttls.enable","true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
Transport transport = session.getTransport();
InternetAddress addressFrom = new InternetAddress(from);
MimeMessage message = new MimeMessage(session);
message.setFrom(addressFrom);
message.setSubject(subject);
message.setContent(msg, "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
transport.connect();
Transport.send(message);
transport.close();
}