如何为 Gmail 配置 Spring JavaMailSenderImpl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2016190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Spring JavaMailSenderImpl for Gmail
提问by Andrew
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="[email protected]" />
<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="[email protected]" />
<property name="password" value="XXX" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.from">[email protected]</prop>
<prop key="mail.smtp.user">[email protected]</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>
回答by jsight
This doesn't seem significantly different, but perhaps try:
这似乎没有太大的不同,但也许可以尝试:
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="username" value="[email protected]" />
<property name="password" value="XXX" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.user" value="[email protected]" />
<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>
回答by axtavt
You should specify your "from" address, either as <prop key="mail.smtp.from">[email protected]</prop>
, or when creating a message.
您应该指定您的“发件人”地址,要么是<prop key="mail.smtp.from">[email protected]</prop>
,要么是在创建消息时。
回答by Ken Burcham
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”)。
回答by timomeinen
The only property needed for GMail is
GMail 所需的唯一属性是
<prop key="mail.smtp.starttls.enable">true</prop>
回答by Marc de Verdelhan
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="[email protected]" />
<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
回答by Rakesh Soni
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.
单击“是你吗”并允许登录。
回答by Vinod Java JEE
For Gmail to work with TLS or SSL:
要让 Gmail 使用 TLS 或 SSL:
Port for TLS/STARTTLS: 587
Port for SSL: 465
TLS/STARTTLS
端口:587 SSL 端口:465
Both are manditory paramater are javax.net.ssl.SSLSocketFactory
, mail.smtp.socketFactory.fallback
and make mail.smtp.starttls.enable
to false.
两者都是强制参数 are javax.net.ssl.SSLSocketFactory
,mail.smtp.socketFactory.fallback
并且 makemail.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>[email protected]</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="[email protected]" />
<property name="to" value="[email protected]" />
<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!!!
像魅力一样工作!!!
回答by karl li
I have resoloved your question.
我已经解决了你的问题。
how to implements an async email service in spring
Note, this works on spring 3 , so I am not sure with spring 2.
请注意,这适用于 spring 3 ,所以我不确定 spring 2 。
回答by Hemant Nagpal
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
https://www.google.com/settings/security/lesssecureapps
and enable the less security feature.
并启用较少的安全功能。
回答by leeor
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 正常工作。