GuidesCreating Java Web Applications for Sending Embedded Images

Creating Java Web Applications for Sending Embedded Images

In most of the e-mails that you receive containing images in the HTML body, those images are not visible. Even if you click the show image option that pops up when you right-click on that image, the problem does not resolve. This is because the <img src> or <embed src> tags in HTML do not work with the e-mail body text, so you need to embed these images inside the e-mail body so that they reach the destination safely.

This article will let you create a Java-based Web application to sending embedded images, text, and HTML in an e-mail body without using attachments.

Requirements

This application is built using Eclipse IDE and the JBoss Application server in a Linux environment. You also can run it on Windows or any other platform. It uses JavaMail and other APIs. Download the APIs along with the resource files to their respective directories as explained in this article and follow the described procedure to create a full-fledged running Web application.

Building the Application

  1. Create a new Java Project by the name of “EmbedEmailExample” with the default settings.
  2. The JRE system library (JDK1.x.x) will automatically be included in your system by default in the Eclipse IDE.
  3. Put your source code (.java files) in a source folder and your compiled classes (.class files) in an output folder. Follow these steps to configure your source and output folders:
    1. Right-click on your project in the Package Explorer.
    2. Go to Properties–>Java Build Path.
    3. Click on the Source tab.
    4. Click on Add Folder.
    5. Click on Create New Folder.
    6. Set the folder name to “src”.
    7. Select Yes when it asks you to remove the project as a source folder and to create a “bin” folder.
  4. Next, set your CLASSPATH by defining the libraries (JAR files) that Eclipse should use to compile your code. These jars should be placed in the Project folder or Jboss folder Jboss-x.0.x/server/default/lib (as I did) before navigating to them if they are not present. Add required libraries (jar files) by going to Project–> Properties–> Java Build Path–> Libraries (tab)–> Add External Jars (Button). These are: “mail.jar” (JavaMail API for handling email service), “activation.jar” (JavaActivation API for handling DataSource objects) and “service.jar” (for handling mail connection settings).
  5. Create a new package, “embed.email.utils”, by right-clicking on the “src” folder and selecting New–> Package and “WEB-INF” folder to keeping the *.xml (web configuration files) from New–> Folder.
  6. Now, you create java classes inside the created package “embed.email.utils” by right-clicking New–> Class with the default setting of public modifier and having the superclass of java.lang.object as follows: “EmailSenderManager.java” (Listing 1), “EmailTemplateManager.java” (Listing 2), “FileConfigManager.java” (Listing 3), and “FileHelper.java” (Listing 4).

Listing 1: EmailSenderManager.java

package embed.email.utils;

import java.io.File;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.Multipart;
import javax.mail.BodyPart;
import javax.activation.*;
import com.pix.service.ServiceLocator;

import embed.email.utils.FileConfigManager;

public class EmailSenderManager {

private static final String JAVAMAIL_SESSION =
   "java:comp/env/mail/EmbeddedMailSession";
private static EmailSenderManager manager;
protected static final boolean DEBUG = true;
protected Properties mailProperties;

private EmailSenderManager() { }

public static final EmailSenderManager getInstance() {
   if (manager == null) {
      manager = new EmailSenderManager();
   }
   return manager;
}

public void send(Vector toList, String subject, String body) throws
   Exception {

   if( !toList.isEmpty() ){

      Session session = ServiceLocator.getInstance().
         getJavaMailSession(JAVAMAIL_SESSION);
      session.setDebug(DEBUG);
      Message msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress(""));
      String toAddress = "";
      for (int i = 0; i < toList.size(); i++) {
        toAddress += toList.elementAt(i).toString() + ",";
      }
      toAddress = toAddress.substring(0, toAddress.length() - 1);

      msg.setRecipients(Message.RecipientType.BCC,
                        InternetAddress.parse(toAddress));

      msg.setSubject(subject);
      msg.setText(body);
      msg.setContent(body, "text/html");
      msg.setSentDate(new Date());

      Transport.send(msg);
      if (DEBUG) {
         System.out.println("Message sent OK.");
      }
   }
}

