Java mail overview
The Spring Framework makes it easy and flexible to send emails in Java by providing helpful interfaces and classes.
This simplifies sending simple text emails, HTML emails, or even emails with attachments.
This simplifies sending simple text emails, HTML emails, or even emails with attachments.
Key Classes and Interfaces in Spring Mail
Class / Interface | Purpose |
---|---|
MailSender (interface) | Root interface for sending simple emails |
JavaMailSender (interface) | Extends MailSender to support MIME messages (e.g., HTML, attachments) |
JavaMailSenderImpl (class) | Concrete implementation to configure SMTP server, authentication, etc. |
SimpleMailMessage (class) | Represents a simple email with fields like from, to, subject, text |
MinmeMessagePreparator | Callback interface for preparing complex MIME message |
MimeMessageHelper | Helper class to easily create MIME messages, add attachments, inline images, etc. |
Example:
Folder & Files Structure
src/
└─ com.mail.quipoin/
├─ Mail.java
└─ Test.java
applicationContext.xml
Mail.java
This class defines the logic to send a simple mail
package com.mail.quipoin;
import org.springframework.mail.MailSender;import org.springframework.mail.SimpleMailMessage;
public class Mail { private MailSender mailSender;
public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; }
public void sendMail(String from, String to, String subject, String msg) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(msg); mailSender.send(message); }}
Test.java
Run this class to send an email
package com.mail.quipoin;
import org.springframework.beans.factory.*;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.*;
public class Test { public static void main(String[] args) { Resource resource = new ClassPathResource("applicationContext.xml"); BeanFactory factory = new XmlBeanFactory(resource); Mail mail = (Mail) factory.getBean("mailMail");
String sender = "your_sender_email@gmail.com"; String receiver = "receiver_email@gmail.com";
mail.sendMail(sender, receiver, "Offer letter", "Welcome to the Quipoin family!"); System.out.println("Success!!"); }}
Note: Replace your_sender_email@gmail.com and receiver_email@gmail.com with your actual emails.
applicationContext.xml
Spring configuration to set up SMTP server and bean wiring
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <property name="username" value="your_sender_email@gmail.com" /> <property name="password" value="your_app_password" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.socketFactory.port">465</prop> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> <prop key="mail.smtp.port">465</prop> </props> </property> </bean>
<bean id="mailMail" class="com.mail.quipoin.Mail"> <property name="mailSender" ref="mailSender" /> </bean></beans>
Important Security Note:
- Use App Passwords instead of your real password.
- Never share your credentials in public code.
Output
Success!!
Key Point
- Spring makes sending emails easy using MailSender and JavaMailSender.
- Use SimpleMailMessage for simple text emails.
- For complex emails (HTML, attachments), use MimeMessageHelper.
- Always secure your credentials.