|
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("user@company.com");
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));
}
} |
|