public void sendTextAndHtml(Vector toList, String subject,
                            String textBody, String htmlBody)
   throws Exception {

   if( !toList.isEmpty() ){

      Session session = ServiceLocator.getInstance().
         getJavaMailSession(JAVAMAIL_SESSION);
      session.setDebug(DEBUG);

      Message msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress(
         "YourSenderAdress@YourMailServer.com"));
      String toAddress = "";
      //YourRecipientAddresses are defined in the jsp file as a
      //Vector field.
      for (int i = 0; i < toList.size(); i++) {
        toAddress += toList.elementAt(i).toString() + ",";
      }
      toAddress = toAddress.substring(0, toAddress.length() - 1);

      msg.setRecipients(Message.RecipientType.BCC,
                        InternetAddress.parse(toAddress));

      msg.setSubject(subject);

      // Create an "Alternative" Multipart message
      Multipart mp = new MimeMultipart("alternative");

      // for the image part
      BodyPart embedImage1=new MimeBodyPart();
      DataSource ds1=new FileDataSource(new File(
         FileConfigManager.getTemplatesPath() + "logo.jpg"));
      embedImage1.setDataHandler(new DataHandler(ds1));
      embedImage1.setHeader("Content-ID","<logo1>"); 
      /*This cid (content-ID) is passed to the jsp page in the
       *<img src> tag for embedding */
      mp.addBodyPart(embedImage1);

      // for the image part
      BodyPart embedImage2=new MimeBodyPart();
      DataSource ds2=new FileDataSource(new File(FileConfigManager.
         getTemplatesPath() + "logo.gif"));
      embedImage2.setDataHandler(new DataHandler(ds2));
      embedImage2.setHeader("Content-ID","<logo2>");
      mp.addBodyPart(embedImage2);

      // for the text part
      BodyPart bp1 = new MimeBodyPart();
      bp1.setContent(textBody, "text/plain");
      mp.addBodyPart(bp1);

      // for the html part
      BodyPart bp2 = new MimeBodyPart();
      bp2.setContent(htmlBody, "text/HTML");
      mp.addBodyPart(bp2);

      msg.setContent(mp);
      msg.setSentDate(new Date());

      Transport.send(msg);

      if (DEBUG) {
         System.out.println("Message sent OK.");
      }
   }
}
}

Listing 2: EmailTemplateManager.java

package embed.email.utils;

import embed.email.utils.FileConfigManager;
import embed.email.utils.FileHelper;

public class EmailTemplateManager {
   protected static final String
   SHARING_EMAIL_HTML = "SharingEmail.html";
protected static final String
   SHARING_EMAIL_TXT = "SharingEmail.txt";

   private static EmailTemplateManager manager;

protected String TemplateText_html;
protected String TemplateText_txt;

private EmailTemplateManager() throws Exception {

   TemplateText_html = FileHelper.
      loadFromFile(FileConfigManager.getTemplatesPath() +
                   SHARING_EMAIL_HTML);
   TemplateText_txt = FileHelper.
      loadFromFile(FileConfigManager.getTemplatesPath() +
                   SHARING_EMAIL_TXT);

}

public static EmailTemplateManager getInstance() throws Exception {
  if (manager == null) {
    manager = new EmailTemplateManager();
  }
  return manager;
}

public String getEmailTextForSharingEmail_txt(String text_txt,
   String find_txt, String replace_txt) {

   String mailText;

      if(text_txt=="")
         mailText = TemplateText_txt;
      else
         mailText = text_txt;

      text_txt = mailText.replace(find_txt, replace_txt);
      System.out.println(text_txt);

   return text_txt;
}

public String getEmailTextForSharingEmail_html(String html_txt,
   String find_txt, String replace_txt) {
      String mailHtml;

         if(html_txt=="")
            mailHtml = TemplateText_html;
         else
            mailHtml = html_txt;

         //
         html_txt = mailHtml.replace(find_txt, replace_txt);
         //Print on console
         System.out.println(html_txt);

      return html_txt;
   }

}

Listing 3: FileConfigManager.java

package embed.email.utils;
/**
 * This class is used to save and load system configurations saved
 * in XML files.
 */
