Skip to Main Content

New to Java

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!

sending multiple To address using java mail api

807599Dec 9 2006 — edited Dec 9 2006
I have written some code to deal with multiple To addresses
Please go through it. But i am getting an error. Please help
public class Emailer
{
	String subject;
	String body;
	Hashtable hashConfigParam = null;
	Address fromAddr;
	InternetAddress[] toAddr; 

  public Emailer()
  {
	  hashConfigParam= new Hashtable();
	  hashConfigParam.put("host","202.202.230.211");
  }

  public Address setFromAddr(String fromEmailAddr) throws AddressException
  {
	 fromAddr = new InternetAddress (fromEmailAddr);
	  return fromAddr;
  }

 public InternetAddress[] setToAddr(String[] toEmailAddr) throws AddressException
  {
	 toAddr[] = new InternetAddress[] (toEmailAddr[]);
	 return toAddr;

  }


  public void sendEmail(String fromEmailAddr, String[] toEmailAddr, String ccEmailAddr, String aSubject, String aBody) throws AddressException
	{
	  subject = aSubject;
	  body = aBody;

	  Address fAddr = setFromAddr(fromEmailAddr);
	  InternetAddress[] tAddr = setToAddr(toEmailAddr);

	  String h = (String)hashConfigParam.get("host");

	 
 	  Properties eMailConfigProps = null;
	  eMailConfigProps = System.getProperties();
	  eMailConfigProps.put("mail.smtp.host", h);

	  Session session = Session.getInstance(eMailConfigProps, null);
	  MimeMessage message = new MimeMessage(session);
		try
		{
		  message.setFrom(fAddr); 

		   for(i=1; i<= tAddr.length ; i++)
			{
			 message.addRecipient(Message.RecipientType.TO, new InternetAddress(tAddr));
}
message.addRecipient(Message.RecipientType.CC, new InternetAddress(ccEmailAddr));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
}

catch (SendFailedException ex)
{
System.err.println("Cannot send email. " + ex);
}

catch (MessagingException ex)
{
System.err.println("Exception. " + ex);
}
}


error::

C:\trainee>javac Emailer.java
Emailer.java:29: not a statement
toAddr[] = new InternetAddress[] (toEmailAddr[]);
^
Emailer.java:29: ';' expected
toAddr[] = new InternetAddress[] (toEmailAddr[]);
^
2 errors
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2007
Added on Dec 9 2006
4 comments
5,272 views