I am trying to find the correct properties to use to connect to the Gmail SMTP sever using the JavaMailSenderImpl class.
我正在尝试使用JavaMailSenderImpl类找到用于连接到Gmail SMTP服务器的正确属性。
Let me first say that I have tried the approach found here. This worked fine. But when I tried the configuration below that post with the exact same authentication information I received a javax.mail.AuthenticationFailedException.
我先说我尝试过这里的方法。这很好用。但当我尝试使用完全相同的身份验证信息在该帖子下面的配置时,我收到了javax.mail.AuthenticationFailedException。
My currently configuration looks like this.
我目前的配置如下所示。
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="username" value="XXX@gmail.com" />
<property name="password" value="XXX" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.host">smtp.gmail.com</prop>
<prop key="mail.smtp.port">587</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
Why am I still getting this javax.mail.AuthenticationFailedException if I know that my credentials are correct.
如果我知道我的凭据是正确的,为什么我仍然得到这个javax.mail.AuthenticationFailedException。
Update
Here is my updated code based on the answers below. I am still receiving the same exception.
这是我基于以下答案的更新代码。我仍然收到同样的例外。
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="username" value="XXX@gmail.com" />
<property name="password" value="XXX" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.from">XXX@gmail.com</prop>
<prop key="mail.smtp.user">XXX@gmail.com</prop>
<prop key="mail.smtp.password">XXX</prop>
<prop key="mail.smtp.host">smtp.gmail.com</prop>
<prop key="mail.smtp.port">587</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
12 个解决方案
#1
29
This worked for me:
这对我有用:
<property name="host"><value>smtp.gmail.com</value></property>
<property name="port"><value>587</value></property>
<property name="protocol"><value>smtp</value></property>
<property name="username"><value>${mail.username}</value></property>
<property name="password"><value>${mail.password}</value></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.quitwait">false</prop>
</props>
</property>
The real trick for me turned out to be that the "protocol" value has to be "smtp" (not "smtps").
对我来说真正的诀窍原来是“协议”值必须是“smtp”(而不是“smtps”)。
#2
8
I struggled for an hour to find the right settings to send an email from Gmail using javamailsender and finally did it. I'm posting this as I cannot find a comprehensive example to send through gmail with javamailsender so hopefully this will help someone who want to do the same thing:
我挣扎了一个小时才找到正确的设置,使用javamailsender从Gmail发送电子邮件,最后做到了。我发布这个,因为我找不到一个全面的例子来通过gmail使用javamailsender发送,所以希望这会帮助那些想要做同样事情的人:
STEP 1:
Add the following settings to mail.properties:
将以下设置添加到mail.properties:
mail.protocol=smtp
mail.host=smtp.gmail.com
mail.port=465
mail.smtp.socketFactory.port=465
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.debug=true
mail.smtp.starttls.required=true
mail.smtp.socketFactory.fallback=false
mail.from=XXX@gmail.com
mail.username=XXX@gmail.com
mail.password=my_password
And then in your mailConfiguration class, @Value them in:
然后在你的mailConfiguration类中,@ Value他们:
@Configuration
@PropertySource("classpath:mail.properties")
public class MailConfiguration {
@Value("${mail.protocol}")
private String protocol;
@Value("${mail.host}")
private String host;
@Value("${mail.port}")
private int port;
@Value("${mail.smtp.socketFactory.port}")
private int socketPort;
@Value("${mail.smtp.auth}")
private boolean auth;
@Value("${mail.smtp.starttls.enable}")
private boolean starttls;
@Value("${mail.smtp.starttls.required}")
private boolean startlls_required;
@Value("${mail.smtp.debug}")
private boolean debug;
@Value("${mail.smtp.socketFactory.fallback}")
private boolean fallback;
@Value("${mail.from}")
private String from;
@Value("${mail.username}")
private String username;
@Value("${mail.password}")
private String password;
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties mailProperties = new Properties();
mailProperties.put("mail.smtp.auth", auth);
mailProperties.put("mail.smtp.starttls.enable", starttls);
mailProperties.put("mail.smtp.starttls.required", startlls_required);
mailProperties.put("mail.smtp.socketFactory.port", socketPort);
mailProperties.put("mail.smtp.debug", debug);
mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
mailProperties.put("mail.smtp.socketFactory.fallback", fallback);
mailSender.setJavaMailProperties(mailProperties);
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setProtocol(protocol);
mailSender.setUsername(username);
mailSender.setPassword(password);
return mailSender;
}
}
Please note that my Spring server is SSL enabled so that is why I'm using port 465. For SSL use port 465. If you are using 487, you must be using TLS.
请注意我的Spring服务器启用了SSL,这就是我使用端口465的原因。对于SSL使用端口465.如果您使用的是487,则必须使用TLS。
STEP 2:
Following this link and choose to turn on access to less secure apps.
点击此链接并选择开启对安全性较低的应用的访问权限。
https://www.google.com/settings/security/lesssecureapps
STEP 3:
Turn off AVAST if you have it on your PC. The AVAST Mail Shield conflicts with sending out emails. If you do not turn it off, you will get the following error:
如果您的PC上有AVAST,请将其关闭。 AVAST Mail Shield与发送电子邮件冲突。如果您不关闭它,您将收到以下错误:
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: Exception reading response; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.
servlet [dispatcherServlet]的Servlet.service()与path []的上下文引发了异常[请求处理失败;嵌套异常是org.springframework.mail.MailSendException:邮件服务器连接失败;嵌套异常是javax.mail.MessagingException:异常读取响应;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径。
#3
5
For Gmail to work with TLS or SSL:
要使Gmail使用TLS或SSL:
Port for TLS/STARTTLS: 587
Port for SSL: 465TLS / STARTTLS的端口:用于SSL的587端口:465
Both are manditory paramater are javax.net.ssl.SSLSocketFactory
, mail.smtp.socketFactory.fallback
and make mail.smtp.starttls.enable
to false.
两者都是manditory paramater,是javax.net.ssl.SSLSocketFactory,mail.smtp.socketFactory.fallback,并使mail.smtp.starttls.enable为false。
<beans>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host"><value>smtp.gmail.com</value></property>
<property name="port"><value>465</value></property>
<property name="protocol"><value>smtp</value></property>
<property name="username"><value>XXXXX@gmail.com</value></property>
<property name="password"><value>XXXX</value></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">false</prop>
<prop key="mail.smtp.quitwait">false</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="mailMail" class="com.embitel.service.email.EmailService">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean>
<bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="XXXX@gmail.com" />
<property name="to" value="yyyyy@gmail.com" />
<property name="subject" value="Testing Subject Line for email senind.." />
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean>
</beans>
worked like a charm!!!
像魅力一样工作!
#4
4
Sometime/first time google prevent the sign in to your account by any third party application or using your code. when you will sign in to your account in browser, you will get a message "Google prevents a Suspicious attempt to sign in to your account" see the screenshot below.
有时/第一次谷歌会阻止任何第三方应用程序或使用您的代码登录您的帐户。当您在浏览器中登录自己的帐户时,您会收到一条消息“Google阻止可疑的尝试登录您的帐户”,请参阅下面的屏幕截图。
click on the "Was it you" and allow the sign in.
点击“是你吗”并允许登录。
#5
4
The only property needed for GMail is
GMail所需的唯一属性是
<prop key="mail.smtp.starttls.enable">true</prop>
#6
2
Here's the javaConfig that worked for me:
这是适合我的javaConfig:
public JavaMailSender getJavaMailSender()
{
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setProtocol("smtp");
sender.setHost("smtp.gmail.com");
sender.setPort(587);
sender.setUsername("username");
sender.setPassword("password");
Properties mailProps = new Properties();
mailProps.put("mail.smtps.auth", "true");
mailProps.put("mail.smtp.starttls.enable", "true");
mailProps.put("mail.smtp.debug", "true");
sender.setJavaMailProperties(mailProps);
return sender;
}
I think you need to use port 587 for the TLS to work.
我认为您需要使用端口587才能使TLS正常工作。
#7
1
This worked for me:
这对我有用:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="protocol" value="smtps" />
<property name="username" value="my_email@domain.tld" />
<property name="password" value="my_password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">true</prop>
</props>
</property>
</bean>
See Google support for further information: http://support.google.com/mail/bin/answer.py?hl=en&answer=78799
有关详细信息,请参阅Google支持:http://support.google.com/mail/bin/answer.py?hl = zh-CN&answer = 78799
#8
1
The config below (yaml format) worked for me after
下面的配置(yaml格式)之后为我工作
- I disabled 2 factor authentication https://support.google.com/accounts/answer/1064203?hl=en&ref_topic=7189195
- I enabled less secure apps https://www.google.com/settings/security/lesssecureapps
我已禁用2因素身份验证https://support.google.com/accounts/answer/1064203?hl=zh-CN&ref_topic=7189195
我启用了安全性较低的应用https://www.google.com/settings/security/lesssecureapps
spring.mail:
host: smtp.gmail.com
port: 465
protocol: smtp
username: xyz@gmail.com
password: abc
test-connection: true
properties:
"mail.smtp.auth": true
"mail.smtp.starttls.enable": true
"mail.smtp.starttls.required": true
"mail.smtp.socketFactory.class": javax.net.ssl.SSLSocketFactory
"mail.debug": true
#9
0
This doesn't seem significantly different, but perhaps try:
这似乎没有显着不同,但也许尝试:
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="username" value="XXX@gmail.com" />
<property name="password" value="XXX" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.user" value="XXX@gmail.com" />
<prop key="mail.smtp.password" value="XXX" />
<prop key="mail.smtp.host">smtp.gmail.com</prop>
<prop key="mail.smtp.port">587</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
#10
0
You should specify your "from" address, either as <prop key="mail.smtp.from">XXX@gmail.com</prop>
, or when creating a message.
您应该指定“from”地址,可以是
#11
0
I have resoloved your question.
我已经解决了你的问题。
how to implements an async email service in spring
如何在spring中实现异步电子邮件服务
Note, this works on spring 3 , so I am not sure with spring 2.
注意,这适用于春天3,所以我不确定春天2。
#12
0
I was also facing this authentication exception and this is because of the gmail security. Open the following url
我也面临着这种身份验证异常,这是因为gmail的安全性。打开以下网址
https://www.google.com/settings/security/lesssecureapps
and enable the less security feature.
并启用较少的安全功能。
#1
29
This worked for me:
这对我有用:
<property name="host"><value>smtp.gmail.com</value></property>
<property name="port"><value>587</value></property>
<property name="protocol"><value>smtp</value></property>
<property name="username"><value>${mail.username}</value></property>
<property name="password"><value>${mail.password}</value></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.quitwait">false</prop>
</props>
</property>
The real trick for me turned out to be that the "protocol" value has to be "smtp" (not "smtps").
对我来说真正的诀窍原来是“协议”值必须是“smtp”(而不是“smtps”)。
#2
8
I struggled for an hour to find the right settings to send an email from Gmail using javamailsender and finally did it. I'm posting this as I cannot find a comprehensive example to send through gmail with javamailsender so hopefully this will help someone who want to do the same thing:
我挣扎了一个小时才找到正确的设置,使用javamailsender从Gmail发送电子邮件,最后做到了。我发布这个,因为我找不到一个全面的例子来通过gmail使用javamailsender发送,所以希望这会帮助那些想要做同样事情的人:
STEP 1:
Add the following settings to mail.properties:
将以下设置添加到mail.properties:
mail.protocol=smtp
mail.host=smtp.gmail.com
mail.port=465
mail.smtp.socketFactory.port=465
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.debug=true
mail.smtp.starttls.required=true
mail.smtp.socketFactory.fallback=false
mail.from=XXX@gmail.com
mail.username=XXX@gmail.com
mail.password=my_password
And then in your mailConfiguration class, @Value them in:
然后在你的mailConfiguration类中,@ Value他们:
@Configuration
@PropertySource("classpath:mail.properties")
public class MailConfiguration {
@Value("${mail.protocol}")
private String protocol;
@Value("${mail.host}")
private String host;
@Value("${mail.port}")
private int port;
@Value("${mail.smtp.socketFactory.port}")
private int socketPort;
@Value("${mail.smtp.auth}")
private boolean auth;
@Value("${mail.smtp.starttls.enable}")
private boolean starttls;
@Value("${mail.smtp.starttls.required}")
private boolean startlls_required;
@Value("${mail.smtp.debug}")
private boolean debug;
@Value("${mail.smtp.socketFactory.fallback}")
private boolean fallback;
@Value("${mail.from}")
private String from;
@Value("${mail.username}")
private String username;
@Value("${mail.password}")
private String password;
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties mailProperties = new Properties();
mailProperties.put("mail.smtp.auth", auth);
mailProperties.put("mail.smtp.starttls.enable", starttls);
mailProperties.put("mail.smtp.starttls.required", startlls_required);
mailProperties.put("mail.smtp.socketFactory.port", socketPort);
mailProperties.put("mail.smtp.debug", debug);
mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
mailProperties.put("mail.smtp.socketFactory.fallback", fallback);
mailSender.setJavaMailProperties(mailProperties);
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setProtocol(protocol);
mailSender.setUsername(username);
mailSender.setPassword(password);
return mailSender;
}
}
Please note that my Spring server is SSL enabled so that is why I'm using port 465. For SSL use port 465. If you are using 487, you must be using TLS.
请注意我的Spring服务器启用了SSL,这就是我使用端口465的原因。对于SSL使用端口465.如果您使用的是487,则必须使用TLS。
STEP 2:
Following this link and choose to turn on access to less secure apps.
点击此链接并选择开启对安全性较低的应用的访问权限。
https://www.google.com/settings/security/lesssecureapps
STEP 3:
Turn off AVAST if you have it on your PC. The AVAST Mail Shield conflicts with sending out emails. If you do not turn it off, you will get the following error:
如果您的PC上有AVAST,请将其关闭。 AVAST Mail Shield与发送电子邮件冲突。如果您不关闭它,您将收到以下错误:
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: Exception reading response; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.
servlet [dispatcherServlet]的Servlet.service()与path []的上下文引发了异常[请求处理失败;嵌套异常是org.springframework.mail.MailSendException:邮件服务器连接失败;嵌套异常是javax.mail.MessagingException:异常读取响应;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径。
#3
5
For Gmail to work with TLS or SSL:
要使Gmail使用TLS或SSL:
Port for TLS/STARTTLS: 587
Port for SSL: 465TLS / STARTTLS的端口:用于SSL的587端口:465
Both are manditory paramater are javax.net.ssl.SSLSocketFactory
, mail.smtp.socketFactory.fallback
and make mail.smtp.starttls.enable
to false.
两者都是manditory paramater,是javax.net.ssl.SSLSocketFactory,mail.smtp.socketFactory.fallback,并使mail.smtp.starttls.enable为false。
<beans>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host"><value>smtp.gmail.com</value></property>
<property name="port"><value>465</value></property>
<property name="protocol"><value>smtp</value></property>
<property name="username"><value>XXXXX@gmail.com</value></property>
<property name="password"><value>XXXX</value></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">false</prop>
<prop key="mail.smtp.quitwait">false</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="mailMail" class="com.embitel.service.email.EmailService">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean>
<bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="XXXX@gmail.com" />
<property name="to" value="yyyyy@gmail.com" />
<property name="subject" value="Testing Subject Line for email senind.." />
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean>
</beans>
worked like a charm!!!
像魅力一样工作!
#4
4
Sometime/first time google prevent the sign in to your account by any third party application or using your code. when you will sign in to your account in browser, you will get a message "Google prevents a Suspicious attempt to sign in to your account" see the screenshot below.
有时/第一次谷歌会阻止任何第三方应用程序或使用您的代码登录您的帐户。当您在浏览器中登录自己的帐户时,您会收到一条消息“Google阻止可疑的尝试登录您的帐户”,请参阅下面的屏幕截图。
click on the "Was it you" and allow the sign in.
点击“是你吗”并允许登录。
#5
4
The only property needed for GMail is
GMail所需的唯一属性是
<prop key="mail.smtp.starttls.enable">true</prop>
#6
2
Here's the javaConfig that worked for me:
这是适合我的javaConfig:
public JavaMailSender getJavaMailSender()
{
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setProtocol("smtp");
sender.setHost("smtp.gmail.com");
sender.setPort(587);
sender.setUsername("username");
sender.setPassword("password");
Properties mailProps = new Properties();
mailProps.put("mail.smtps.auth", "true");
mailProps.put("mail.smtp.starttls.enable", "true");
mailProps.put("mail.smtp.debug", "true");
sender.setJavaMailProperties(mailProps);
return sender;
}
I think you need to use port 587 for the TLS to work.
我认为您需要使用端口587才能使TLS正常工作。
#7
1
This worked for me:
这对我有用:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="protocol" value="smtps" />
<property name="username" value="my_email@domain.tld" />
<property name="password" value="my_password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">true</prop>
</props>
</property>
</bean>
See Google support for further information: http://support.google.com/mail/bin/answer.py?hl=en&answer=78799
有关详细信息,请参阅Google支持:http://support.google.com/mail/bin/answer.py?hl = zh-CN&answer = 78799
#8
1
The config below (yaml format) worked for me after
下面的配置(yaml格式)之后为我工作
- I disabled 2 factor authentication https://support.google.com/accounts/answer/1064203?hl=en&ref_topic=7189195
- I enabled less secure apps https://www.google.com/settings/security/lesssecureapps
我已禁用2因素身份验证https://support.google.com/accounts/answer/1064203?hl=zh-CN&ref_topic=7189195
我启用了安全性较低的应用https://www.google.com/settings/security/lesssecureapps
spring.mail:
host: smtp.gmail.com
port: 465
protocol: smtp
username: xyz@gmail.com
password: abc
test-connection: true
properties:
"mail.smtp.auth": true
"mail.smtp.starttls.enable": true
"mail.smtp.starttls.required": true
"mail.smtp.socketFactory.class": javax.net.ssl.SSLSocketFactory
"mail.debug": true
#9
0
This doesn't seem significantly different, but perhaps try:
这似乎没有显着不同,但也许尝试:
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="username" value="XXX@gmail.com" />
<property name="password" value="XXX" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.user" value="XXX@gmail.com" />
<prop key="mail.smtp.password" value="XXX" />
<prop key="mail.smtp.host">smtp.gmail.com</prop>
<prop key="mail.smtp.port">587</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
#10
0
You should specify your "from" address, either as <prop key="mail.smtp.from">XXX@gmail.com</prop>
, or when creating a message.
您应该指定“from”地址,可以是
#11
0
I have resoloved your question.
我已经解决了你的问题。
how to implements an async email service in spring
如何在spring中实现异步电子邮件服务
Note, this works on spring 3 , so I am not sure with spring 2.
注意,这适用于春天3,所以我不确定春天2。
#12
0
I was also facing this authentication exception and this is because of the gmail security. Open the following url
我也面临着这种身份验证异常,这是因为gmail的安全性。打开以下网址
https://www.google.com/settings/security/lesssecureapps
and enable the less security feature.
并启用较少的安全功能。