public class FileConfigManager    {

   private static String templatePath;
   private static FileConfigManager fileConfigManager;

   static {

      fileConfigManager = new FileConfigManager();
   }

   private FileConfigManager() {
      String jboss = System.getProperties().
         getProperty("jboss.server.home.url");
      if(jboss.lastIndexOf("file:")!=-1 ) {
         jboss = jboss.substring(5);
      }
      /* Place all the resource files (images, text and html) in
       * this "resources" folder in the Jboss default directory */
      templatePath =jboss + "resources/";
}

Listing 4: FileHelper.java

package embed.email.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/*
 * File handling library.
 */

public class FileHelper {
   private static char[] loadData(String fileName) throws Exception {
      char[] data = null;

      File f = new File(fileName);
      BufferedReader fileReader =
         new BufferedReader(new FileReader(f));
      data = new char[(int)f.length()];
      fileReader.read(data);
      fileReader.close();
      return data;
   }

   /**
    * Loads the file given by <code>fileName</code> into a
    * <code>String </code>
    * and returns it.
    */

   public static String loadFromFile(String fileName)
      throws Exception {
      return new String(loadData(fileName));
   }

   /**
    * Loads the file given by <code>fileName</code> into the
    * <code>targetBuffer</code>.
    */
   public static void loadFromFile(String fileName,
                                   StringBuffer targetBuffer)
      throws Exception {

      targetBuffer.append(loadData(fileName));
   }
}
  1. Create a new folder, “pages,” to keep all the *.jsp files for this project.
  2. Create a new folder, “resources,” to keep all the images and other material (*.html pages and *.txt files) and place it in Jboss-x.0.x/server/default folder.
  3. Create a file named “mail-service.xml” and place it in the folder Jboss-x.0.x/server/default/deploy that contains all the mail settings shown in Listing 5.

Listing 5: mail-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server>
<!-- $Id: mail-service.xml,v 1.5.6.1 2005/06/01 06:20:43 starksm
        Exp $ -->

<server>
   <!-- ====================================================== -->
   <!-- Mail Connection Factory -->
   <!-- ====================================================== -->
   <mbean code="org.jboss.mail.MailService"
          name="jboss:service=Mail">
      <attribute name="JNDIName">java:/Mail</attribute>
      <attribute name="User">YourUserName</attribute>
      <attribute name="Password">YourPassword</attribute>
      <attribute name="Configuration">
         <!-- Test -->
         <configuration>
            <!-- Change to your mail server protocol -->
            <property name="mail.store.protocol" value="pop3"/>
            <property name="mail.transport.protocol" value="smtp"/>

            <!-- Change to the user who will receive mail -->
            <property name="mail.user" value="nobody"/>

            <!-- Change to the mail server -->
            <property name="mail.pop3.host"
                      value="pop3.YourPopServer.com"/>

            <!-- Change to the SMTP gateway server -->
            <property name="mail.smtp.host"
                      value="smtp.YourSMTPServer.com"/>

            <!-- Change to the address mail will be from -->
            <property name="mail.from"
                      value="YourUserName@YourMailServer.com"/>
            <!-- Enable debugging output from the javamail classes -->
            <property name="mail.debug" value="true"/>
         </configuration>
         <depends>jboss:service=Naming</depends>
      </attribute>
   </mbean>

</server>
  1. Now, create the “EmbedEmail.jsp” file (Listing 6) inside the “pages” folder that you created.

Listing 6: EmbedEmail.jsp

<%@ page import="java.util.*,java.io.*" %>
<%@ page import="embed.email.utils.*" %>

<%! static int count= 1; %>
<%

   // Email recipients list
   Vector email_list = new Vector();
      email_list.add("YourRecipientAddress@example.com");

      // Subject of Email
   String str_subject, str_txt, str_html;
   str_subject = "Embedded Email Demo";

      //Text body of Email
      str_txt = "Hi, i am sharing this embedded email example with you";
      str_txt = EmailTemplateManager.getInstance().
         getEmailTextForSharingEmail_txt("","TextGoesHere",str_txt);

      //Html body of Email
      str_html = EmailTemplateManager.getInstance().
         getEmailTextForSharingEmail_html("","TextGoesHere",str_txt);
      str_html = EmailTemplateManager.getInstance().
         getEmailTextForSharingEmail_html(str_html,"Picture1",
         "<img src="cid:logo1" HEIGHT=100 WIDTH=144>");
      str_html = EmailTemplateManager.getInstance().
         getEmailTextForSharingEmail_html(str_html,"Picture2",
         "<img src="cid:logo2" HEIGHT=100 WIDTH=144>");

      //Display the html page in the browser
      out.print("<h1>Congratulations!</h1><br><h2>Embedded email is
         successfully sent.</h2> <h3>Please check your email.</h3>");

      //Send embedded email containing images, text and html
      EmailSenderManager.getInstance().sendTextAndHtml(email_list,
         str_subject, str_txt ,str_html);

%>
  1. Finally, create the configuration files “web.xml” (Listing 7) and “jboss-web.xml” (Listing 8) inside the src/WEB-INF folder.

Listing 7: web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
   PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
   "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app

   <display-name>Embedded Email Demo</display-name>

   <!-- Standard Action Servlet Configuration (with debugging) -->

   <resource-ref>
      <res-ref-name>mail/EmbeddedMailSession</res-ref-name>
      <res-type>javax.mail.Session</res-type>
      <res-auth>Container</res-auth>
   </resource-ref>

</web-app>

Listing 8: jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web
   PUBLIC "-//JBoss//DTD Web Application 2.3V2//EN"
   "http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd">

<jboss-web>
   <resource-ref>
      <res-ref-name>mail/EmbeddedMailSession</res-ref-name>
      <jndi-name>java:/Mail</jndi-name>
   </resource-ref>

</jboss-web>

Setting Up the Packaging Configuration

Before you can deploy your application to JBoss, you need to define the structure of your WAR file through a Packaging Configuration, which you then run to create a WAR file. Here’s how to create a Packaging Configuration:

  1. Right-click on your project in the Package Explorer.
  2. Select Properties–>Packaging Configurations.
  3. Right-click in the right frame and click Add Std. Archive.
  4. Select Standard-WAR.war and click OK.
  5. Right-click on the configuration and click Edit.
  6. Rename it to embed.war.
  7. Expand the configuration.
  8. Right-click on the line with Manifest.MF and remove it.
  9. Click OK and you should see a file in your project called packaging-build.xml. Add the following lines in this file to include the pages folder that you created in the deployment package.
<zipfileset dir="bin" prefix="WEB-INF/classes" includes="**/*.class"/>
   <zipfileset dir="pages" prefix="pages">
      <include name="*.jsp"/>
</zipfileset>

Creating and Deploying the WAR File

Create a WAR file by right-clicking on your project and then clicking Run Packaging. You will have to right-click on the project and click Refresh before you see the WAR file. The file should be in the top level of your project.

Right-click on the WAR file, select Deployment, and then Deploy To. You will see a Target Choice dialog appear, allowing you to select which application server you would like to deploy to. I have JBoss 4.0.4 configured on my machine. After selecting the target configuration, you should see a dialog that confirms that the application was deployed.

Figure 1 shows the directory structure of the project in the package explorer view in Eclipse. Try to keep the structure similar to what is shown in the figure to avoid any errors and conflicts when following the steps in the article.

Figure 1: View of the Package Explorer

Running Your Application in a Browser

Now, open your Web browser and try it out. Type http://127.0.0.1:8080/embed/pages/EmbedEmail.jsp or http://localhost:8080/embed/pages/EmbedEmail.jsp. to run the Web application you just built.

Conclusion

I hope this application has let you practice making a Web-based application with an e-mail facility very quickly and easily. It can be modified and enhanced by the readers according to their needs.

About the Author

Fatima Ahmed has been working on C#, JSP, Servlets and Java programming since 2002 and also teaches an object-oriented course in the Computer Science Department at a university using the Java and C# languages. She likes to share her knowledge with others; therefore, she frequently writes articles and papers. Her interests include learning, teaching, adventure, and traveling.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories