2017-05-19 48 views
1

我試圖在我的web應用程序中添加JavaMail。javax.mail.MessagingException:[EOF](Spring MVC)

我的背景:

<context:property-placeholder location="classpath:mail/gmail.com.properties" ignore-unresolvable="true" /> 

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
     <property name="javaMailProperties"> 
      <props> 
       <prop key="mail.smtps.auth">${mail.smtps.auth}</prop> 
      </props> 
     </property> 
     <property name="host" value="${mail.host}" /> 
     <property name="port" value="${mail.port}" /> 
     <property name="username" value="${mail.username}" /> 
     <property name="password" value="${mail.password}" /> 
     <property name="protocol" value="${mail.protocol}" /> 
    </bean> 

    <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage"> 
     <property name="from" value="${mail.from}" /> 
     <property name="subject" value="Mail subject" /> 
    </bean> 

我的屬性:

mail.host=smtp.gmail.com 
mail.port=465 
[email protected] 
mail.password=my 
[email protected] 
mail.protocol=smtps 
mail.smtps.auth=true 

我的服務:

@Service 
public class MailServiceImpl implements MailService { 

    @Autowired 
    private MailSender mailSender; 

    @Override 
    public void sendMail(String to, String subject, String body) { 
     SimpleMailMessage mailMessage = new SimpleMailMessage(); 
     mailMessage.setTo(to); 
     mailMessage.setText(body); 
     try { 
      mailSender.send(mailMessage); 
     } catch (MailException mailException) { 
      mailException.printStackTrace(); 
     } 
    } 
} 

日誌:

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: [EOF]. Failed messages: javax.mail.MessagingException: [EOF]; message exception details (1) are: 
Failed message 1: 
javax.mail.MessagingException: [EOF] 
    at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363) 
    at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:836) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375) 
    at javax.mail.Service.connect(Service.java:275) 
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389) 
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:306) 
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296) 
    at by.netcracker.artemyev.service.impl.MailServiceImpl.sendMail(MailServiceImpl.java:25) 
    at by.netcracker.artemyev.service.impl.UserServiceImpl.addUser(UserServiceImpl.java:67) 
    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.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) 
    at com.sun.proxy.$Proxy39.addUser(Unknown Source) 
    at by.netcracker.artemyev.web.UserController.registrationUser(UserController.java:59) 
    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:963) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) 
    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:230) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    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:475) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) 
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495) 
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1354) 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 

在同一時間,如果我想的是:

try (GenericXmlApplicationContext context = new GenericXmlApplicationContext()) { 
      context.load("classpath:applicationContext.xml"); 
      context.refresh(); 
      JavaMailSender mailSender = context.getBean("mailSender", JavaMailSender.class); 
      SimpleMailMessage templateMessage = context.getBean("templateMessage", SimpleMailMessage.class); 
      SimpleMailMessage mailMessage = new SimpleMailMessage(templateMessage); 
      mailMessage.setTo("[email protected]"); 
      mailMessage.setText("Hello"); 
      try { 
       mailSender.send(mailMessage); 
       System.out.println("Mail sended"); 
      } catch (MailException mailException) { 
       System.out.println("Mail send failed."); 
       mailException.printStackTrace(); 
      } 
     } 

我沒有一個問題:

*失敗的消息1:

javax.mail.MessagingException的:[EOF] *

爲什麼我在我的第一個例子中的問題?這個怎麼解決?

+0

這似乎是你的前一個問題的副本。如果不是,請解釋有什麼不同。 –

+0

@BillShannon這是一個相同的問題。如果你知道答案,請解釋如何解決問題 – studentST

+0

壞,壞,壞。不要發佈重複的問題。它不會讓人們更可能幫助你。回到原來的問題,閱讀我的評論,並做我說的,這樣你就可以提供足夠的信息來幫助你。 –

回答

0

嘗試設置下面的屬性

"mail.smtp.starttls.enable", "true" 

下面是它的外觀

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
     <property name="host" value="smtp.gmail.com" /> 
     <property name="port" value="587" /> 
     <property name="username" value="XX"/> 
     <property name="password" value="XX"/> 

     <property name="javaMailProperties"> 
      <props> 
       <prop key="mail.transport.protocol">smtp</prop> 
       <prop key="mail.smtp.auth">true</prop> 
       <prop key="mail.smtp.starttls.enable">true</prop> 
       <prop key="mail.debug">true</prop> 
      </props> 
     </property> 
    </bean> 
+0

不,它沒有幫助,得到相同的錯誤 – studentST

+0

@studentST嘗試將端口更改爲587一次。正如你使用Gmail的SMTP服務器。 –