当前位置: 首页 > 知识库问答 >
问题:

使用spring boot和带有代理的JavaMailSender发送邮件

栾耀
2023-03-14

我正在尝试在Spring Boot中使用JavaMailSender发送电子邮件。我尝试了几乎所有的方法,但仍然得到这个错误。请建议我该怎么做才能使我的代码正常工作?谢谢!!!

这是我发送电子邮件的代码。

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文件

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

当我尝试运行代码时,遇到以下错误异常

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 

共有1个答案

湛宜春
2023-03-14

JavaMail FAQ提供了配置JavaMail以使用代理服务器的说明。如果您使用的是JavaMail 1.6.0或更高版本,请设置mail.smtp.proxy.hostmail.smtp.proxy.port属性。

 类似资料:
  • 我正在尝试使用Spring的在Spring(版本3.0.5. RELEASE)网络应用程序中发送一封html电子邮件。 电子邮件已发送,但不正确: 他们错过了主题 To字段显示为空或显示为“未公开的收件人:;” html正文显示为纯文本 我尝试了以下方法,但没有成功: (

  • 使用的技术: Spring Boot 1.4.2.Release,Spring 4.3.4.Release,Tymeleaf 2.1.5.Release,Tomcat Embeded 8.5.6、Maven 3、Java 8 我创建了这个服务来发送电子邮件

  • 问题内容: 如何使用Outlook 2010发送带有附件的电子邮件(本地文件或Intranet中的文件)? 似乎不起作用。 问题答案: 不,这根本不可能。协议中对此没有规定,如果可能的话,这将是一个巨大的安全漏洞。 发送文件但让客户端发送我能想到的电子邮件的最佳方法是: 让用户选择一个文件 将文件上传到服务器 上传后让服务器返回随机文件名 在消息正文中建立一个包含上载文件的URL 的链接

  • 所有数据以文本格式发送,而不是作为附件的PDF格式。 内容类型:多部分/备用;边界=myapp--_myapp_内容类型:文本/普通;charset=UTF-8Content-Transfer-Encoding:7bit测试消息--myapp--_myapp_Content-Type:Application/pdf;name=myPdf。PDF内容处置:附件;filename=myPdf。PDF内

  • 我正在将Mailgun集成到我的iOS应用程序中,并尝试发送带有附件的电子邮件。电子邮件已发送,但附件似乎已被忽略。有什么想法吗?代码如下。我使用的是AFNetworking 2,我没有使用本机的Mailgun Objective-C SDK,因为它似乎没有得到维护。 我可以使用curl发送附件,例如:

  • 有人能告诉我使用phpMailer发送带有附件的电子邮件的步骤吗?我已经准备好了发送电子邮件本身的所有代码,它工作正常,但我不明白如何发送附件。我有表单,如果我从POST上的文件输入中获得值,它只会给我文件名而不是完整路径。我认为为了添加附件,我需要获取文件的完整路径,对吗?我不必将文件存储在服务器上,只需通过电子邮件发送即可。