How to send a mail java 2017

A classic topic!

In Java, you can send an email using the JavaMail API. Here's a step-by-step guide on how to do it:

Prerequisites:

  1. You need to have JavaMail API (JavaMail 1.6.2 or later) in your project's classpath.
  2. You need to have a mail server configured (e.g., Gmail, Outlook, etc.).

Step 1: Add JavaMail API to your project

If you're using Maven, add the following dependency to your pom.xml file:

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

If you're not using Maven, you can download the JavaMail API JAR file from the official website and add it to your project's classpath.

Step 2: Create a JavaMail session

Create a Session object to connect to your mail server:

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

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

Replace "[email protected]" and "your-password" with your actual email address and password.

Step 3: Create a MimeMessage object

Create a MimeMessage object to represent the email:

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

// Create a MimeMessage
MimeMessage message = new MimeMessage(session);

Step 4: Set the email properties

Set the email properties, such as the sender, recipient, subject, and body:

// Set the sender
message.setFrom(new InternetAddress("[email protected]"));

// Set the recipient
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));

// Set the subject
message.setSubject("Hello from Java!");

// Set the body
message.setText("This is a test email sent from Java.");

Step 5: Send the email

Send the email using the Transport object:

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

Full code example:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

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

        // Create a MimeMessage object
        MimeMessage message = new MimeMessage(session);

        // Set the sender
        message.setFrom(new InternetAddress("[email protected]"));

        // Set the recipient
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));

        // Set the subject
        message.setSubject("Hello from Java!");

        // Set the body
        message.setText("This is a test email sent from Java.");

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

Note: Make sure to replace "[email protected]" and "your-password" with your actual email address and password. Also, ensure that your mail server allows less secure apps to access your account (if you're using Gmail).

That's it! With these steps, you should be able to send an email using JavaMail API in Java.