Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending Email to multiple TO

Status
Not open for further replies.

crguy

Programmer
May 24, 2001
34
CA
Hi,

I have a Java class SendMail.java (code below).
I have a jsp page that send the required parameters to the Java class. (code below). Everything works as it should except when I try to send it to multiple people in the TO field. Can anybody direct me on how to fix this problem?

Thanks
crguy

=============================================
SendMail.jsp
=============================================
<%@ page contentType=&quot;text/html;charset=WINDOWS-1252&quot;%>

<HTML>
<HEAD>

<TITLE>OC4J JSP Sample - Send Email</TITLE>
</HEAD>
<BODY background=&quot;../../images/Background.gif&quot;>
<CENTER>
<BR>
<%--
Prepare the inputs and call the bean method to send the
mail using the API

--%>
<jsp:useBean id=&quot;sendMail&quot; class=&quot;mypackage4.SendMail&quot; scope=&quot;page&quot; />
<%
String Sender = request.getParameter(&quot;p_from&quot;);
String Recipient = request.getParameter(&quot;p_to&quot;);
String cc = request.getParameter(&quot;p_cc&quot;);
String bcc = request.getParameter(&quot;p_bcc&quot;);
String subject = request.getParameter(&quot;p_subject&quot;);
String Body = request.getParameter(&quot;p_message&quot;);
String ErrorMessage = request.getParameter(&quot;p_ErrorMessage&quot;);
String Attachments = request.getParameter(&quot;p_filename&quot;);

sendMail.Send(Sender, Recipient, subject, Body, Attachments, ErrorMessage);

%>

<FONT SIZE=3 COLOR=&quot;blue&quot;>
<A HREF=&quot;InputsForm.jsp&quot;>Compose Mail</A>
</CENTER>
</BODY>
</HTML>



=============================================
SendMail.java
=============================================
package mypackage4;

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

public class SendMail {
// Sender, Recipient, CCRecipient, and BccRecipient are comma-
// separated lists of addresses;
// Body can span multiple CR/LF-separated lines;
// Attachments is a ///-separated list of file names;
public static int Send(String Sender,
String Recipient,
String Subject,
String Body,
String Attachments,
String ErrorMessage) {

// Error status;
int ErrorStatus = 0;

//Strings
String SMTPServer = &quot;host&quot;;



// create some properties and get the default Session;
Properties props = System.getProperties();
props.put(&quot;mail.smtp.host&quot;, SMTPServer);
Session session = Session.getDefaultInstance(props, null);

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

// extracts the senders and adds them to the message;
// Sender is a comma-separated list of e-mail addresses as
// per RFC822;
{
InternetAddress[] TheAddresses =
InternetAddress.parse(Sender);
msg.addFrom(TheAddresses);
}

// extract the recipients and assign them to the message;
// Recipient is a comma-separated list of e-mail addresses
// as per RFC822;
{
InternetAddress[] TheAddresses =
InternetAddress.parse(Recipient);
msg.addRecipients(Message.RecipientType.TO,
TheAddresses);
}

// subject field;
msg.setSubject(Subject);

// create the Multipart to be added the parts to;
Multipart mp = new MimeMultipart();

// create and fill the first message part;
{
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(Body);

// attach the part to the multipart;
mp.addBodyPart(mbp);
}

// attach the files to the message;
if (null != Attachments) {
int StartIndex = 0, PosIndex = 0;
while (-1 != (PosIndex = Attachments.indexOf(&quot;///&quot;,
StartIndex))) {
// create and fill other message parts;
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds =
new FileDataSource(Attachments.substring(StartIndex,
PosIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
PosIndex += 3;
StartIndex = PosIndex;
}
// last, or only, attachment file;
if (StartIndex < Attachments.length()) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds =
new FileDataSource(Attachments.substring(StartIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
}

// add the Multipart to the message;
msg.setContent(mp);

// set the Date: header;
msg.setSentDate(new Date());

// send the message;
Transport.send(msg);
} catch (MessagingException MsgException) {
ErrorMessage = MsgException.toString();
Exception TheException = null;
if ((TheException = MsgException.getNextException()) !=
null)
ErrorMessage = ErrorMessage + &quot;\n&quot; +
TheException.toString();
ErrorStatus = 1;
}
return ErrorStatus;
}
}
 
Maybe you can do something with the following code snippets. If not I shall post the complete code.
Code:
...
private Vector to = null;
private Vector cc = null;
private Vector bcc = null;
...
  mimeMessage.addRecipients(Message.RecipientType.TO, this.getTo());
  mimeMessage.addRecipients(Message.RecipientType.CC, this.getCc());
  mimeMessage.addRecipients(Message.RecipientType.BCC, this.getBcc());
...

public MailAddress[] getTo() {
  return toMailAddress(this.to);
}
public MailAddress[] getCc() {
  return toMailAddress(this.cc);
}
public MailAddress[] getBcc() {
  return toMailAddress(this.bcc);
}

private MailAddress[] toMailAddress(Vector vector) {
  return (MailAddress[]) vector.toArray(new MailAddress[vector.size()]);
}
 
Hi hologram,

If I use Vectors, what do I put in my SendMail.jsp page?
I am confused all how it all ties in together. Any detailed example would be greatful.

crguy
 
Forget about the Vector, it is being converted to an array in
Code:
private MailAddress[] toMailAddress(Vector vector) {
  return (MailAddress[]) vector.toArray(new MailAddress[vector.size()]);
}
In our application we parse an XML file for the To, Cc and Bcc, ... fields and add to these to the vector (it's easier then using an array)
====================
Your code seems to be OK, and if it works for 1 TO-address, the problem could be in your Recipient field.
Are you sure your Recipient field &quot;is a comma-separated list of e-mail addresses&quot; ? Can you print it before you parse it ? Can you print &quot;TheAddresses&quot; after the parsing ?
Code:
InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
 
Hi hologram,

I get this error when I try to execute it:
javax.mail.internet.AddressException: Illegal address in string ``john.smith@aol.com,john.smith@aol.com''

I will paste the revised code for SendMail.java any code examples would be great. Thanks in advance.
==========================================
package JavaBean;


import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


public class SendMail {

public static Session getSession()
{
Session session = null;

// Name of the Host Machine
String smtpServer = &quot;mailhost.mcgill.ca&quot;;

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put(&quot;mail.smtp.host&quot;, smtpServer);

// Get session
session = Session.getDefaultInstance(props, null);

return session;
}

public static void send (String from, String[] to, String subject, String filename) throws MessagingException
{
Message message = new MimeMessage(getSession());

message.setFrom(new InternetAddress(from));

InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i = 0; i<to.length; i++)
{
sendTo = new InternetAddress(to);
}

message.setSubject(subject);

// Create the multi-part
Multipart multipart = new MimeMultipart();

// Create part one
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText(&quot;Here is the file&quot;);

// Add the first part
multipart.addBodyPart(messageBodyPart);

//Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);

// Add the second part
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send message
Transport.send(message);
}

} //end of bean



 
Hi hologram,

I am sorry I posted the wrong code above.

package JavaBean;


import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


public class SendMail {
// Sender, Recipient, CCRecipient, and BccRecipient are comma-
// separated lists of addresses;
// Body can span multiple CR/LF-separated lines;
// Attachments is a ///-separated list of file names;
public static int Send(String from,
String to,
String Subject,
String Body,
String Attachments,
String ErrorMessage) {

// Error status;
int ErrorStatus = 0;

//Strings
String SMTPServer = &quot;myhost.com&quot;;



// create some properties and get the default Session;
Properties props = System.getProperties();
props.put(&quot;mail.smtp.host&quot;, SMTPServer);
Session session = Session.getDefaultInstance(props, null);

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

// extracts the senders and adds them to the message;
// Sender is a comma-separated list of e-mail addresses as
// per RFC822;
{
InternetAddress[] TheAddresses =
InternetAddress.parse(from);
msg.addFrom(TheAddresses);
}

// extract the recipients and assign them to the message;
// Recipient is a comma-separated list of e-mail addresses
// as per RFC822;
{
InternetAddress[] TheAddresses =
InternetAddress.parse(to);

msg.addRecipients(Message.RecipientType.TO,
TheAddresses);
}

// subject field;
msg.setSubject(Subject);

// create the Multipart to be added the parts to;
Multipart mp = new MimeMultipart();

// create and fill the first message part;
{
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(Body);

// attach the part to the multipart;
mp.addBodyPart(mbp);
}

// attach the files to the message;
if (null != Attachments) {
int StartIndex = 0, PosIndex = 0;
while (-1 != (PosIndex = Attachments.indexOf(&quot;///&quot;,
StartIndex))) {
// create and fill other message parts;
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds =
new FileDataSource(Attachments.substring(StartIndex,
PosIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
PosIndex += 3;
StartIndex = PosIndex;
}
// last, or only, attachment file;
if (StartIndex < Attachments.length()) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds =
new FileDataSource(Attachments.substring(StartIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
}

// add the Multipart to the message;
msg.setContent(mp);

// set the Date: header;
msg.setSentDate(new Date());

// send the message;
Transport.send(msg);
} catch (MessagingException MsgException) {
ErrorMessage = MsgException.toString();
Exception TheException = null;
if ((TheException = MsgException.getNextException()) !=
null)
ErrorMessage = ErrorMessage + &quot;\n&quot; +
TheException.toString();
ErrorStatus = 1;
}
return ErrorStatus;
}
}







 
Figured it out using JDBC. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top