Cannot send email from server heroku using spring javamail

五迷三道 提交于 2020-01-21 11:46:06

问题


I tried to send Email from Heroku, using Spring javamail, but got error.

My code:

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);
    }
}

Beans:

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

And 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);
    }`

At localhost:8080 and localhost:5000, test completed successfully, but on heroku server I have exception:

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

I performed all recommendations from google. Can you help me solve this problem. Maybe link or something like that. Thank you.


回答1:


Devcenter heroku said:

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.

source




回答2:


I solved this activating DisplayUnlockCaptcha :

https://accounts.google.com/b/0/DisplayUnlockCaptcha

Also activating the Lesssecureapps

https://www.google.com/settings/security/lesssecureapps

And these are the properties :

#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



回答3:


This is my setting with my app running on Heroku, it worked:

#  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


来源:https://stackoverflow.com/questions/37900348/cannot-send-email-from-server-heroku-using-spring-javamail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!