Using JavaMail within WebSphere® Application Server to send HTML messages and attached images
 Technote (troubleshooting)
 
Problem(Abstract)
You are using JavaMail™ within WebSphere® Application Server to send HTML messages and attached images. However, instead of the messages being sent in HTML format, they are sent as plain text. The code you are using works outside the WebSphere Application Server environment.
 
Cause
Excerpt of the relevant code:

Properties mailProps = new Properties();

String mailhost = "mail.attbi.com";
mailProps.put("mail.host", "mail.attbi.com");

Session session = Session.getDefaultInstance(mailProps, null);
MimeMessage mimeMessage = new javax.mail.internet.MimeMessage(session);

String testString = "<H1>Simple message</H1><P><img src=\"http: //www.ibm.com/i/v11/m/en/lanim.gif\">";
mimeMessage.setContent(testString, "text/html");
String subject = "Simple Subject";
mimeMessage.setSubject(subject);

InternetAddress internetaddress = new InternetAddress("AUser@SomeDomain.com");
mimeMessage.setFrom(internetaddress);

mimeMessage.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress("AUser@SomeDomain.com"));
 
Resolving the problem
In all releases of WebSphere Application Server V4 and V5, the integration of JavaMail with the WebSphere Application Server environment is in accordance with the Java™ 2 Platform, Enterprise Edition (J2EE™) specification. The code above, however, is not following the J2EE specification. The application should not create mail sessions directly in the application code. Your WebSphere Application Server administrator should use the Administrative Console to create mail sessions as system resources (or other system management scripting tools) and then have the application make references to those created system resources.
The following example demonstrates how to do it correctly. Note that it also demonstrates an alternative way (using a multipart/related MIME message) to send an image as an attachment, although one can simply refer to them as in, <img src=http: //someserver/image.gif>.

Multipart mp = new MimeMultipart("related");
         
//HTML bodypart      
StringBuffer sb = new StringBuffer();        
sb.append("<HTML> <HEAD> <TITLE>");          
sb.append("JafTestCore #4");    
sb.append("</TITLE> <HEAD> <BODY>");        
sb.append("<H4><U>An important image follows</U></H4>");
sb.append("<img src=\

"

cibd:abcxyz\">"); //referencing the image    
sb.append("</BODY> </HTML>");    
MimeBodyPart bp = new MimeBodyPart();        
bp.setContent(sb.toString(), "text/html");  
mp.addBodyPart(bp);  
         
//Image in a file    
InputStream is = this.getClass().getResourceAsStream("LouGerstner.jpg");  
String fname = System.getProperty("java.io.tmpdir") +    
File.separator+ "LouGerstner.jpg";          
BufferedInputStream bi = new BufferedInputStream(is);    
BufferedOutputStream bo = new BufferedOutputStream(new  
FileOutputStream(fname));        
int ch;  
while ((ch = bi.read()) != -1) { bo.write(ch); }        
bo.close();          
FileDataSource fds = new FileDataSource(fname);          
bp = new MimeBodyPart();        
bp.setDataHandler(new DataHandler(fds));    
bp.setHeader("Content-ID", "abcxyz");        
mp.addBodyPart(bp);  
         
msg.setContent(mp);  
Transport.send(msg);
 
Related information
JavaMail(TM) API - FAQ (Frequently Asked Questions)
JavaMail(TM) API
 
 
Cross Reference information
Segment Product Component Platform Version Edition
Application Servers Runtimes for Java Technology Java SDK
 
 


Document Information


Product categories: Software > Application Servers > Distributed Application & Web Servers > WebSphere Application Server > General
Operating system(s): Windows
Software version: 5.0
Software edition:
Reference #: 1166341
IBM Group: Software Group
Modified date: Jan 19, 2006