简体   繁体   English

使用带有代理的 Spring Boot 和 JavaMailSender 发送邮件

[英]Sending Mail Using spring boot and JavaMailSender with proxy

I am trying to send email using JavaMailSender in spring boot.我正在尝试在 Spring Boot 中使用 JavaMailSender 发送电子邮件。 I have tried almost everything but still getting this error.我几乎尝试了所有方法,但仍然收到此错误。 Please suggest me what should I do In order to get my code working?请建议我应该怎么做才能使我的代码正常工作? Thanks!!!谢谢!!!

here is my code for sending an email.这是我发送电子邮件的代码。

public void prepareAndSend(String recipient, String message) {
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    mailMessage.setFrom("xxx@gmail.com");
    mailMessage.setTo(recipient);
    mailMessage.setSubject("test");
    mailMessage.setText(message);
    javaMailSender.send(mailMessage);
}

Application.properties file Application.properties 文件

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=*******
spring.mail.password= *******

#mail properties
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

-Dhttp.proxyHost=192.168.15.1
-Dhttp.proxyPort=2120 
-Dhttps.proxyHost=192.168.15.1
-Dhttps.proxyPort=2120
-Dhttps.proxySet=true 
-Dhttp.proxySet=true

server.use-forwarded-headers=true

I am getting the following error Exception when I try to run the code当我尝试运行代码时出现以下错误异常

2018-12-14 11:38:03.832 ERROR 2540 --- [nio-9090-exec-1] 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: Mail server connection failed; nested exception is javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
  nested exception is:
    java.net.UnknownHostException: smtp.gmail.com. Failed messages: javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
  nested exception is:
    java.net.UnknownHostException: smtp.gmail.com; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
  nested exception is:
    java.net.UnknownHostException: smtp.gmail.com] with root cause

java.net.UnknownHostException: smtp.gmail.com
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_172]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_172]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_172]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_172]
    at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_172]
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319) ~[mail-1.4.5.jar:1.4.5]
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233) ~[mail-1.4.5.jar:1.4.5]
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938) ~[mail-1.4.5.jar:1.4.5]
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642) ~[mail-1.4.5.jar:1.4.5]
    at javax.mail.Service.connect(Service.java:295) ~[mail-1.4.5.jar:1.4.5]
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:501) ~[spring-context-support-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:421) ~[spring-context-support-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307) ~[spring-context-support-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296) ~[spring-context-support-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at com.bharti.firstspringbootproject.service.UserService.prepareAndSend(UserService.java:46) ~[classes/:na]
    at com.bharti.firstspringbootproject.controller.AuthenticateController.authenticateUser(AuthenticateController.java:51) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_172]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_172]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_172]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_172]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at com.bharti.firstspringbootproject.security.JwtAuthenticationFilter.doFilterInternal(JwtAuthenticationFilter.java:49) ~[classes/:na]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) ~[spring-security-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at 

The JavaMail FAQ has instructions for configuring JavaMail to use a proxy server . JavaMail FAQ 有关于配置 JavaMail 以使用代理服务器的说明 If you're using JavaMail 1.6.0 or later, set the mail.smtp.proxy.host and mail.smtp.proxy.port properties.如果您使用 JavaMail 1.6.0 或更高版本,请设置mail.smtp.proxy.hostmail.smtp.proxy.port属性。

To clarify at least one point concerning the usage of https.proxySet=true and http.proxySet=true. 为了阐明有关https.proxySet = true和http.proxySet = true的用法的至少一点。

JDK-4632974 proxySet=false is ignored for HttpURLConnection.getOutputStream JDK-4632974 proxySet = false对于HttpURLConnection.getOutputStream被忽略

"The use of property http.proxySet was eliminated in favor of simply testing for the presence of the http.proxyHost property. This property existed up to JDK1.0.2 but was removed for JDK1.1 in 1996." “取消了对属性http.proxySet的使用,转而仅测试了http.proxyHost属性的存在。此属性在JDK1.0.2之前一直存在,但在1996年为JDK1.1删除。”

In other words, do not use proxySet as it serves no purpose. 换句话说,不要使用proxySet,因为它没有用。 It is non-existing Java system property nowadays. 如今,它是不存在的Java系统属性。

I already wrote the mail server using spring boot.我已经使用 spring boot 编写了邮件服务器。 but I didn't get this kind of issue.但我没有遇到这种问题。 This seems network(proxy) issue.这似乎是网络(代理)问题。 Please try without proxy.请尝试不使用代理。

Find below sample code for your reference.找到以下示例代码供您参考。

@Autowired
private JavaMailSender javaMailSender;

I used MineMessage as below.我使用 MineMessage 如下。

   MimeMessage message = javaMailSender.createMimeMessage();
   MimeMessageHelper helper = new MimeMessageHelper(message);
   helper.setFrom(systemEmail);
   helper.setTo(mailBacklog.getToAddress());
     if(mailBacklog.getSubject() != null) {
          helper.setSubject(mailBacklog.getSubject());
     }
   helper.setText(mailBacklog.getMessage());
   javaMailSender.send(message);

sample configuration (.yml)示例配置 (.yml)

spring:  
  mail:
    host: smtp.gmail.com
    port: 587
    username: xxxxxxxx@gmail.com
    password: xxxxxx
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM