Here you can find the source of sendMail(Properties props, String recipients[], String subject, String message, String from)
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 |
Parameter | Description |
---|---|
MessagingException | an exception |
public static void sendMail(Properties props, String recipients[], String subject, String message, String from) throws MessagingException
//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); } }