How to define java mail in pom.xml

To define Java Mail in your pom.xml file, you need to add the following dependencies:

  1. Add the following dependencies to your pom.xml file:

    <dependencies>
     <dependency>
         <groupId>com.sun.mail</groupId>
         <artifactId>javax.mail</artifactId>
         <version>1.6.2</version>
     </dependency>
     <dependency>
         <groupId>org.glassfish.jersey.containers</groupId>
         <artifactId>jersey-container-servlet-core</artifactId>
         <version>2.30</version>
     </dependency>
    </dependencies>

    The first dependency is for the Java Mail API, which provides the functionality for sending and receiving emails. The second dependency is for the Jersey library, which is used to create a RESTful web service.

  2. If you are using Maven 3.2.0 or later, you can use the following dependencies:

    <dependencies>
     <dependency>
         <groupId>org.glassfish.jersey.containers</groupId>
         <artifactId>jersey-container-servlet-core</artifactId>
         <version>2.30</version>
     </dependency>
     <dependency>
         <groupId>com.sun.mail</groupId>
         <artifactId>javax.mail</artifactId>
         <version>1.6.2</version>
     </dependency>
    </dependencies>
  3. If you are using Maven 3.1.0 or earlier, you can use the following dependencies:

    <dependencies>
     <dependency>
         <groupId>com.sun.mail</groupId>
         <artifactId>javax.mail</artifactId>
         <version>1.5.5</version>
     </dependency>
     <dependency>
         <groupId>org.glassfish.jersey.containers</groupId>
         <artifactId>jersey-container-servlet-core</artifactId>
         <version>2.0</version>
     </dependency>
    </dependencies>
  4. Make sure to update your Maven project by running the command mvn clean install or by clicking on the "Maven Update" button in your IDE.

  5. Once you have added the dependencies, you can use the Java Mail API in your Java code to send and receive emails.

Here is an example of how to use the Java Mail API to send an email:

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

public class SendEmail {
    public static void main(String[] args) {
        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("[email protected]"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
            message.setSubject("Test Email");
            message.setText("Hello, this is a test email.");

            Transport.send(message);

            System.out.println("Email sent successfully.");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

This code sends an email using the Java Mail API. You need to replace the placeholders ([email protected], your-password, and [email protected]) with your actual email credentials and the recipient's email address.