I am trying to send emails from spring boot using spring-boot-starter-mail using a REST api.
After sending a post request with the following body, I never get a response.
{
"senderName":"radwan",
"confirmationEmail":"[email protected]",
"confirmationBody" : "test body"
}
This is the email model :
public class ConfirmationEmail {
@NotNull
private String senderName;
@NotNull
@Email
private String confirmationEmail;
@NotNull
private String confirmationBody;
public String getSenderName() {
return senderName;
}
public void setSenderName(String name) {
this.senderName = name;
}
public String getConfirmationEmail() {
return confirmationEmail;
}
public void setConfirmationEmail(String confirmationEmail) {
this.confirmationEmail = confirmationEmail;
}
public String getConfirmationBody() {
return confirmationBody;
}
public void setConfirmationBody(String confirmationBody) {
this.confirmationBody = confirmationBody;
}
}
The email configuration class :
@Component
public class EmailConfig {
@Value("${spring.mail.host}")
private String host;
@Value("${spring.mail.port}")
private int port;
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
and the rest controller :
@RestController
@RequestMapping(path = "api/v1/email")
public class EmailController {
private EmailConfig emailConfig;
@Autowired
public EmailController(EmailConfig emailConfig) {
this.emailConfig = emailConfig;
}
@PostMapping
public void sendEmail(@RequestBody ConfirmationEmail confirmationEmail,
BindingResult bindingResult) throws ValidationException {
if (bindingResult.hasErrors()) {
throw new ValidationException("Invalid");
}
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(emailConfig.getHost());
mailSender.setPort(emailConfig.getPort());
mailSender.setUsername(emailConfig.getUsername());
mailSender.setPassword(emailConfig.getPassword());
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(confirmationEmail.getConfirmationEmail());
simpleMailMessage.setTo("[email protected]");
simpleMailMessage.setSubject("Test Subject from "
confirmationEmail.getSenderName());
simpleMailMessage.setText(confirmationEmail.getConfirmationBody());
mailSender.send(simpleMailMessage);
}
}
These are the application.properties entries:
spring.mail.host=smtp.mailtrap.io
spring.mail.port=2525
spring.mail.username=<>
spring.mail.password=<>
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
Thanks
EDIT : Added pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mailingTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mailingTest</name>
<description>mailingTest</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
CodePudding user response:
Since you are using spring-boot-mail-starter, you do not need to create the sender. That is why some of the properties are not read/in use.
For mailtrap, I believe you should have properties set as such:
spring.mail.host=smtp.mailtrap.io
spring.mail.port=2525
spring.mail.username=<your username>
spring.mail.password=<your password>
spring.mail.properties.mail.smtp.starttls.enabled: true //you are missing this one
Then, in your controller:
@RestController
@RequestMapping(path = "api/v1/email")
public class EmailController {
private final JavaMailSender javaMailSender;
@Autowired
public EmailController(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
...
@PostMapping
public void sendEmail(@RequestBody ConfirmationEmail confirmationEmail,
BindingResult bindingResult) throws ValidationException {
if (bindingResult.hasErrors()) {
throw new ValidationException("Invalid");
}
MimeMessage mail = javaMailSender.createMimeMessage();
// set to, from, subject, body/text, etc...
javaMailSender.send(mail);
}
}
CodePudding user response:
Mailtrap is a fake SMTP server designed to test, view, and share emails sent from the development and staging environments without spamming real customers or flooding your own inboxes.