November 24, 2015
Hot Topics:

Creating Java Web Applications for Sending Embedded Images

  • August 17, 2006
  • By Fatima Ahmed
  • Send Email »
  • More Articles »

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().
      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()];
      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 {

  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 2005/06/01 06:20:43 starksm
        Exp $ -->

   <!-- ====================================================== -->
   <!-- Mail Connection Factory -->
   <!-- ====================================================== -->
   <mbean code="org.jboss.mail.MailService"
      <attribute name="JNDIName">java:/Mail</attribute>
      <attribute name="User">YourUserName</attribute>
      <attribute name="Password">YourPassword</attribute>
      <attribute name="Configuration">
         <!-- Test -->
            <!-- 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"

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

            <!-- Change to the address mail will be from -->
            <property name="mail.from"
            <!-- Enable debugging output from the javamail classes -->
            <property name="mail.debug" value="true"/>

  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();

      // 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().

      //Html body of Email
      str_html = EmailTemplateManager.getInstance().
      str_html = EmailTemplateManager.getInstance().
         "<img src="cid:logo1" HEIGHT=100 WIDTH=144>");
      str_html = EmailTemplateManager.getInstance().
         "<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
         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"


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

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



Listing 8: jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web
   PUBLIC "-//JBoss//DTD Web Application 2.3V2//EN"



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"/>

Page 2 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel