Java Email Send sendEmail(Session session, String fromEmail, String toEmail, String subject, String body)

Here you can find the source of sendEmail(Session session, String fromEmail, String toEmail, String subject, String body)

Description

Utility method to send simple HTML email

License

Apache License

Parameter

Parameter Description
session a parameter
toEmail a parameter
subject a parameter
body a parameter

Declaration

public static void sendEmail(Session session, String fromEmail, String toEmail, String subject, String body) 

Method Source Code


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

import java.util.Date;

import javax.mail.Message;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

public class Main {
    /**// w w  w  . ja v a 2s  .c o  m
     * Utility method to send simple HTML email
     * @param session
     * @param toEmail
     * @param subject
     * @param body
     */
    public static void sendEmail(Session session, String fromEmail, String toEmail, String subject, String body) {
        try {
            MimeMessage msg = new MimeMessage(session);
            //set message headers
            msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
            msg.addHeader("format", "flowed");
            msg.addHeader("Content-Transfer-Encoding", "8bit");

            msg.setFrom(new InternetAddress(fromEmail, "NoReply-JD"));

            msg.setReplyTo(InternetAddress.parse("no_reply@journaldev.com", false));

            msg.setSubject(subject, "UTF-8");

            msg.setText(body, "UTF-8");

            msg.setSentDate(new Date());

            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
            System.out.println("Message is ready");
            Transport.send(msg);

            System.out.println("EMail Sent Successfully!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. send(final String username, final String password, final String toEmail, final String subject, final String content)
  2. send(String from, String to, String bcc, String subject, String content)
  3. send(String to, String from, String subject, String text, Properties mailProps)
  4. sendBulkUpdateFailureNotice(final String msgBody)
  5. sendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject, final String aBody)
  6. sendEmail(String subject, String text, String receiverEmail)
  7. sendEmail(String to, String from, String subject, String text)
  8. sendEmail(String toAddress, String subject, String message)
  9. sendMail(Properties props, String recipients[], String subject, String message, String from)