Apache common mail

Apache Commons Mail is a library developed by the Apache Software Foundation that provides a simple and flexible way to send and receive email messages in Java applications. It is part of the Apache Commons project, which is a collection of reusable Java libraries that provide common functionality for Java applications.

Apache Commons Mail provides a number of features that make it a popular choice for sending and receiving email in Java applications, including:

  1. Simple API: The library provides a simple and easy-to-use API for sending and receiving email messages.
  2. Support for multiple email protocols: Apache Commons Mail supports multiple email protocols, including SMTP, POP3, and IMAP.
  3. Support for multiple email clients: The library supports multiple email clients, including Gmail, Yahoo, and Outlook.
  4. Encryption and authentication: Apache Commons Mail provides support for encryption and authentication, including SSL/TLS and username/password authentication.
  5. Thread-safe: The library is designed to be thread-safe, making it suitable for use in multi-threaded applications.

Some of the key classes in Apache Commons Mail include:

  1. Mail: The Mail class represents an email message and provides methods for setting the message's subject, body, and attachments.
  2. MailSession: The MailSession class represents a session with an email server and provides methods for sending and receiving email messages.
  3. SmtpMailSession: The SmtpMailSession class represents a session with an SMTP email server and provides methods for sending email messages.
  4. Pop3MailSession: The Pop3MailSession class represents a session with a POP3 email server and provides methods for receiving email messages.

Apache Commons Mail is widely used in Java applications, including web applications, desktop applications, and enterprise applications. It is also used in a number of popular Java frameworks and libraries, including Spring and Hibernate.

Here is an example of how to use Apache Commons Mail to send an email message:

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SendEmail {
    public static void main(String[] args) {
        Email email = new SimpleEmail();
        email.setHostName("smtp.gmail.com");
        email.setSmtpPort(587);
        email.setAuthenticator(new DefaultAuthenticator("username", "password"));
        email.setFrom("[email protected]");
        email.setSubject("Test email");
        email.setMsg("This is a test email");
        try {
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}

This example shows how to create a SimpleEmail object and set its properties, including the host name, SMTP port, authentication credentials, from address, subject, and message body. The send() method is then called to send the email message.