我尝试使用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服务器上,我有例外:
验证失败;嵌套的异常是javax.mail.AuthenticationFailedException:534-5.7.14请通过Web浏览器和534-5.7.14登录,然后重试。 534-5.7.14在534 5.7.14了解更多信息https://support.google.com/mail/answer/78754 j4sm53975482wjg.20-gsmtp
我执行了Google的所有建议。您能帮我解决这个问题吗?也许链接或类似的东西。谢谢。
最佳答案
我解决了这个激活 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