How can we send mail using java

There are several ways to send mail using Java. Here are a few approaches:

1. Using JavaMail API

The JavaMail API is a popular and widely-used library for sending emails in Java. You can download the API from the official Oracle website.

Here's an example code snippet that demonstrates how to send an email using JavaMail API:

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

public class SendEmail {
    public static void main(String[] args) {
        // Set up the mail server
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", 587);
        props.put("mail.smtp.auth", "true");

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

        // Create a message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
        message.setSubject("Test Email");
        message.setText("This is a test email sent using JavaMail API");

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

2. Using JavaMail API with Spring

If you're using the Spring framework, you can use the JavaMailSender class to send emails. Here's an example code snippet:

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreprocessor;
import org.springframework.mail.javamail.MimeMessagePreprocessorFactory;

public class SendEmail {
    @Autowired
    private JavaMailSender mailSender;

    public void sendEmail() {
        MimeMessagePreprocessorFactory factory = new MimeMessagePreprocessorFactory();
        MimeMessagePreprocessor preprocessor = factory.createPreprocessor();

        MimeMessage message = mailSender.createMimeMessage();
        message.setFrom("[email protected]");
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
        message.setSubject("Test Email");
        message.setText("This is a test email sent using JavaMail API with Spring");

        preprocessor.processMessage(message);

        mailSender.send(message);
    }
}

3. Using Apache Commons Email

Apache Commons Email is another popular library for sending emails in Java. Here's an example code snippet:

import org.apache.commons.mail.DefaultAuthenticatingMailSession;
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) {
        // Set up the mail server
        DefaultAuthenticatingMailSession mailSession = new DefaultAuthenticatingMailSession("smtp.gmail.com", 587, "[email protected]", "your-password");

        // Create an email object
        Email email = new SimpleEmail();
        email.setFrom("[email protected]");
        email.setSubject("Test Email");
        email.setMsg("This is a test email sent using Apache Commons Email");

        // Send the email
        try {
            email.send(mailSession);
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}

These are just a few examples of how you can send mail using Java. The choice of library and approach depends on your specific requirements and preferences.