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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With