Make your own free website on Tripod.com

Visit Gopalan Suresh Raj's Web Cornucopia...

Developing the MultiPart E-Mail Message Sender Bean
Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................JDK 1.1.7 or higher

To work with the Servlets and JSP, you will need
.........................................
JSWDK 1.0.1 or higher

To work with the Java Mail API, you will need
.........................................
JavaMail 1.1.3 or higher
.........................................
Java Activation Framework 1.0.1 or higher
.........................................
POP3 Provider 1.1.1 or higher

1. Define your SendMultipartMessage class

Develop the Bean that actually constructs the Multipart Mime Message and actually sends out the email. Compile the file and create the class file.

The JSP Model 2 Architecture
(Model-View-Controller Based)

Figure shows what we are trying to ultimately accomplish in these pages here.

EmailHandler\SendMultipartMessage.java
package EmailHandler;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
 
public class SendMultipartMessage implements java.io.Serializable { 
 
 private EmailContents m_contents;
 
 public SendMultipartMessage (EmailContents contents) {
  m_contents = contents;
 }
 
 public EmailContents getContents () {
  return m_contents;
 }
 
 public boolean sendMail (String hostName) {

  boolean result = true;
  String element = null;
  
  Properties prop = new Properties();
  prop.put("mail.smtp.host", hostName);
  Session session = Session.getDefaultInstance(prop, null);
  
  
  
  try {
   Message msg = new MimeMessage(session);

   InternetAddress from = new InternetAddress(m_contents.getFrom());
   msg.setFrom(from);
   
   int count = 0;

   Vector toList = (Vector)((Vector)m_contents.getTo()).clone ();
   InternetAddress to[] = new InternetAddress [toList.size ()];
   for (count = 0; count < toList.size (); count++) {
    element = (String)toList.elementAt (count);
    System.out.println("mail number "+count+" to:"+ element);
    to [count] = new InternetAddress (element);
   }
   msg.setRecipients(Message.RecipientType.TO, to);

   Vector ccList = (Vector)((Vector)m_contents.getCc()).clone ();
   InternetAddress cc[] = new InternetAddress [ccList.size ()];
   for (count = 0; count < ccList.size (); count++) {
    element = (String)ccList.elementAt (count);
    System.out.println("mail number "+count+" cc:"+ element);
    cc [count] = new InternetAddress (element);
   }
   msg.setRecipients(Message.RecipientType.CC, cc);
   
   Vector bccList = (Vector)((Vector)m_contents.getBcc()).clone ();
   InternetAddress bcc[] = new InternetAddress [bccList.size ()];
   for (count = 0; count < bccList.size (); count++) {
    element = (String)bccList.elementAt (count);
    System.out.println("mail number "+count+" bcc:"+ element);
    bcc [count] = new InternetAddress (element);
   }
   msg.setRecipients (Message.RecipientType.BCC, bcc);

   msg.setSubject (m_contents.getSubject ());

   MimeBodyPart mimeBodyPart = new MimeBodyPart ();
   mimeBodyPart.setContent (m_contents.getBody (), "text/html");

   Multipart multiPart = new MimeMultipart ();
   multiPart.addBodyPart (mimeBodyPart);

   msg.setContent (multiPart);

   Transport.send (msg);
  } catch(MessagingException me) {
   me.printStackTrace();
   result = false;
  }
  return result;
 }
}

2. Create a Directory called "EmailHandler" on the "jswdk-1.0.1\examples\WEB-INF\jsp\beans\" directory and Copy this JSP page there.

click here to go to
My JSP/Servlets HomePage...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He is contributing author to a couple of books "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (http://www.execpc.com/~gopalan) or mail him at gopalan@execpc.com.

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since September 21,1998.

Last Updated : Dec 19, '98

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2000, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.