How to send email to more than one recipient
Hi All,
I have the following method to send a confirmation email message which is working. I would like to send the message to 3 or 4 different email addresses. Could anyone provide the format for the Bcc Ccc recipients or any other way of sending it to multiple people?
Thanks
Jim
public void sendConfirmationEmail(String name,String address,String basket, String email, String orderInfoID) {
// method to send an e-mail via SMTP
// (if no authentication is required from the mailserver)
boolean loggedIn = getLoggedIn();
String from = "woo@woo.com";
String to = email+",coo@woo.com";
String mailserver = "mail.woo.com";
log("To address is: "+to);
log("About to try and Email details: To: "+email+" Firstname: "+name);
try {
log("In the Email try");
SmtpClient client = new SmtpClient( mailserver );
log("Got client");
client.from(from);
client.to(to);
log("Set the client from and to - About to create the print stream");
java.io.PrintStream message = client.startMessage();
log("Started message");
message.println("To: " + to);
// TODO: set the subject line
message.println("Subject: Order Details - Woo Widgets Order");
// TODO: set the body of the message
message.println("Hello "+name+",\nThanks for your order with Focal Point Furniture Online.\n\nPlease check that the following information is correct and keep this Email for your records.\n\n");
message.println("Your Name: " +name+"\n");
message.println("YourAddress: " + address+"\n");
message.println("Your Order ID is: " +orderInfoID+"\n\n");
message.println("Your Order detail: " +basket+"\n\n");
if (loggedIn){
message.println("If any of these details are incorrect please log in to http://www.woo.co.uk to ammend.\nThanks for your order.\n\nRegards,\nJames Walker");
}else{
message.println("If any of these details are incorrect please telephone 01573228393 or Email correctOrder@woo.co.uk to rectify.\nThanks for your order.\n\nRegards,\nJames Woo");
}
client.closeServer();
log("Confirmation message is sent!!!");
} catch (java.io.IOException ex) {
// in the case of an exception, print a message to the output log
log("ERROR SENDING EMAIL:"+ex);
}
// return null;
}
[2399 byte] By [
jimmykelso] at [2007-11-26 8:19:02]

# 1
To use the code to send mail to multiple receipients, it looks like you will merely have to call client.to() multiple times, once for each address.
Ref: http://jal.paneris.net/xref/org/paneris/jal/model/Email.html
...
String[] toList,
...
for (int i = 0; i < toList.length; i++)
smtp.to(toList);
But it looks like SmtpClient does not support cc or bcc. In any case sun.net.smtp.SmtpClient is an internal class and not part of the API.
It may be better to use the JavaMail API to send emails:
http://java.sun.com/products/javamail/
http://java.sun.com/products/javamail/reference/techart/index.html
http://java.sun.com/developer/JDCTechTips/2001/tt1023.html#tip2
# 3
Hi All,
Thanks for the assistance. In the end I used the method shown below which sends the details to any address in the String array.
Regards
Jim
public void sendConfirmationEmail(String name,String address,String basket, String email, String orderInfoID) {
// method to send an e-mail via SMTP
// (if no authentication is required from the mailserver)
// This takes a String array of email addresses including the customers email
//and sends a formatted list of the customers order details
boolean loggedIn = getLoggedIn();
String from = "x@x.com";
String mailserver = "mail.x.com";
String [] toList = {"x@x.com","y@x.com",email,"ex@y.com"};
for (int i = 0; i < toList.length; i++){
try {
log("In the Email try");
SmtpClient client = new SmtpClient( mailserver );
log("Got client");
client.from(from);
String anEmail = toList;
client.to(anEmail);
log("The current email address is "+ anEmail);
java.io.PrintStream message = client.startMessage();
log("Started message");
message.println("To: " + anEmail);
// TODO: set the subject line
message.println("Subject: Order Details - Focal Point Furniture");
// TODO: set the body of the message
message.println("Hello "+name+",\nThanks for your order with x Online.\n\nPlease check that the following information is correct and keep this Email for your records.\n\n");
message.println("Your Name: " +name+"\n");
message.println("YourAddress: " + address+"\n");
message.println("Your Order ID is: " +orderInfoID+"\n\n");
message.println("Your Order detail: " +basket+"\n\n");
message.println("If any of these details are incorrect please telephone1234567or Email correctOrder@fx.com to rectify.\nThanks for your order.\n\nRegards,\nx.com");
log("Confirmation message is sent!!!");
client.closeServer();
} catch (java.io.IOException ex) {
// in the case of an exception, print a message to the output log
log("ERROR SENDING EMAIL:"+ex);
}
}
}