Sending Emails is a basic requirement, regardless of the platform you are working on, such as Java, Java EE, Python, etc. You may need to send error alerts or confirmation of registration or signup. Java provides the facility to send emails by writing Java programs.
Steps to Send an Email Using a Java Program
Step 1: Set Up Your Java Project
- Create a new Java project in your IDE (Eclipse, IntelliJ, NetBeans, etc.).
- If using Maven, create a pom.xml file.
- If not using Maven, use a plain Java project.
Step 2: Add Required Libraries
- Download or add the Jakarta Mail API and Java Activation Framework (JAF).
- Add the following JARs to your project classpath: mail.jar, smtp.jar and activation.jar
- If using Maven, add the dependency:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
Step 3: Decide Your SMTP Server
- Choose the email service you want to use (Gmail, Outlook, Yahoo, etc.).
- Note the SMTP host and port:
- Generate App Password if your service uses 2FA (Gmail, Outlook
| Service | SMTP Host | Port |
|---|---|---|
| Gmail | smtp.gmail.com | 587 |
| Outlook | smtp.office365.com | 587 |
| Yahoo | smtp.mail.yahoo.com | 587 |
Step 4: Configure SMTP Properties
Set properties using a Properties object in Java:
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Step 5: Create an Email Session
Create a session with Authenticator:
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your_email@gmail.com", "your_app_password");
}
});
Step 6: Compose the Email
- Create a MimeMessage object using the session.
- Set From, To, and Subject:
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your_email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("receiver@gmail.com"));
message.setSubject("Test Email from Java");
Step 7: Add Email Content
Plain Text Email:
message.setText("Hello! This is a test email.");
HTML Email:
message.setContent("<h1>Hello!</h1><p>This is an HTML email.</p>", "text/html");
Email with Attachment:
- Create MimeBodyPart for text
- Create MimeBodyPart for attachment
- Combine using Multipart and set it in the message
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("Please find the attachment.");
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile("C:/files/sample.pdf");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textPart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
Step 8: Send the Email
Use Transport.send() to send the email:
Transport.send(message);
System.out.println("Mail successfully sent!");
Step 9: Handle Exceptions
Wrap all code in a try-catch block to handle MessagingException and other errors.
Step 10: Test Your Program
- Run the program.
- Check your inbox to verify that the email is delivered.
- Try sending to multiple recipients by using:
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("user1@gmail.com, user2@gmail.com"));

