Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 24th, 2006, 12:07 AM
Registered User
 
Join Date: Apr 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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






Similar Threads
Thread Thread Starter Forum Replies Last Post
Email Sending Problem dharmeshtandel Forum and Wrox.com Feedback 1 April 30th, 2008 08:46 AM
Email sending problem [email protected] ASP.NET 1.0 and 1.1 Basics 3 December 21st, 2006 01:16 PM
problem sending an email leo_vinay SQL Server 2000 3 July 23rd, 2006 12:55 PM
Email Sending Problem.. gaurav_jain2403 VB.NET 2002/2003 Basics 2 February 4th, 2006 09:08 AM
sending email problem maitias C# 1 February 3rd, 2006 01:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.