[英]Java Mail: Operation timeout while sending mail over Gmail SMTP on port 587
Trying to write a java mail API program but getting following error: 尝试编写Java Mail API程序,但出现以下错误:
Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
java.net.ConnectException: Operation timed out
Here is the java code I have written: 这是我编写的Java代码:
String HOST = "smtp.gmail.com";
String PORT = "587";
String USERNAME = "myname@gmail.com";
String PASSWORD = "myPassword";
Properties properties = System.getProperties();
properties.put("mail.smtp.host", HOST);
properties.put("mail.smtp.port", PORT);
properties.put("mail.smtp.user", USERNAME);
properties.put("mail.smtp.password", PASSWORD);
Session session = Session.getDefaultInstance(properties);
// 2) compose message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(request.getSenderEmailId()));
InternetAddress[] addressTo = new InternetAddress[request.getRecipients().size()];
for (int i = 0; i < request.getRecipients().size(); i++) {
addressTo[i] = new InternetAddress(request.getRecipients().get(i));
}
message.addRecipients(Message.RecipientType.TO, addressTo);
// 3) create MimeBodyPart object and set your message text
BodyPart messageBodyPart1 = new MimeBodyPart();
// 4) create new MimeBodyPart object and set DataHandler object to
// this object
Multipart multipart = new MimeMultipart();
messageBodyPart1.setContent(request.getContent(),
EmailConstants.TYPE_HTML);
message.setSubject(request.getSubject());
multipart.addBodyPart(messageBodyPart1);
// 6) set the multiplart object to the message object
message.setContent(multipart);
// 7) send message
Transport.send(message);
} catch (MessagingException ex) {
ex.printStackTrace();
}
Now I have tried all the variations I could find out here in StackOverflow but nothing helped. 现在,我已经尝试了所有可以在StackOverflow中找到的变体,但是没有任何帮助。 Tried MkYong.com Java Mail Example but same error. 尝试了MkYong.com Java Mail示例,但存在相同的错误。
Also changed the PORT
to 465
and added these statements in properties but still the same issue. PORT
更改为465
并在属性中添加了这些语句,但仍然是相同的问题。
properties.put("mail.smtp.socketFactory.port", PORT);
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Interestingly, I am using the same code to send emails from my corp-account
and it works just fine. 有趣的是,我使用相同的代码从corp-account
发送电子邮件,效果很好。 Just provided corp host name and port as 25 and it is working. 刚提供的公司主机名和端口为25,它正在工作。
Am trying to deploy this to AWS where corp account does not work so need to migrate to Gmail. 我正在尝试将其部署到公司帐户不起作用的AWS上,因此需要迁移到Gmail。
Am using Java 8
and Mail API
is: 我正在使用Java 8
而Mail API
是:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
Am trying to ping at smtp.gmail.com
which is connecting but when I do telnet smtp.gmail.com 587
it keeps on trying but doesn't connect. 我试图在正在连接的smtp.gmail.com
上执行ping操作,但是当我执行telnet smtp.gmail.com 587
它会继续尝试但无法连接。
Any help would be appreciated. 任何帮助,将不胜感激。
It works for me: 这个对我有用:
public class AppConfig {
@Bean
public JavaMailSender getJavaMailSender() {
log.info("Init mail sender bean");
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
mailSender.setUsername("Username");
mailSender.setPassword("Password");
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
return mailSender;
}
}
public class EmailSender {
..
@Autowired
public JavaMailSender emailSender;
public void send(String from, String to, String subject, String body, boolean html) {
try {
MimeMessage message = createMimeMessage(subject);
getMimeMessageHelper(from, to, subject, body, html, message, false);
emailSender.send(message);
} catch (Exception ex) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.