Reputation: 83
I want to send emails using gmail smtp server and spring mail sender, it doesn' throw any exceptions but my mail isn't sent i don't receive it.
Here's how Service looks:
@Service
public class MailSenderService {
@Autowired
public MailSender mailSender;
public void prepareAndSend(String recipient, String message) {
try {
SimpleMailMessage mail = new SimpleMailMessage();
String from = "[email protected]";
String to = "[email protected]";
String subject = "Test subject";
mail.setFrom(from);
mail.setTo(recipient);
mail.setSubject(subject);
mail.setText(message);
mailSender.send(mail);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
AppProperties
spring.mail.host = smtp.gmail.com
spring.mail.username = ********@gmail.com
spring.mail.password = ********
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
spring.mail.properties.mail.smtp.ssl.enable = true
And pom dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
I deploy my app on port 8080 and just call this service with these two parameters, no exception is caught but in my testRecipient1 inbox i don't find any new mail, what have i missed ?
Upvotes: 1
Views: 8453
Reputation: 33
You could also try the below settings but i couldn't give more information regarding this as it will be a long post. However GeeksForGeeks do explain what everything does in the spring mail properties, also google has it's own guidelines which you can find here
p.s. I have also disabled the Access to less secure applications from the gmail account settings
Below is application.properties
spring.mail.host=smtp.gmail.com
spring.mail.port=465
[email protected]
spring.mail.password=*****
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.default-encoding=UTF-8
Below is my email service class (not that professional but for a personal projects it does the trick I need)
@Service
@AllArgsConstructor
@Slf4j
public class EmailService implements EmailSender {
private final JavaMailSender mailSender;
@Override
@Async
public void send(String to, String email) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
helper.setText(email, true);
helper.setTo(to);
helper.setSubject("Your subject");
helper.setFrom("[email protected]");
mailSender.send(mimeMessage);
} catch (MessagingException e) {
log.error("failed to send email", e);
throw new IllegalStateException("failed to send email");
}
}
}
Upvotes: 2
Reputation: 246
application.yml example (tested with Spring Boot 2.1.1):
spring
mail:
host: smtp.gmail.com
username: [email protected]
port: 587
password: ******
protocol: smtp
properties:
mail:
smtp:
starttls:
enable: true
Upvotes: 0
Reputation: 11
If You did not use SSL Certificate in You web application than try my code, it work perfectly in spring boot, text mail and HTML Mail both working perfectly.
@Autowired
private JavaMailSender mailSender;
@RequestMapping(value="/ShareVecancy",method=RequestMethod.GET)
public ModelAndView sendEmailToReference(HttpServletRequest request,HttpSession session){
try {
MimeMessage mail = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mail, true);
messageHelper.setTo("[email protected]");
messageHelper.setSubject("Testing mail");
messageHelper.setText("HTML Text or Any text You want to send ", true);
mailSender.send(mail);
} catch (MailException e) {
e.printStackTrace();
}
}
Property File:::
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=*******
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.default-encoding=UTF-8
Dependency ::
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Upvotes: 0