dcsimg
June 23, 2018
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().
         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>




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.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date