Messages |
|
Make sure you have completed all the configuration requirements before running any of the examples.
import com.linar.jintegra.Cleaner;
import com.intrinsyc.cdo.*;
public class SendMessageWithAttachment {
public static void main(String[] args) {
//TODO: Change the following parameters based on your setup and configuration
String domain = "mydomain";
String username = "jsmith";
String password = "password";
String CDOmachine = "0.0.0.0";
String exchangeServer = "0.0.0.0";
String mailbox = "jsmith";
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("Test Message");
message.setText("This is a test message sent from a Java program through J-Integra(R).");
// set message recipients
Recipients recipients = new RecipientsProxy(message.getRecipients());
Recipient r1 = new RecipientProxy(recipients.add(null, null, null, null));
Recipient r2 = new RecipientProxy(recipients.add(null, null, null, null));
// set recipients' addresses
r1.setName("msue@jintegra.com");
r2.setName("jdoe@jintegra.com");
// r1 will be the 'to' address and r2 will be 'cc'-ied
r1.setType(new Integer(CdoRecipientType.CdoTo));
r2.setType(new Integer(CdoRecipientType.CdoCc));
// Each recipient added must be resolved before it can be used.
// 0 == false to inhibit dialog boxes from popping up
r1.resolve(new Integer(0));
r2.resolve(new Integer(0));
// set the attachment
Attachments attachments = new AttachmentsProxy(message.getAttachments());
Attachment attachment = new AttachmentProxy(attachments.add(null, null, null, null));
attachment.setSource("c:\\temp\\attach.doc");
// 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 ex) {
ex.printStackTrace();
}finally{
Cleaner.releaseAll();
}
}
}
|
Replying to a message is like creating and sending a new message. The only major difference is, with message replies you have to figure out the e-mail address of the original sender. E-mail address retrieval is explained in this knowledge base article.
// TODO: create and logon to a Session
// retrieve the inbox folder
Integer folderType = new Integer(CdoDefaultFolderTypes.CdoDefaultFolderInbox);
Folder inbox = new FolderProxy(session.getDefaultFolder(folderType));
// get the message collection from the inbox
Messages messages = new MessagesProxy(inbox.getMessages());
// loop through the message collection and print out selected details
int count = ((Integer)messages.getCount()).intValue();
for(int i=1; i<=count; i++){
Message message = new MessageProxy(messages.getItem(new Integer(i)));
// print out the sender's name
AddressEntry sender = new AddressEntryProxy(message.getSender());
String address = sender.getName().toString();
System.out.println("--------------------------------------------------");
System.out.println("FROM: " + address);
// print out the subject
String subject = message.getSubject().toString();
System.out.println("SUBJECT: " + subject);
// print out the date/time the message was received
String receivedOn = message.getTimeReceived().toString();
System.out.println("RECEIVED: " + receivedOn);
// print out the importance level
String importance = message.getImportance().toString();
System.out.println("IMPORTANCE: " + importance);
// print out the sensitivity
String sensitivity = message.getSensitivity().toString();
System.out.println("SENSITIVITY: " + sensitivity);
// print out the message body
String body = message.getText().toString();
System.out.println("BODY: " + body);
} |
CDO provides a MessageFilter object which you can use to filter/search messages by different criteria. Check out this knowledge base article for an example.