无法使用 spring javamail 从服务器 heroku 发送电子邮件
Cannot send email from server heroku using spring javamail
我尝试使用 Spring javamail 从 Heroku 发送电子邮件,但出现错误。
我的代码:
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
@Service("mailService")
public class JavaMailerServiceImpl {
private MailSender mailSender;
public JavaMailerServiceImpl(JavaMailSenderImpl mailSender) {
this.mailSender = mailSender;
}
public void sendMail(String to, String subject, String body){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
}
豆类:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"/>
<property name="port" value="587"/>
<property name="username" value="**********@gmail.com"/>
<property name="password" value="********"/>
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
和控制器:
@Autowired
private JavaMailerServiceImpl mailService;
@RequestMapping(method = RequestMethod.POST)
public String sendEmail(HttpServletRequest request) {
String recipientAddress = request.getParameter("recipient");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
mailService.sendMail(recipientAddress,subject,message);
}`
在 localhost:8080 和 localhost:5000,测试成功完成,但在 heroku 服务器上出现异常:
Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 j4sm53975482wjg.20 - gsmtp
我执行了 google 中的所有建议。你能帮我解决这个问题吗?也许 link 或类似的东西。谢谢。
Devcenter heroku 说:
The Heroku platform itself doesn’t provide an email service - but instead provides add-ons that act as backing services - that can be attached to your app to provide the service.
Consult the Heroku Add-ons marketplace for an appropriate email service that matches your requirements.
这是我在 Heroku 上对我的应用 运行 进行的设置,它有效:
# sparkpost.com SMTP server:
mail.host=smtp.sparkpostmail.com
mail.port=587
mail.smtp.auth=true
mail.smtp.socketFactory.port=587
mail.smtp.socketFactory.fallback=true
mail.smtp.starttls.enable=true
mail.smtp.starttls.required=true
mail.smtp.ssl.enable=false
#mail.smtp.debug=true
mail.username=xxxx
mail.password=xxxx
我解决了这个激活 DisplayUnlockCaptcha :
https://accounts.google.com/b/0/DisplayUnlockCaptcha
同时激活 Lesssecureapps
https://www.google.com/settings/security/lesssecureapps
这些是属性:
#Mail properties
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.gmail.com
spring.mail.username=--
spring.mail.password=--
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.debug=true
spring.mail.smtp.auth=true
spring.mail.smtp.starttls.enable=true
by Skizo-ozᴉʞS 是正确的,但截至 2022 年 5 月 30 日,Google 将不会不再支持使用 third-party 应用程序和设备,允许您使用 用户名和密码 .[= 登录您的 Google 帐户20=]
但是,Google 提供了一个简单的解决方案。
输入由 Google 生成的 应用程序密码 而不是密码。首先,进入设置并启用 2-Step Verification
.
然后点击App passwords
。
您应该会看到应用程序密码屏幕。 应用密码可让您从不支持两步验证 的设备上的应用登录您的Google 帐户。 Select Mail
作为应用,然后 select 设备。就我而言,我选择了 Other
,因为我想将我的应用程序部署到云端。
完成后,单击 GENERATE
按钮。您将看到生成的应用程序密码。
只需复制密码并将您的电子邮件发送服务中的旧密码替换为生成的密码即可。不过,您将无法再次看到密码。
就是这样!
我尝试使用 Spring javamail 从 Heroku 发送电子邮件,但出现错误。
我的代码:
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
@Service("mailService")
public class JavaMailerServiceImpl {
private MailSender mailSender;
public JavaMailerServiceImpl(JavaMailSenderImpl mailSender) {
this.mailSender = mailSender;
}
public void sendMail(String to, String subject, String body){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
}
豆类:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"/>
<property name="port" value="587"/>
<property name="username" value="**********@gmail.com"/>
<property name="password" value="********"/>
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
和控制器:
@Autowired
private JavaMailerServiceImpl mailService;
@RequestMapping(method = RequestMethod.POST)
public String sendEmail(HttpServletRequest request) {
String recipientAddress = request.getParameter("recipient");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
mailService.sendMail(recipientAddress,subject,message);
}`
在 localhost:8080 和 localhost:5000,测试成功完成,但在 heroku 服务器上出现异常:
Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 j4sm53975482wjg.20 - gsmtp
我执行了 google 中的所有建议。你能帮我解决这个问题吗?也许 link 或类似的东西。谢谢。
Devcenter heroku 说:
The Heroku platform itself doesn’t provide an email service - but instead provides add-ons that act as backing services - that can be attached to your app to provide the service.
Consult the Heroku Add-ons marketplace for an appropriate email service that matches your requirements.
这是我在 Heroku 上对我的应用 运行 进行的设置,它有效:
# sparkpost.com SMTP server:
mail.host=smtp.sparkpostmail.com
mail.port=587
mail.smtp.auth=true
mail.smtp.socketFactory.port=587
mail.smtp.socketFactory.fallback=true
mail.smtp.starttls.enable=true
mail.smtp.starttls.required=true
mail.smtp.ssl.enable=false
#mail.smtp.debug=true
mail.username=xxxx
mail.password=xxxx
我解决了这个激活 DisplayUnlockCaptcha :
https://accounts.google.com/b/0/DisplayUnlockCaptcha
同时激活 Lesssecureapps
https://www.google.com/settings/security/lesssecureapps
这些是属性:
#Mail properties
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.gmail.com
spring.mail.username=--
spring.mail.password=--
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.debug=true
spring.mail.smtp.auth=true
spring.mail.smtp.starttls.enable=true
但是,Google 提供了一个简单的解决方案。 输入由 Google 生成的 应用程序密码 而不是密码。首先,进入设置并启用 然后点击 您应该会看到应用程序密码屏幕。 应用密码可让您从不支持两步验证 的设备上的应用登录您的Google 帐户。 Select 完成后,单击 只需复制密码并将您的电子邮件发送服务中的旧密码替换为生成的密码即可。不过,您将无法再次看到密码。 就是这样!2-Step Verification
.App passwords
。Mail
作为应用,然后 select 设备。就我而言,我选择了 Other
,因为我想将我的应用程序部署到云端。GENERATE
按钮。您将看到生成的应用程序密码。