Java Email Send send(final String username, final String password, final String toEmail, final String subject, final String content)

Here you can find the source of send(final String username, final String password, final String toEmail, final String subject, final String content)

Description

send

License

Apache License

Declaration

public static void send(final String username, final String password, final String toEmail,
            final String subject, final String content) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static void send(final String username, final String password, final String toEmail,
            final String subject, final String content) {

        // Get system properties
        Properties properties = System.getProperties();

        // Setup mail server
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");

        ////from   ww w .  ja v  a2  s.c  o m
        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
            message.setSubject(subject);
            message.setText(content);

            Transport.send(message);

            System.out.println("Done Sending E-mail.");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getMailSender(MimeMessage message)
  2. getSenderEmail(MimeMessage msg)
  3. send(String from, String to, String bcc, String subject, String content)
  4. send(String to, String from, String subject, String text, Properties mailProps)
  5. sendBulkUpdateFailureNotice(final String msgBody)
  6. sendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject, final String aBody)