Jakarta.mail

jakarta.mail is a Java API for sending and receiving email messages. It is part of the Jakarta EE (Enterprise Edition) specification and is used to interact with email servers and send/receive email messages.

Here are some key features of jakarta.mail:

  1. Sending email: You can use jakarta.mail to send email messages to one or more recipients. You can specify the sender, recipient, subject, and body of the message.
  2. Receiving email: You can use jakarta.mail to receive email messages from an email server. You can specify the email server, port, and other settings to connect to the server.
  3. Email protocols: jakarta.mail supports several email protocols, including:
    • SMTP (Simple Mail Transfer Protocol) for sending email
    • POP3 (Post Office Protocol version 3) for receiving email
    • IMAP (Internet Message Access Protocol) for receiving email
  4. Email message handling: jakarta.mail provides classes for creating and manipulating email messages, including:
    • Message: represents an email message
    • Part: represents a part of an email message (e.g., attachment)
    • MimeMessage: represents a MIME-encoded email message
  5. Email server connection: jakarta.mail provides classes for connecting to email servers, including:
    • Session: represents a connection to an email server
    • Store: represents a connection to an email server's mailbox
    • Transport: represents a connection to an email server's SMTP server

Here is an example of how to use jakarta.mail to send an email message:

import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

public class SendEmail {
    public static void main(String[] args) {
        // Create a session
        Session session = Session.getDefaultInstance(new Properties());

        // Create a message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
        message.setSubject("Hello");
        message.setText("Hello, world!");

        // Send the message
        Transport.send(message);
    }
}

And here is an example of how to use jakarta.mail to receive an email message:

import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.search.FlagTerm;

public class ReceiveEmail {
    public static void main(String[] args) {
        // Create a session
        Session session = Session.getDefaultInstance(new Properties());

        // Create a store
        Store store = session.getStore("imap://username:[email protected]");

        // Open the store
        store.connect();

        // Search for new messages
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.RECENT)));

        // Process the messages
        for (Message message : messages) {
            System.out.println(message.getSubject());
            System.out.println(message.getText());
        }

        // Close the store
        store.close();
    }
}

Note that jakarta.mail is a Java API, so you need to use a Java development environment (such as Eclipse or IntelliJ IDEA) to write and run Java code that uses jakarta.mail.