Javamail code

Here is an example of how to use JavaMail to send an email:

Step 1: Add the JavaMail dependencies

Add the following dependencies to your pom.xml file (if you're using Maven) or your build.gradle file (if you're using Gradle):

<!-- Maven -->
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

<!-- Gradle -->
dependencies {
    implementation 'com.sun.mail:javax.mail:1.6.2'
}

Step 2: Create a JavaMail session

Create a JavaMail session by creating an instance of Session:

import javax.mail.Session;
import javax.mail.PasswordAuthentication;

public class EmailSender {
    public static void main(String[] args) {
        // Create a JavaMail session
        Properties props = new Properties();
        props.put("mail.smtp.host", "your-smtp-host");
        props.put("mail.smtp.port", "your-smtp-port");
        props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("your-email-username", "your-email-password");
            }
        });
    }
}

Step 3: Create a message

Create a Message object and set its properties:

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailSender {
    //...

    public void sendEmail() {
        // Create a message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("your-email-username"));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email-username"));
        message.setSubject("Test email");
        message.setText("This is a test email");
    }
}

Step 4: Send the message

Send the message using the Transport class:

import javax.mail.Transport;
import javax.mail.MessagingException;

public class EmailSender {
    //...

    public void sendEmail() {
        // Send the message
        Transport.send(message);
    }
}

Full code example

Here is the full code example:

import javax.mail.Session;
import javax.mail.PasswordAuthentication;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Transport;

public class EmailSender {
    public static void main(String[] args) {
        // Create a JavaMail session
        Properties props = new Properties();
        props.put("mail.smtp.host", "your-smtp-host");
        props.put("mail.smtp.port", "your-smtp-port");
        props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("your-email-username", "your-email-password");
            }
        });

        // Create a message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("your-email-username"));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email-username"));
        message.setSubject("Test email");
        message.setText("This is a test email");

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

Note that you'll need to replace the placeholders (your-smtp-host, your-smtp-port, your-email-username, and your-email-password) with your actual email server settings and credentials.