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

标签 java spring email heroku gmail

我尝试使用 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);
    }
}

bean 类:

<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>

和 Controller :

    @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

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

最佳答案

我解决了这个激活 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

关于java - 无法使用 spring javamail 从服务器 heroku 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37900348/

相关文章:

java - Jess打印输出内容用Java打印

java - 避免通过不受信任的证书连接

java - Apache poi XWPF 段落行间距

javascript - windows live 登录 获取电子邮件地址

java - Spring中的全局资源对象

java - 在 Spring MVC 中将 Bean 放入 Controller 时出现问题

java - Hibernate 无法正确显示 arraylist 的值

java - 如何在Spring中更改Messaging Exception的Failed Message

security - PHP Mailer 类 - 保护电子邮件凭据

java - Amazon sesThrotdling – 超出最大发送速率