Java Email Send sendMail(Properties props, String recipients[], String subject, String message, String from)

Here you can find the source of sendMail(Properties props, String recipients[], String subject, String message, String from)

Description

Sends a mail to a list of recipents

License

Open Source License

Parameter

Parameter Description
props Mail properties (see javax.mail), such as smtp host....
recipients List of "To:"
subject Subject of the message
message Message to send
from "From:" user to use

Exception

Parameter Description
MessagingException an exception

Declaration

public static void sendMail(Properties props, String recipients[], String subject, String message, String from)
        throws MessagingException 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Main {
    /**//from w  ww.  ja  va  2s.c om
     * Sends a mail to a list of recipents
     * @param props Mail properties (see javax.mail), such as smtp host....
     * @param recipients List of "To:"
     * @param subject Subject of the message
     * @param message Message to send
     * @param from "From:" user to use
     * @throws MessagingException
     */
    public static void sendMail(Properties props, String recipients[], String subject, String message, String from)
            throws MessagingException {
        boolean debug = false;

        // create some properties and get the default Session
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);

        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }
}

Related

  1. sendEmail(final String aFromEmailAddr, final String aToEmailAddr, final String aSubject, final String aBody)
  2. sendEmail(Session session, String fromEmail, String toEmail, String subject, String body)
  3. sendEmail(String subject, String text, String receiverEmail)
  4. sendEmail(String to, String from, String subject, String text)
  5. sendEmail(String toAddress, String subject, String message)
  6. sendMail(Session session, Message message)
  7. sendMail(String filePathName, InternetAddress[] addresses, List> md5cellRows, String today, byte[] report1bytes, byte[] report2bytes, byte[] report3bytes)
  8. sendMail(String host, int port, String username, String password, String recipients, String subject, String content, String from)
  9. sendMail(String smtpServer, String to, String from, String subject, String body)