邮件失败:javax.mail.MessagingException:无法确定本地电子邮件地址

时间:2016-01-09 14:09:07

标签: java email spring-boot mime-message

您好,我在尝试使用spring boot发送SMTP电子邮件后,继续学习这个错误。 Failed messages: javax.mail.MessagingException: can't determine local email address

application.properties

#email configuration
spring.mail.host = smtp.gmail.com
spring.mail.password = ************
spring.mail.username = ******@gmail.com
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.ssl.enable = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false

实施文件:

@Service("mailServiceImpl")
public class MailServiceImpl {

    @Autowired
    private JavaMailSender javaMailSender;

    public void send(String to, String subject, String body) throws MessagingException {

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setText(body, true);

        javaMailSender.send(mimeMessage);
    }
}

最后在名为HomeController.java的控制器类中:

@RequestMapping (value="guest/reservation", method = RequestMethod.POST)
    @ResponseBody
    public Reservation createReservation(@RequestBody Reservation reservation) throws MessagingException {
        Reservation reservation1 =reservationservice.create(reservation);
        billingservice.create(reservation1);
        String body = "Welcome";
        //mailServiceImpl.send(reservation1.getGuest().getEmail(), "", body);
        System.out.println(reservation1.getGuest().getEmail());
        sendMail(reservation1.getGuest().getEmail(), "Reservation", body);
        return reservation1;
    }

    private void sendMail(String to, String subject, String body) throws MessagingException {
        mailServiceImpl.send(to, subject,  body);
    }

来自控制台的错误:

2016-01-09 16:22:28.989 ERROR 17396 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: can't determine local email address; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: can't determine local email address] with root cause

org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: can't determine local email address
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:459)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
    at edu.sjsu.cmpe275Project.service.impl.MailServiceImpl.send(MailServiceImpl.java:29)
    at edu.sjsu.cmpe275Project.controller.HomeController.sendMail(HomeController.java:69)
    at edu.sjsu.cmpe275Project.controller.HomeController.createReservation(HomeController.java:64)
    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:483)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:299)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:102)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:76)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    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)

请问我做错了什么?

1 个答案:

答案 0 :(得分:4)

消息显示缺少本地电子邮件地址。这意味着发件人的电子邮件地址,或者换句话说,消费者可以用来回复邮件的地址。此地址是您与#wctopdropcont{ width:100%; height:45px; display:block; padding: 5.5px 0 0 0; z-index:100; top:-2px; left: 0px; position: fixed; background:#f5f5f5; border-bottom: 1px solid #f0f0f0; } #wctopdropnav{ float: left; width:97%; height:7px; display:block; padding:0px; } #wctopdropnav li{ float:left; list-style:none; line-height:13px; padding: 10px 6.5px 6.5px 6.5px; background:#f5f5f5; } #wctopdropnav li a, #wctopdropnav li a:link{ color:#494949; float: left; display:block; text-transform: uppercase; font-size: 10.5px!important; font-family: karla, arial!important; padding: 5px; text-decoration:none; font-weight: normal!important; letter-spacing : 0.09em; } #wctopdropnav li:first-child a { font-weight: bold!important; margin-left: 20px; } #wctopdropnav li a:hover, #wctopdropnav li a:active, #wctopdropnav .current_page_item a { color: #a6a6a6; font-weight: normal; padding: 5px; background: #f5f5f5; } #wctopdropnav li li a, #wctopdropnav li li a:link, #wctopdropnav li li a:visited{ font-size: 10.5px; background:#f5f5f5; color: #494949; width: 90px; margin: 0px; padding: 0; line-height: 15px; position: relative; } #wctopdropnav li li a:hover, #wctopdropnav li li a:active { color: #a6a6a6; background: #f5f5f5; filter: #f5f5f5; } #wctopdropnav li:hover, #wctopdropnav li.sfhover{ position:static } #socialmediabuttons { display: block; float: right; position: relative; margin: 0.9% -1% 0 0; } #socialmediabuttons a { padding: 0 0 0 18px; } #socialmediabuttons a:hover { opacity: 0.4; filter: alpha(opacity=40); } </style> <div id='wctopdropcont'> <div id='wctopdropnav'> <li><a href='http://www.blankesque.com'>Home</a></li> <li><a href='http://www.blankesque.com/search/label/Advice'>Advice</a></li> <li><a href='http://www.blankesque.com/search/label/Beauty'>Beauty</a></li> <li><a href='http://www.blankesque.com/search/label/Fashion'>Fashion</a></li> <li><a href='http://www.blankesque.com/search/label/Lifestyle'>Lifestyle</a></li> <li><a href='http://www.blankesque.com/search/label/Skin &amp; Hair'>Skin &amp; Hair</a></li> <div id='socialmediabuttons'> <a href='https://www.pinterest.com/blankesque' target='_blank'><img height='20px' src='http://i1379.photobucket.com/albums/ah140/mynamesiram/Mobile%20Uploads/91F98FB1-242C-428E-A472-50F7D511C38E_zpsaiuhz6yb.gif' width='20px'/> </a> <a href='https://www.twitter.com/' target='_blank'><img height='20px' src='http://i1379.photobucket.com/albums/ah140/mynamesiram/Mobile%20Uploads/923FF7F8-5AA7-4676-935F-2CB5FF465122_zpsmctqg100.gif' width='20px'/></a> <a href='http://www.bloglovin.com/blogs/blankesque-14431777' target='_blank'><img height='20px' src='http://i1379.photobucket.com/albums/ah140/mynamesiram/Mobile%20Uploads/7CC1080E-1911-4D0B-B99F-55109C044D54_zps2ky5dfgt.gif' width='20px'/></a> <a href='https://instagram.com/' target='_blank'><img height='20px' src='http://i1379.photobucket.com/albums/ah140/mynamesiram/Mobile%20Uploads/C6567CDB-FB01-4F2D-A2FD-D0D875A30B80_zps5mgdqong.gif' width='20px'/></a> </div> </div></div> 一起使用的MimeMessage的属性,您可以使用它来设置地址

32.3 Using the JavaMail MimeMessageHelper

  

在处理JavaMail消息时非常方便的类是MimeMessageHelper类,它使您不必使用详细的JavaMail API。使用org.springframework.mail.javamail.MimeMessageHelper可以很容易地创建MimeMessageHelper

MimeMessage

这与文档页面上的代码相同,但添加了// of course you would use DI in any real-world cases JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost("mail.host.com"); MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message); helper.setFrom("your@email.address"); helper.setTo("test@host.com"); helper.setText("Thank you!"); sender.send(message); ,仅在此示例中缺失。