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="text/html;charset=WINDOWS-1252"%>
<HTML>
<HEAD>
<TITLE>OC4J JSP Sample - Send Email</TITLE>
</HEAD>
<BODY background="../../images/Background.gif">
<CENTER>
<BR>
<%--
Prepare the inputs and call the bean method to send the
mail using the API
--%>
<jsp:useBean id="sendMail" class="mypackage4.SendMail" scope="page" />
<%
String Sender = request.getParameter("p_from"
String Recipient = request.getParameter("p_to"
String cc = request.getParameter("p_cc"
String bcc = request.getParameter("p_bcc"
String subject = request.getParameter("p_subject"
String Body = request.getParameter("p_message"
String ErrorMessage = request.getParameter("p_ErrorMessage"
String Attachments = request.getParameter("p_filename"
sendMail.Send(Sender, Recipient, subject, Body, Attachments, ErrorMessage);
%>
<FONT SIZE=3 COLOR="blue">
<A HREF="InputsForm.jsp">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 = "host";
// create some properties and get the default Session;
Properties props = System.getProperties();
props.put("mail.smtp.host", 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("///",
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 + "\n" +
TheException.toString();
ErrorStatus = 1;
}
return ErrorStatus;
}
}
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="text/html;charset=WINDOWS-1252"%>
<HTML>
<HEAD>
<TITLE>OC4J JSP Sample - Send Email</TITLE>
</HEAD>
<BODY background="../../images/Background.gif">
<CENTER>
<BR>
<%--
Prepare the inputs and call the bean method to send the
mail using the API
--%>
<jsp:useBean id="sendMail" class="mypackage4.SendMail" scope="page" />
<%
String Sender = request.getParameter("p_from"
String Recipient = request.getParameter("p_to"
String cc = request.getParameter("p_cc"
String bcc = request.getParameter("p_bcc"
String subject = request.getParameter("p_subject"
String Body = request.getParameter("p_message"
String ErrorMessage = request.getParameter("p_ErrorMessage"
String Attachments = request.getParameter("p_filename"
sendMail.Send(Sender, Recipient, subject, Body, Attachments, ErrorMessage);
%>
<FONT SIZE=3 COLOR="blue">
<A HREF="InputsForm.jsp">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 = "host";
// create some properties and get the default Session;
Properties props = System.getProperties();
props.put("mail.smtp.host", 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("///",
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 + "\n" +
TheException.toString();
ErrorStatus = 1;
}
return ErrorStatus;
}
}