JavaMail multiple recipients

J

John Bailo

I can send a JavaMail message to multiple recipients with


///
javax.mail.internet.InternetAddress[] addressTo = new
javax.mail.internet.InternetAddress[recipients.length];

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

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?
 
T

The_Sage

Reply to article by: John Bailo said:
Date written: Tue, 25 Jul 2006 14:29:37 -0700
MsgID:<[email protected]>
I can send a JavaMail message to multiple recipients with
///
javax.mail.internet.InternetAddress[] addressTo = new
javax.mail.internet.InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
addressTo = new javax.mail.internet.InternetAddress(recipients);

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?

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

=============================================================
http://members.cox.net/the.sage/index.htm

"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
=============================================================
 
Joined
Jun 30, 2010
Messages
1
Reaction score
0
Sendmail Help

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..
thanks
phil

import java.io.IOException;
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("mail.smtp.host", "smtp.mysmtp.here");


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

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 = new InternetAddress(a);
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>" +
"<p>&nbsp;</p></body></html>");

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

calculateDate();
writeEmailToFile();

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

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top