EmailSender异常读取响应

时间:2019-07-01 03:21:01

标签: java spring-boot

我相信我已经从https://vandeneyndefilip.nu/spring-boot/spring-boot-sending-email/网站正确地遵循了指南,但是我得到了阅读异常的回复。

我试图在Internet上找到解决该问题的方法,我所能获得的只是将端口从465更改为587。不幸的是,我确实从一开始就使用了587端口。

  1. 这是我的application.properties
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=myemail@gmail.com
spring.mail.password=mypassword
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
  1. 这是我的实现方式
            String jsonData = user.toJson();

            System.out.println(jsonData);
            byte[] arrayBlob = blobTemplate.getBytes(1, (int) blobTemplate.length());
            String data = new String(arrayBlob);

            String newData = MustacheProcessor.mustacheProcess(data, jsonData);

            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(user.getEmail());
            message.setSubject(template.getSubject());
            message.setText(newData);

            emailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

我试图查看这条线上的数据传输,一切都很好

            message.setTo(user.getEmail());
            message.setSubject(template.getSubject());
            message.setText(newData);

我已经阅读了踪迹,发送电子邮件时似乎出现了问题

emailSender.send(message);

  1. 这是痕迹的一半
  nested exception is:
    java.net.SocketException: Connection reset. Failed messages: javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketException: Connection reset; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketException: Connection reset
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2460)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2187)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
    at javax.mail.Service.connect(Service.java:366)
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:517)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:436)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:322)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:311)
    at com.sender.tugaskp2.tugaskp2.service.impl.EmailServiceImp.sendSinglePayslip(EmailServiceImp.java:55)
    at com.sender.tugaskp2.tugaskp2.controller.EmailController.sendSinglePayslip(EmailController.java:21)
    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:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)

1 个答案:

答案 0 :(得分:0)

我已解决此问题,方法是更换我使用的网络并激活我的电子邮件的安全性较低的应用程序访问权限。我不确定这是否是解决此问题的最合适方法。

我从此链接获得了答案 https://www.mkyong.com/spring-boot/spring-boot-how-to-send-email-via-smtp/

祝你有美好的一天