Language: Java
Email/Communication
JavaMail was originally developed as part of the Java EE platform and later moved to the Eclipse Foundation under Jakarta EE. It abstracts the complexities of email protocols, allowing developers to focus on application logic. Jakarta Mail is widely used in enterprise applications, automated email notifications, and email clients.
Jakarta Mail (formerly JavaMail) is a Java API for sending, receiving, and managing email messages using protocols such as SMTP, IMAP, and POP3. It provides a simple interface for building email applications and integrating email functionality into Java applications.
Add dependency in pom.xml:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.1.2</version>
</dependency>Add dependency in build.gradle:
implementation 'com.sun.mail:jakarta.mail:2.1.2'Jakarta Mail allows sending emails via SMTP, reading emails from IMAP or POP3, handling attachments, multipart messages, HTML content, and managing folders. It integrates easily with Java SE and Jakarta EE applications.
import jakarta.mail.*;
import jakarta.mail.internet.*;
import java.util.Properties;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@example.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@example.com"));
message.setSubject("Test Email");
message.setText("Hello, this is a test email.");
Transport.send(message);Sends a simple text email using SMTP with authentication.
Properties props = new Properties();
props.put("mail.store.protocol", "imap");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imap");
store.connect("imap.example.com", "username", "password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
for (Message msg : messages) {
System.out.println(msg.getSubject());
}
inbox.close(false);
store.close();Connects to an IMAP server and prints the subjects of emails in the inbox.
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@example.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@example.com"));
message.setSubject("HTML Email");
message.setContent("<h1>Hello</h1><p>This is an HTML email</p>", "text/html");
Transport.send(message);Sends an email with HTML content.
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("Please find the attachment.");
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(new File("file.pdf"));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textPart);
multipart.addBodyPart(attachmentPart);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@example.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@example.com"));
message.setSubject("Email with Attachment");
message.setContent(multipart);
Transport.send(message);Sends an email with both text content and a file attachment.
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
for (Message msg : inbox.getMessages()) {
if (!msg.isSet(Flags.Flag.SEEN)) {
System.out.println("Unread: " + msg.getSubject());
msg.setFlag(Flags.Flag.SEEN, true);
}
}
inbox.close(true);Reads unread emails and marks them as seen.
Use SSL/TLS for secure email transmission.
Reuse Session objects instead of creating new ones repeatedly.
Handle exceptions like MessagingException and IOException gracefully.
Close connections, folders, and stores properly to avoid resource leaks.
Validate email addresses before sending to prevent delivery failures.