Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JavaMail multiple recipients

Thread Tools

JavaMail multiple recipients

John Bailo
Posts: n/a

I can send a JavaMail message to multiple recipients with

javax.mail.internet.InternetAddress[] addressTo = new

for (int i = 0; i < recipients.length; i++)
addressTo[i] = new javax.mail.internet.InternetAddress(recipients[i]);

msg.setRecipients(javax.mail.Message.RecipientType .TO, addressTo);

But this sends out individual emails to each recipient. How can I send
a single email with multiple recipients so that all the recipients names
appear on the To: line?

Reply With Quote
Posts: n/a
>Reply to article by: John Bailo <(E-Mail Removed)>
>Date written: Tue, 25 Jul 2006 14:29:37 -0700
>MsgID:<07SdnYqIiqHdEFvZnZ2dnUVZ_sGdnZ2d@speakeasy .net>

>I can send a JavaMail message to multiple recipients with

>javax.mail.internet.InternetAddress[] addressTo = new

>for (int i = 0; i < recipients.length; i++)
>addressTo[i] = new javax.mail.internet.InternetAddress(recipients[i]);

>msg.setRecipients(javax.mail.Message.RecipientTyp e.TO, addressTo);

>But this sends out individual emails to each recipient. How can I send
>a single email with multiple recipients so that all the recipients names
>appear on the To: line?

Your addressTo object is a multiple element array when the "To:" field needs to
be a single line of text, with each receipient email address concantenated to
the others with a space and a semi-colon. Just look at how it is done on your
email application on your computer, ie -- MS Outlook for example.

The Sage

================================================== ===========

"All those painted screens erected by man to shut out reality
-- history, religion, duty, social position --
all were illusions, mere opium fantasies"
John Fowles, The French Lieutenant's Woman
================================================== ===========
Reply With Quote
philmin philmin is offline
Junior Member
Join Date: Jun 2010
Posts: 1
Hi, another newbie - my code is almost right, but I cannot work out how to send duplicate emails. I get a java.lang.ArrayIndexOutOfBoundsException, I have had the whole system working for one email, just not lots. Please help me, I am sure I am nearly there, I just need a final shove..

import java.util.Date;
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 SendEmail extends Helper
//Variables to pull in form details
public static String strChangeRequestNumber;
public static Object objChangeRequestType;
public static String strChangeDescription;
public static Object objRecipients;
public static String[] a;

//main procedure
public static void main(String[] args) throws IOException
ChangeRequestForm ChangeRequestForm = new ChangeRequestForm(null);
//get variables from Form when control is returned
String strChangeRequestNumber = ChangeRequestForm.strChangeRequestNumber;
String strRecipients = ChangeRequestForm.strRecipients;
Object objChangeRequestType = ChangeRequestForm.objChangeType;
String strChangeDescription = ChangeRequestForm.strChangeDescription;
objRecipients = ChangeRequestForm.objRecipient;
a = ChangeRequestForm.a;

Properties props = (Properties)System.getProperties().clone();
props.put("", "");

// set as properties as needed using current session
Session session = Session.getInstance(props, null);

try {
Message msg = new MimeMessage(session);

//***edit in to dictate from address (if allowed)***
msg.setFrom(new InternetAddress("MyFromEmailAddressHere"));
//InternetAddress[] address = {new InternetAddress((String) objRecipients)};
//InternetAddress[] address = {new InternetAddress((String) strRecipients)};

InternetAddress[] addressTo = new InternetAddress[strRecipients.length()];
//(StrRecipients is produced from a combibox with multiple selections enabled, I am happy with the output from this - each email addr is seperated by a comma etc)

//For each recipient, send an email..if only...
for (int i = 0; i < strRecipients.length(); i++)
addressTo[i] = new InternetAddress(a[i]);
msg.setRecipients(Message.RecipientType.TO, addressTo);


//msg.setRecipients(Message.RecipientType.TO, address);
//msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("My subject here: " + objChangeRequestType + strChangeRequestNumber);
msg.setSentDate(new Date());

//HTML format of email
strMsgText =("<html><head><title></title></head>" +
"<span style = \"FONT-family: Tahoma\">" +
"<body><p>Hi,</p>" +
"<p>some text:<br><br>" +
"<span style=\"BACKGROUND-COLOR: #ffffff\">" +
objChangeRequestType +
strChangeRequestNumber +
"<br><br>" + "Description: " +
strChangeDescription +
"</span></p>" +

msg.setContent(Helper.strMsgText, "text/html");


displayMsgBox("email sent to" + ChangeRequestForm.a[ChangeRequestForm.q]);
displayMsgBox("email sent, no option to recall!");
catch (MessagingException e) {
displayMsgBox("error sending message");


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Emailing Multiple Recipients jeffbee Computer Support 1 08-18-2004 03:28 PM
Single email to multiple recipients .Net Newbie ASP .Net 2 07-01-2004 02:11 PM
smtp module - specifying multiple email recipients? Alex Hunsley Python 2 07-01-2004 01:32 PM
FTP ... send to multiple recipients? ºoºfizzyºoº Computer Support 5 01-03-2004 05:42 PM
Multiple bcc recipients Erica Perl 1 10-06-2003 07:14 PM