简体   繁体   English

Java Mail:在端口587上通过Gmail SMTP发送邮件时,操作超时

[英]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 8Mail 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.

相关问题 通过Java发送电子邮件-javax.mail.MessagingException:无法连接到SMTP主机:localhost,端口:587; - Sending emails through Java - javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587; com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587; 超时-1 - com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1 javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587; java.net.NoRouteToHostException:主机没有路由 - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; java.net.NoRouteToHostException: No route to host javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587 - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587 使用Java邮件通过Gmail SMTP发送邮件时出现问题 - Problem in sending mail with Gmail SMTP using Java mail 使用smtp gmail发送邮件 - using smtp gmail to sending mail javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587无法管理解决此问题 - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587 Can not manage to solve this problem Android使用SMTP发送邮件到Gmail - Android Sending mail to gmail using smtp 使用smtp在Java中发送邮件 - Sending an mail in java using smtp smtp.gmail.com,端口:587 失败 - smtp.gmail.com, port: 587 fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM