http://www.developer.com/tech/article.php/3626951/Creating-Java-Web-Applications-for-Sending-Embedded-Images.htm
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. 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. Listing 1: EmailSenderManager.java Listing 2: EmailTemplateManager.java Listing 3: FileConfigManager.java Listing 4: FileHelper.java Listing 5: mail-service.xml Listing 6: EmbedEmail.jsp Listing 7: web.xml
Listing 8: jboss-web.xml 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: 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 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. 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. 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.
Creating Java Web Applications for Sending Embedded Images
August 17, 2006
Requirements
Building the Application
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.");
}
}
}
}
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;
}
}
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/";
}
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));
}
}
<?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>
<%@ 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);
%>
<?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>
<?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
<zipfileset dir="bin" prefix="WEB-INF/classes" includes="**/*.class"/>
<zipfileset dir="pages" prefix="pages">
<include name="*.jsp"/>
</zipfileset>
Creating and Deploying the WAR File

Click here for a larger image.
Running Your Application in a Browser
Conclusion
About the Author