1. Add the mail starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2. Configure SMTP in application.properties
spring.mail.host=smtp.sendcorex.com
spring.mail.port=587
spring.mail.username=${SENDCOREX_API_KEY}
spring.mail.password=${SENDCOREX_API_KEY}
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
Keep the key in an environment variable, never in the repository.
3. Send a plain-text email
@Service
public class MailService {
private final JavaMailSender mailer;
public MailService(JavaMailSender mailer) { this.mailer = mailer; }
public void sendWelcome(String to) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom("Acme <hello@yourdomain.com>");
msg.setTo(to);
msg.setSubject("Welcome to Acme");
msg.setText("Thanks for signing up!");
mailer.send(msg);
}
}
4. Send HTML
MimeMessage mime = mailer.createMimeMessage();
MimeMessageHelper h = new MimeMessageHelper(mime, true, "UTF-8");
h.setFrom("hello@yourdomain.com", "Acme");
h.setTo(to);
h.setSubject("Your receipt");
h.setText("Your receipt is attached.", "<html><body><p>Your receipt is attached.</p></body></html>");
mailer.send(mime);
Pass both a plain-text and an HTML version, and send a full HTML document with <html> and <body> tags — fragment-only HTML is a common spam signal.
5. Authenticate your domain
Before going live, add your domain in Sendcorex and publish the SPF, DKIM and DMARC records it shows. Use the same sender name on every email, matching your brand. Our SPF generator and DMARC generator build the records for you.