Mail api in java

Here are some popular Java APIs for sending and receiving emails:

  1. JavaMail API: A Java API for sending and receiving emails. It provides a simple and easy-to-use interface for working with emails.
  2. Apache Commons Email: A Java library for sending and receiving emails. It provides a simple and easy-to-use interface for working with emails.
  3. Spring Framework's Mail API: A Java API for sending and receiving emails as part of the Spring Framework.
  4. Java API for XML-Based Services (JAX-WS): A Java API for sending and receiving emails using SOAP-based web services.
  5. Java API for RESTful Web Services (JAX-RS): A Java API for sending and receiving emails using RESTful web services.

Here are some examples of how to use these APIs:

JavaMail API

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);
        }
    }
}

Apache Commons Email

import org.apache.commons.email.Email;
import org.apache.commons.email.EmailException;
import org.apache.commons.email.SimpleEmail;

public class SendEmail {
    public static void main(String[] args) {
        Email email = new SimpleEmail();
        email.setHostName("smtp.gmail.com");
        email.setSmtpPort(587);
        email.setAuthenticator(new DefaultAuthenticator("[email protected]", "your-password"));
        email.setFrom("[email protected]");
        email.setSubject("Test Email");
        email.setMsg("Hello, this is a test email.");

        try {
            email.send();
            System.out.println("Email sent successfully!");
        } catch (EmailException e) {
            throw new RuntimeException(e);
        }
    }
}

Spring Framework's Mail API

import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class SendEmail {
    @Autowired
    private MailSender mailSender;

    public void sendEmail() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("[email protected]");
        message.setTo("[email protected]");
        message.setSubject("Test Email");
        message.setText("Hello, this is a test email.");

        mailSender.send(message);
        System.out.println("Email sent successfully!");
    }
}

These are just a few examples of how to use these APIs. You can find more information and documentation on each API's website.