使用身份验证TLS配置SMTP服务器

时间:2017-11-09 13:25:34

标签: java spring-boot smtp javamail starttls

我正在配置我的微服务以在事件发生时发送电子邮件(在身份验证模式下,使用TLS),但我还没有获得所需的结果。 这是我上下文中的bean:

<bean id="microserviceMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.smtp.host:}" />
        <property name="port" value="${mail.smtp.port:587}"/>
        <property name="username" value="${mail.smtp.username:}"></property>
        <property name="password" value="${mail.smtp.password:}"></property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">${mail.smtp.auth:true}</prop>
                <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable:true}</prop>
                <prop key="mail.debug">${mail.smtp.debug:true}</prop>
                <prop key="mail.smtp.from">MicroService@localhost</prop>
            </props>
        </property>
    </bean>

我已经使用三个不同的SMTP服务器的三个不同帐户进行了测试,获得了三种不同的结果

使用 hotmail 配置:

mail.smtp.starttls.enable=true
mail.smtp.auth=true
mail.smtp.host=smtp.live.com
mail.smtp.port=587
mail.smtp.username=XXXX@hotmail.com
mail.smtp.password=XXXX
mail.smtp.protocol=smtp
mail.smtp.debug=true
mail.smtp.mail.from=MicroService@localhost **//Solved setting the same of "username" field**
mail.smtp.mail.subject=alert

我收到以下错误:

DEBUG SMTP: trying to connect to host "smtp.live.com", port 587, isSSL false
220 VI1PR04CA0076.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 9 Nov 2017 11:55:40 +0000
DEBUG SMTP: connected to host "smtp.live.com", port: 587

EHLO ES00201376.localhost.se
250-VI1PR04CA0076.outlook.office365.com Hello [195.235.15.200]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "157286400"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO ES00201376.localhost.se
250-VI1PR04CA0076.outlook.office365.com Hello [195.235.15.200]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-AUTH LOGIN XOAUTH2
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "157286400"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN XOAUTH2"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: protocolConnect login, host=smtp.live.com, user=xxxxx@hotmail.com, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<MicroService@localhost>
250 2.1.0 Sender OK
RCPT TO:<xxxxx@yahoo.com>
250 2.1.5 Recipient OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   xxxxx@yahoo.com
DATA
354 Start mail input; end with <CRLF>.<CRLF>
Date: Thu, 9 Nov 2017 12:55:42 +0100 (CET)
To: xxxxx@yahoo.com
Message-ID: <514152430.0.1510228542603@ES00201376.localhost.se>
Subject: Notification from MicroService
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 16.55847:.... [Hostname=AM4PR02MB3236.eurprd02.prod.outlook.com]
DEBUG SMTP: got response code 554, with response: 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 16.55847:....[Hostname=AM4PR02MB3236.eurprd02.prod.outlook.com]

RSET
250 2.0.0 Resetting
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 16.55847:... [Hostname=AM4PR02MB3236.eurprd02.prod.outlook.com]

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2267)

使用 Gmail 配置

mail.smtp.starttls.enable=true
mail.smtp.auth=true
mail.smtp.host=smtp.gmail.com
mail.smtp.port=587
mail.smtp.username=XXXX@gmail.com
mail.smtp.password=XXXX
mail.smtp.protocol=smtp
mail.smtp.debug=true
mail.smtp.mail.from=MicroService@localhost
mail.smtp.mail.subject=alert

我没有回复:

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false

最后,使用另一台服务器

mail.smtp.starttls.enable=true
mail.smtp.auth=true
mail.smtp.host=mail.cartograf.net
mail.smtp.port=587
mail.smtp.username=XXXX@cartograf.net
mail.smtp.password=XXXX
mail.smtp.protocol=smtp
mail.smtp.debug=true
mail.smtp.mail.from=MicroService@localhost
mail.smtp.mail.subject=alert

结果如下:

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.cartograf.net", port 587, isSSL false
220 homiemail-a21.g.dreamhost.com ESMTP
DEBUG SMTP: connected to host "mail.cartograf.net", port: 587

EHLO ES00201376.localhost.se
DEBUG SMTP: exception reading response, THROW: 
java.net.SocketTimeoutException: Read timed out

0 个答案:

没有答案