开发者社区> 问答> 正文

ecs使用java通过阿里企业邮发送邮件失败

/**
  * 发送邮件
  * @param someone 接收方邮箱
  * @param subject 标题
  * @param content 内容(可html格式)
  */
 private static int sendEmail(String someone, String subject, String content){
        Properties props = new Properties();
        props.setProperty("mail.host", " smtp.qiye.aliyun.com");
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.timeout", "10000");
        props.setProperty("mail.smtp.ssl.enable", "true");
        Authenticator authenticator = new Authenticator(){
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(USERNAME,PASSWORD);
            }
        };
        Session session = Session.getDefaultInstance(props, authenticator);
        session.setDebug(true);
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(FROM));
            message.setRecipients(RecipientType.TO,InternetAddress.parse(someone));//多个邮件以逗号隔开
            message.setSubject(subject);
            message.setContent(content,"text/html;charset=UTF-8");
            Transport.send(message);
            return 1;//发送成功
        } catch (AddressException e) {
         logger.error("AddressException邮箱地址错误:",e);
         return 2;//发送失败,地址错误
        } catch (MessagingException e) {
         logger.error("MessagingException消息发送失败:",e);
         return 3;//发送失败,消息发送失败
        } catch (Exception e) {
         logger.error("Exception消息发送失败:",e);
         return 3;//发送失败,未知错误
        }
    }
windows server下可发送邮件,普通centos下可发送邮件,但阿里云ECS无法发送邮件,安全组进出465端口已打开,防火墙关闭
报错信息如下
DEBUG: setDebug: JavaMail version 1.6.0
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qiye.aliyun.com", port 465, isSSL true
220 smtp.aliyun-inc.com MX AliMail Server(10.194.99.21)
DEBUG SMTP: connected to host "smtp.qiye.aliyun.com", port: 465

EHLO  手动隐藏
250-smtp.aliyun-inc.com
250-8BITMIME
250-AUTH=PLAIN LOGIN XALIOAUTH
250-AUTH PLAIN LOGIN XALIOAUTH
250-PIPELINING
250 DSN
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN XALIOAUTH"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XALIOAUTH"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:  手动隐藏
250 Mail Ok
RCPT TO:  手动隐藏
250 Rcpt Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:     手动隐藏
DATA
354 End data with <CR><LF>.<CR><LF>
QUIT
DEBUG SMTP: exception reading response: java.net.SocketTimeoutException: Read timed out
[ERROR] [2019-06-13 09:47:56] [com.musicIp.core.utils.send.SendEmailUtil:77] MessagingException消息发送失败:
javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2210)
        at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1222)
        at javax.mail.Transport.send0(Transport.java:257)
        at javax.mail.Transport.send(Transport.java:124)
        at com.musicIp.core.utils.send.SendEmailUtil.sendEmail(SendEmailUtil.java:71)
        at com.musicIp.core.utils.send.SendEmailUtil.SendEmailCode(SendEmailUtil.java:93)
        at com.musicIp.module.service.code.TSendCodeServiceImpl.sendCode(TSendCodeServiceImpl.java:69)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
        at com.sun.proxy.$Proxy24.sendCode(Unknown Source)
        at com.musicIp.module.controller.util.SendApi.registerSendCode(SendApi.java:109)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at com.musicIp.core.filter.DoReqFilter.doFilter(DoReqFilter.java:45)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
        at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:679)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:171)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
        at sun.security.ssl.InputRecord.read(InputRecord.java:503)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
        at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:933)
        at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
        at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
        at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2188)
        ... 69 more

windows下发送正常
DEBUG: setDebug: JavaMail version 1.6.0
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: protocolConnect returning false, host=smtp.qiye.aliyun.com, user=liuc, password=<null>
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qiye.aliyun.com", port 465, isSSL true
220 smtp.aliyun-inc.com MX AliMail Server(10.194.98.253)
DEBUG SMTP: connected to host "smtp.qiye.aliyun.com", port: 465

