尝试编写java邮件API程序,但收到以下错误:
Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
java.net.ConnectException: Operation timed out
这是我写的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();
}
现在我已经尝试了StackOverflow中可以找到的所有变体,但没有任何帮助。尝试MkYong.com Java Mail Example,但同样的错误。
同时将PORT
更改为465
并在属性中添加了这些语句,但问题仍然存在。
properties.put("mail.smtp.socketFactory.port", PORT);
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
有趣的是,我使用相同的代码从我的corp-account
发送电子邮件,它运行正常。刚提供的公司主机名和端口为25,它正在运行。
我尝试将此部署到AWS账户不起作用的AWS,因此需要迁移到Gmail。
使用Java 8
和Mail API
是:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
我试图在正在连接的smtp.gmail.com
上ping,但当我 telnet smtp.gmail.com 587
时,它会继续尝试但是没有连接。
任何帮助都将不胜感激。
答案 0 :(得分:0)
它对我有用:
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) {
}
}