无法使用 spring javamail 从服务器 heroku 发送电子邮件

Vol*_*huk 4 java email gmail spring heroku

我尝试使用Spring javamailHeroku发送电子邮件,但出现错误。

我的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

豆子:

<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>
Run Code Online (Sandbox Code Playgroud)

和控制器:

    @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);
    }`
Run Code Online (Sandbox Code Playgroud)

在 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

我执行了谷歌的所有建议。你能帮我解决这个问题吗。也许链接或类似的东西。谢谢你。

Ski*_*ᴉʞS 8

我解决了这个激活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
Run Code Online (Sandbox Code Playgroud)