Maven jakarta mail

A great combination!

To use Jakarta Mail (formerly known as JavaMail) with Maven, you'll need to add the following dependencies to your pom.xml file:

  1. Jakarta Mail API:
    <dependency>
     <groupId>org.glassfish.jakarta.mail</groupId>
     <artifactId>jakarta.mail-api</artifactId>
     <version>2.1.7</version>
    </dependency>
  2. Jakarta Mail implementation (e.g., GMail):
    <dependency>
     <groupId>org.glassfish.jakarta.mail</groupId>
     <artifactId>jakarta.mail-gmail</artifactId>
     <version>2.1.7</version>
    </dependency>

    Alternatively, you can use other implementations like OpenMail or Oracle's JavaMail implementation.

Here's an example pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
     http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jakarta.mail</groupId>
            <artifactId>jakarta.mail-api</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jakarta.mail</groupId>
            <artifactId>jakarta.mail-gmail</artifactId>
            <version>2.1.7</version>
        </dependency>
    </dependencies>

</project>

Once you've added the dependencies, you can use Jakarta Mail in your Java code to send emails. Here's a simple example:

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

public class EmailSender {
    public static void sendEmail(String from, String to, String subject, String body) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", 587);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]", "your-password");
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setText(body);

            Transport.send(message);
            System.out.println("Email sent successfully!");
        } catch (MessagingException e) {
            System.out.println("Error sending email: " + e.getMessage());
        }
    }
}

Make sure to replace the placeholders ([email protected] and your-password) with your actual Gmail credentials.