Problem in sending Email
Hi,
I want to write jsp code which can send emails to multiple recipients with attachment.i had used the follwing code.but the problem with the following code it can supports single recipient and it is unable to send the email body but the attachemnt can send.
<%@ page import=" javax.activation.*, java.util.*, java.io.*, javax.mail.internet.*, javax.mail.* " %>
<html>
<head>
<title>Send an email ans File with jsp page</title>
</head>
<body bgcolor="#C0C0C0" text="#CC0000" >
<%
if(request.getMethod().equals("POST") )
{
boolean status = true;
// enter here the smtp mail server address
// ask your ISP to get the proper name
String mailServer = "mail.nannacomputers.com";
String fromEmail = request.getParameter("from");
String toEmail = request.getParameter("to");
String messageEnter = request.getParameter("message");
String fileName = request.getParameter("theFile");
try
{
Properties props = new Properties();
props.put("mail.smtp.host", mailServer);
Session s = Session.getInstance(props,null);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress(fromEmail);
message.setFrom(from);
InternetAddress to = new InternetAddress(toEmail);
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Send Email with jsp");
//message.setText(messageEnter);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(messageEnter);
// Part two is attachment
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
Transport.send(message);
// ENCTYPE="multipart/form-data"
}
catch (Exception e)
{
System.out.println(e.getMessage() );
out.println("ERROR, your message failed, reason is: " + e);
status = false;
}
if (status == true)
{
out.println("Your message to " + toEmail + " was sent successfully!");
}
}
else
{
%>
<h1>Send email and attatchment file with jsp</h1>
<form method="post" name="mail" action="mailer.jsp">
<table BORDER="0">
<tr>
<td>To :</td>
<td><input type="text" name="to" size=24></td>
</tr>
<p>
<tr>
<td>From :</td>
<td><input type="text" name="from" size=24></td>
</tr>
<p>
<tr>
<td>Message :</td>
<td><TEXTAREA name="message" ROWS = "5" COLS="65"></TEXTAREA></td>
</tr>
<p>
<tr>
<td>File attachment: </td>
<td><input TYPE="FILE" name="theFile"></td>
</tr>
<tr>
<td></td>
<td><input TYPE="submit" value="submit" name="Command"></td>
</tr>
</table>
</form>
<%
}
%>
</body>
</html>
Please provide the solution ASAP
Regards...
Rama
|