发送邮件失败,连接超时错误

时间:2020-05-06 11:23:54

标签: java jakarta-mail

我具有使用Java Mail API的邮件发送功能。它在本地环境中工作。但是当我在aws实例上部署时,它会抛出错误。起初,我认为不允许从aws实例到邮件服务器的连接。但是从aws实例,它可以通过端口587远程传送到邮件服务器。我尝试也增加超时选项,并且仍然是相同的异常,即套接字超时异常。如果我的配置错误,请帮助我纠正。

这是我使用的依赖项。

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

这是邮件配置。

public static boolean sendMail() {

        Properties props = getSMTPProperties(HOST);
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(SENDER, PWD);
            }
        });

        try {
            Message message = createMessage(session, SENDER_EMAIL, RECEIVER, "testing", "testing");
            Transport.send(message);

            Store store = session.getStore("imap");
            store.connect(HOST, SENDER, PWD);
            Folder folder = store.getFolder("Sent Items");
            folder.open(Folder.READ_WRITE);
            folder.appendMessages(new Message[]{message});
            store.close();
        } catch (MessagingException e) {
            log.info("Cannot send" + e.getCause().getMessage());
        }
        return true;
    }

    private static Properties getSMTPProperties(String smtpHost) {
        Properties props = new Properties();

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.ssl.trust", smtpHost);
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", "587");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.debug", "true");
        props.put("mail.smtp.connectiontimeout", "20000");
        props.put("mail.smtp.timeout", "20000");

        props.put("mail.imap.starttls.enable", "true");
        props.put("mail.imap.ssl.trust", smtpHost);
        return props;
    }


    private static Message createMessage(Session session, String from, String[] recipients, String subject, String body)
            throws MessagingException {

        Message message = new MimeMessage(session);
        message.addHeader("Content-type", "text/HTML; charset=UTF-8");
        message.addHeader("format", "flowed");
        message.addHeader("Content-Transfer-Encoding", "8bit");

        message.setFrom(new InternetAddress(from));

        Address[] addresses = new Address[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addresses[i] = new InternetAddress(recipients[i]);
        }
        message.addRecipients(Message.RecipientType.TO, addresses);

        message.setSubject(subject);
        message.setContent(body, "text/html; charset=utf-8");

        return message;
    }

这是错误

2020-05-06 08:55:51,601 INFO  [stdout] (default task-58) DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
2020-05-06 08:55:51,601 INFO  [stdout] (default task-58) DEBUG: Tables of loaded providers
2020-05-06 08:55:51,601 INFO  [stdout] (default task-58) DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
2020-05-06 08:55:51,602 INFO  [stdout] (default task-58) DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
2020-05-06 08:55:51,602 INFO  [stdout] (default task-58) DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
2020-05-06 08:55:51,604 INFO  [stdout] (default task-58) DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
2020-05-06 08:55:51,608 INFO  [stdout] (default task-58) DEBUG SMTP: need username and password for authentication
2020-05-06 08:55:51,608 INFO  [stdout] (default task-58) DEBUG SMTP: protocolConnect returning false, host=mail.dummy.local, user=wildfly, password=<null>
2020-05-06 08:55:51,609 INFO  [stdout] (default task-58) DEBUG SMTP: useEhlo true, useAuth true
2020-05-06 08:55:51,609 INFO  [stdout] (default task-58) DEBUG SMTP: trying to connect to host "mail.dummy.local", port 587, isSSL false
2020-05-06 08:56:21,627 ERROR [stderr] (default task-58) com.sun.mail.util.MailConnectException: Couldn't connect to host, port: mail.dummy.local, 587; timeout 30000;
2020-05-06 08:56:21,627 ERROR [stderr] (default task-58)   nested exception is:
2020-05-06 08:56:21,627 ERROR [stderr] (default task-58)    java.net.SocketTimeoutException: connect timed out

1 个答案:

答案 0 :(得分:0)

我们遇到了类似的问题,并在降级为javax.mail:mail:1.4.5后使其正常工作。

为了给您更多的背景知识,我们有来自org.apache.commons:commons-email:1.3.2的javax.mail 1.4.5和来自javax:javaee-api:8.0的javax.mail 1.6。 >

我们有一个适用于1.4.5的模块,所以这是唯一的区别。

您可以尝试这个。