EHLO  手动隐藏
250-smtp.aliyun-inc.com
250-8BITMIME
250-AUTH=PLAIN LOGIN XALIOAUTH
250-AUTH PLAIN LOGIN XALIOAUTH
250-PIPELINING
250 DSN
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN XALIOAUTH"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XALIOAUTH"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: protocolConnect login, host=smtp.qiye.aliyun.com, user= 手动隐藏, 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: 手动隐藏
250 Mail Ok
RCPT TO: 手动隐藏
250 Rcpt Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:    手动隐藏
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Wed, 12 Jun 2019 17:26:27 +0800 (CST)
From:  手动隐藏
To:  手动隐藏
Message-ID: < 手动隐藏@smtp.qiye.aliyun.com>
Subject: =?UTF-8?B?5Zyw5Z2A5YiG6YWN55Sz6K+36YCa6L+H?=
MIME-Version: 1.0
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<style>body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,fieldse=
t,lengend,button,input,textarea,th,td{margin:0;padding:0;}body,button,input=
,select,textarea{font:12px / 1.5 'Microsoft YaHei','b8bf53',sans-serif;colo=
r:#666;}h2,h3,h4,h5,h6{font-family:Microsoft YaHei,sans-serif;}ul,ol{list-s=
tyle:none;}img{display:block;border:0;}input,button,textarea,select,optgrou=
p,option{font-family:inherit;font-size:inherit;font-style:inherit;font-weig=
ht:inherit;}input,button,textarea,select{font-size:100%;border-radius: 10px=
;border: solid 1px #d7e0e7;outline: none;}textarea{resize:none;}table{borde=
r-collapse:collapse;border-spacing:0;}a{color:#606060;text-decoration:none;=
}a:hover{color:#009B9D;}*{box-sizing:border-box;}.w{width: 1200px;margin: 0=
 auto;}.fl{float:left;}.fr{float:right;}.clear{clear:both;}.clearfix:after{=
content:'€B';display:block;height:0;clear:both;}.clearfix {*zoom:1;}.c=
ontainer{width: 1200px;height: 750px;margin: 0 auto;border: solid 1px #d7e5=
ee;}.container .title{padding-left: 25px;padding-top: 20px;padding-right: 2=
5px;border-bottom:solid 1px #d7e5ee;height: 60px;}.container .title .compan=
y{margin-left:20px;color: #606060;font-size:16px;font-weight: bold; }.conta=
iner .contain{text-align: center;}.container .contain .email{width: 100%;he=
ight: 470px;background: url('nulltemplates_com/images/email/email-contain.p=
ng') center;margin-top: 0px;padding-top: 50px;text-align: left;padding-left=
: 320px;font-size: 16px;}.container .contain .email .text1{margin-bottom: 2=
0px;}.container .contain .email .text2 .code{font-size: 37px;color: #29b77d=
;}.container .contain .email .text2{line-height: 45px;}.container .contain =
..email .text2 a{color: #49a1fd;}.container .contain .email .date{margin-rig=
ht:280px;margin-top: 25px; }.container .contain .tips{color: #49a1fd;width:=
 750px;margin: 0 auto;}.container .contain .tips{margin-top: 80px;}.contain=
er .contain .tips .tip div{width: 710px;line-height: 23px;}.fl{float:left;}=
..fr{float:right;}.clear{clear:both;}.clearfix:after{content:'';display:bloc=
k;height:0;clear:both;}.clearfix {*zoom:1;}.grween{color: #29b77d;}</style>=
<div class=3D'container'><div class=3D'title clearfixed'><div class=3D'fl c=
ompany'>=E6=98=A0=E5=85=89=E8=AE=A1=E5=88=92</div></div><div class=3D'conta=
in'><div class=3D'email'><div class=3D'text1'> =E5=B0=8A=E6=95=AC=E7=9A=84w=
ds=EF=BC=9A</div><div class=3D'text2'> =E7=B3=BB=E7=BB=9F=E5=B7=B2=E4=B8=BA=
=E6=82=A8=E5=88=86=E9=85=8D=E5=A5=BD=E6=98=A0=E5=85=89=E8=AE=A1=E5=88=92=E5=
=B9=B3=E5=8F=B0=E5=90=8E=E5=8F=B0=E7=AE=A1=E7=90=86=E8=B4=A6=E5=8F=B7=E4=BB=
=A5=E5=8F=8A=E5=9F=9F=E5=90=8D</div><div class=3D'text2'>  &#=
12288;=E7=94=A8=E6=88=B7=E5=90=8D=EF=BC=9A<span class=3D'grween'>sdgsa</spa=
n>    =E5=88=9D=E5=A7=8B=E5=AF=86=E7=A0=81=EF=BC=9A<span clas=
s=3D'grween'>123456</span>    =E5=9F=9F=E5=90=8D=EF=BC=9Agd.m=
usic-ip.com=E3=80=82</div><div class=3D'text2'>=E6=82=A8=E5=8F=AF=E7=9B=B4=
=E6=8E=A5=E8=AE=BF=E9=97=AE<a href=3D'gd.music-ip.com' target=3D'_blank'>gd=
..music-ip.com</a>=EF=BC=8C=E6=9F=A5=E7=9C=8B=E7=B3=BB=E7=BB=9F=E5=89=8D=E7=
=AB=AF=E7=95=8C=E9=9D=A2=E5=B1=95=E7=A4=BA=E3=80=82</div><div class=3D'text=
2'>=E4=B9=9F=E5=8F=AF=E4=BD=BF=E7=94=A8=E7=94=A8=E6=88=B7=E5=90=8D=E3=80=81=
=E5=88=9D=E5=A7=8B=E5=AF=86=E7=A0=81=E8=AE=BF=E9=97=AE<a href=3D'https://pa=
rtner.music-ip.com' target=3D'_blank'>https://partner.music-ip.com</a>=EF=
=BC=8C=E7=99=BB=E5=BD=95=E7=B3=BB=E7=BB=9F=E5=90=8E=E5=8F=B0=E3=80=82</div>=
<div class=3D'text2'>=E8=BF=99=E6=98=AF=E4=B8=80=E5=B0=81=E7=B3=BB=E7=BB=9F=
=E8=87=AA=E5=8A=A8=E5=8F=91=E5=87=BA=E7=9A=84=E9=82=AE=E4=BB=B6=EF=BC=8C=E8=
=AF=B7=E4=B8=8D=E8=A6=81=E7=9B=B4=E6=8E=A5=E5=9B=9E=E5=A4=8D=EF=BC=8C=E5=A6=
=82=E6=82=A8=E6=9C=89=E4=BB=BB=E4=BD=95=E7=96=91=E9=97=AE=EF=BC=8C=E8=AF=B7=
=E8=81=94=E7=B3=BB=EF=BC=9A</div><div class=3D'text2'>  =E9=
=82=AE=E7=AE=B1=EF=BC=9Amusicip@egongzheng.com</div><div class=3D'text2'>&#=
12288; =E7=94=B5=E8=AF=9D=EF=BC=9A400-878-0020</div><div class=3D'da=
te fr'><div>=E6=98=A0=E5=85=89=E8=AE=A1=E5=88=92</div><div>2019-06-12 17:26=
:27</div></div></div><div class=3D'tips clearfix'><div class=3D'fl tip'><di=
v>=E6=9C=AC=E9=82=AE=E4=BB=B6=E6=98=AF=E7=94=A8=E6=88=B7=E6=B3=A8=E5=86=8C=
=E6=98=A0=E5=85=89=E8=AE=A1=E5=88=92=E5=90=8E=E7=B3=BB=E7=BB=9F=E8=87=AA=E5=
=8A=A8=E5=8F=91=E5=87=BA=EF=BC=8C=E5=A6=82=E6=9E=9C=E4=BD=A0=E5=B9=B6=E6=9C=
=AA=E6=B3=A8=E5=86=8C=E6=98=A0=E5=85=89=E8=AE=A1=E5=88=92=EF=BC=8C=E5=8F=AF=
=E8=83=BD=E6=98=AF=E5=9B=A0=E4=B8=BA=E5=85=B6=E4=BB=96=E7=94=A8=E6=88=B7=E8=
=AF=AF=E8=BE=93=E5=85=A5=E4=BA=86=E4=BD=A0=E7=9A=84=E9=82=AE=E7=AE=B1=E5=9C=
=B0=E5=9D=80=E8=80=8C=E4=BD=BF=E4=BD=A0=E6=94=B6=E5=88=B0=E4=BA=86=E8=BF=99=
=E5=B0=81=E9=82=AE</div><div>=E4=BB=B6=E3=80=82=E5=A6=82=E8=AF=AF=E6=94=B6=
=E6=9C=AC=E9=82=AE=E4=BB=B6=EF=BC=8C=E8=AF=B7=E7=AB=8B=E5=8D=B3=E5=88=A0=E9=
=99=A4=EF=BC=8C=E4=B8=8D=E8=A6=81=E8=BF=9B=E8=A1=8C=E6=93=8D=E4=BD=9C</div>=
</div></div></div></div>
.
250 Data Ok: queued as freedom
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 Bye

展开
收起
游客b5m3wineitomu 2019-07-10 18:05:10 2130 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载