I would like to send an email, when someone will try order thing in my application. So I have created a two main method to do this. The first sender method set configurations for sender. There I set host, user data, port, and other settings.
@Configuration
public class EmailData {
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("poczta.interia.pl");
javaMailSender.setPort(587);
javaMailSender.setUsername("myemail@interia.pl");
javaMailSender.setPassword("adminadmin123");
Properties properties = javaMailSender.getJavaMailProperties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.auth", "false");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.debug", "true");
return javaMailSender;
}
}
And the second method in class service trying to send a message:
@Service
@Async
public class EmailService {
@Autowired
private JavaMailSender javaMailSender;
public EmailService(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void sendMail(String toEmail, long idOrder) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("myemail@interia.pl");
simpleMailMessage.setTo("myemail1@gmail.com");
simpleMailMessage.setSubject("subject");
simpleMailMessage.setText("Thanks for your order!");
javaMailSender.send(simpleMailMessage);
}
There I am trying to send an email, but last line throws an exception:
javax.mail.AuthenticationFailedException: 535 5.7.8 Error: authentication failed: UGFzc3dvcmQ6
Also I have a configuration in properties file:
spring.mail.host=poczta.interia.pl
spring.mail.port=587
spring.mail.username=myemail@interia.pl
spring.mail.password=adminadmin123
spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.starttls.enable=true
What is wrong? Why it throws an error? It seems that email and password are correct. Any help will be appreciated.