Home : Send an HTML Message
Q183243 - HOWTO: Send an HTML Message

How to Send an HTML Message

The following example demonstrates a workaround to send an HTML message using CDO (which does not support the automatic generation of HTML for HTML messages) and J-Integra for Exchange. The code below writes the HTML information to one of the Message object's fields. For more information about Field objects, please refer to the Microsoft CDO reference. Before running this code, please ensure that CDO has first been configured.

Java Application Code (HTMLTest.java):

import com.linar.jintegra.Cleaner;
import com.intrinsyc.cdo.*;
import java.io.*;


public class HTMLTest {

    public static void main(String[] args) {
	//TODO: Change the following parameters based on your setup and configuration
        String domain           = "domain";
        String username         = "username";
        String password         = "password";
        String CDOmachine       = "0.0.0.0";
        String exchangeServer   = "0.0.0.0";        
        String mailbox          = "mailbox";
		
	String tempFile = "HTMLTest.txt";
		
	try {
	    // authenticate to Windows
            com.linar.jintegra.AuthInfo.setDefault(domain, username, password);

            // create a Session in the CDOmachine
            Session session = new Session(CDOmachine);

            // logon to the Exchange Server
            session.logon(null, null, new Boolean(false), new Boolean(true),
                          new Boolean(false), new Boolean(false),
                          exchangeServer + "\n" + mailbox);

            // create a message in the Outbox
            Integer folderType = new Integer(CdoDefaultFolderTypes.CdoDefaultFolderOutbox);
            Folder outbox = new FolderProxy(session.getDefaultFolder(folderType));
            Messages messages = new MessagesProxy(outbox.getMessages());
            Message message = new MessageProxy(messages.add(null, null, null, null));

            // set message subject and body
            message.setSubject("HTML Message Test");
            message.setText("This is an HTML message sent via J-Integra! Note the bold and italics.");			
			
	    // read in HTML source from file.
	    BufferedReader input = null;
	    input = new BufferedReader(new FileReader(tempFile));
	    boolean EOF = false;
	    String HTML = "";
	    while (!EOF) {
		String line = input.readLine();
		if (line != null) {
		    HTML = HTML + line;
		} else {
		    EOF = true;
		}
	    }
	    input.close();
			
	    // convert HTML source to byte array.
	    System.out.println("HTML source: " + HTML + "\n");
	    byte[] bytes = HTML.getBytes();			
			
	    // convert byte array to hexadecimal string.
	    String hexString = bytesToHex(bytes);
	    System.out.println("Hex value: " + hexString);			
			
	    // write the hex string to the specified field
	    Fields fds = new FieldsProxy(message.getFields());
	    Field fd = new FieldProxy(fds.add(new Integer(269680898), (hexString), null, null));				
			
	    // set message recipients
            Recipients recipients = new RecipientsProxy(message.getRecipients());
            Recipient r1 = new RecipientProxy(recipients.add(null, null, null, null));
            
            // set recipients' addresses
            r1.setName("[email protected]");            
            r1.setType(new Integer(CdoRecipientType.CdoTo));            
            r1.resolve(new Integer(0));            

            // save changes to the message			
            message.update(new Boolean(true), new Boolean(true));

            // and finally, send the message
            message.send(null, null, null);

            // logoff from the session
            session.logoff();					
			
	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    Cleaner.releaseAll();
	}		
    }	
	
    // simple byte[]-to-hex conversion methods obtained via google search.
    /**
    * Convenience method to convert a byte to a hex string.
    *
    * @param data the byte to convert
    * @return String the converted byte
    */
    public static String byteToHex(byte data) {
	StringBuffer buf = new StringBuffer();
	buf.append(toHexChar((data>>>4)&0x0F));
	buf.append(toHexChar(data&0x0F));
	return buf.toString();
    }

    /**
    * Convenience method to convert a byte array to a hex string.
    *
    * @param data the byte[] to convert
    * @return String the converted byte[]
    */
    public static String bytesToHex(byte[] data) {
	StringBuffer buf = new StringBuffer();
	for (int i = 0; i < data.length; i++) {
	    buf.append(byteToHex(data[i]));
	}
	return buf.toString();
    }

    /**
    * Convenience method to convert an int to a hex char.
    *
    * @param i the int to convert
    * @return char the converted char
    */
    public static char toHexChar(int i) {
	if ((0 <= i) && (i <= 9 ))
	    return (char)('0' + i);
	else
	    return (char)('a' + (i-10));
    }	
}

Sample HTML Source (HTMLTest.txt):

<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<style>
<!--
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman";}
a:link, span.MsoHyperlink
	{color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:Arial;
	color:windowtext;}
@page Section1
	{size:8.5in 11.0in;
	margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
	{page:Section1;}
-->
</style>

</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>This is an <b>HTML message</b> sent via J-Integra! Note the <b>bold</b> and <i>italics</i>.<o:p></o:p></span></font></p>

</div>

</body>

</html>

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 1/9/2008.
Last Modified on 1/9/2008.
Last Modified by J-Integra KB Admin.
Article has been viewed 9897 times.
Rated 4 out of 10 based on 23 votes.
Print Article
Email Article