Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to send multiple Recipients using the mail.jar and activation.jar

843838Nov 4 2005 — edited Apr 8 2008
hi!

could somebody help me. how do i send multiple Recipient using mail.jar. when i would input 2email address in to Recipient

(example: user1@domain.com, user2@domain.com)

i get a DEBUG: setDebug: JavaMail version 1.3.2

but if i send a single email it just works properly.

heres my code

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;



public class SendMail
{
public SendMail(String to, String from, String subject, String body)
//public SendMail(String to)
{
String message_recip = to;
String message_subject = subject;
String message_cc = "";
String message_body = body;

//The JavaMail session object
Session session;

//The JavaMail message object
Message mesg;

// Pass info to the mail server as a Properties, since JavaMail (wisely) allows room for LOTS of properties...
Properties props = new Properties( );

// LAN must define the local SMTP server as "mailhost" to be able to send mail...
//props.put("mail.smtp.host","true");
props.put("mail.smtp.host", "mailhost");

// Create the Session object
session = Session.getDefaultInstance(props, null);
session.setDebug(true);

try
{
// create a message
mesg = new MimeMessage(session);

// From Address - this should come from a Properties...
mesg.setFrom(new InternetAddress(from));

// TO Address
InternetAddress toAddress = new InternetAddress(message_recip);
mesg.addRecipient(Message.RecipientType.TO, toAddress);

// CC Address
InternetAddress ccAddress = new InternetAddress(message_cc);
mesg.addRecipient(Message.RecipientType.CC, ccAddress);

// The Subject
mesg.setSubject(message_subject);

// Now the message body.
mesg.setText(message_body);

// XXX I18N: use setText(msgText.getText( ), charset)
// Finally, send the message!
Transport.send(mesg);
}//end of try
catch (MessagingException ex)
{
while ((ex = (MessagingException)ex.getNextException( )) != null)
{
ex.printStackTrace( );
}//end of while
}//end of catch

}//end of SendMail

public static void main(String[] args)
{
//String t = "user1@domain.com, user2@domain.com"; - this I think causes error
String t = "user1@domain.com";
String f = "user3@domain.com";
String s = "Hello World";
String b = "the quick brown fox jumps over the lazy dog";

SendMail sm = new SendMail(t,f,s,b);
}//end of main

}//end of class

could someone please help me im stuck-up with this. thanx!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 6 2008
Added on Nov 4 2005
11 comments
2,887 views