Home : Forward, Copy, or Move a Message
Q183239 - HOWTO: Forward, Copy, or Move a Message

How to Forward, Copy, or Move a Message

The following piece of code demonstrates how to Forward, Copy, or Move the most recent message in a user's Inbox. Each independent action (Forward, Copy, and Move) is clearly commented for ease of use and illustration. Please note that the above actions are presented together only for the sake of efficiency, and are not dependent upon one another for proper functionality. For more information about Message objects, please refer to the Microsoft CDO reference. Before running this code, please ensure that CDO has first been configured.

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

public class ForwardCopyMoveMessage {

    public static void main(String[] args) {

        // TODO: Change the following parameters based on your setup and configuration
        // the logon parameters of the service account you used to configure CDO
        String domain           = "domain";
        String username         = "username";
        String password         = "password";
        // the DNS name or IP address of the machine where you installed CDO
        String CDOmachine       = "0.0.0.0";
        // the DNS name or IP address of the Exchange Server
        String exchangeServer   = "0.0.0.0";
        // the mailbox you wish to access (NOTE: this is the same as the user's login name
        // e.g., if the username is jsmith, mailbox would also be jsmith )
        String mailbox          = "mailbox";

        try {
            // use credentials of service account to 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);
            // print the current user for this Session.
            AddressEntry user = new AddressEntryProxy(session.getCurrentUser());
            System.out.println("Current user: " + user.getName());
   
            // 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());
            // get most recent message in the collection.
            Message message = new MessageProxy(messages.getLast(null));
            // print out message for confirmation.
            String body = message.getText().toString();
            String subject = message.getSubject().toString();
            System.out.println("BODY:\n" + body +"\n");
   
            // ***** The following demonstrates how to: *****
            // ***** FORWARD A MESSAGE *****
            // modify forwarding message details.
            Message forwardMessage = new MessageProxy(message.forward());
            String forwardMsg = "ENTER FORWARDING MESSAGE HERE";
            forwardMessage.setSubject("FW: " + subject);
            forwardMessage.setText(forwardMsg + "\n\n------- Original Message -------\n\n" + body);
            // set message recipients.
            Recipients recipients = new RecipientsProxy(forwardMessage.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("[email protected]");
            r2.setName("[email protected]");
            // 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));
            // save changes to the message and send
            forwardMessage.update(new Boolean(true), new Boolean(true));
            forwardMessage.send(null, null, null);
            
            // ***** The following demonstrates how to: *****
            // ***** COPY A MESSAGE *****
            // get destination folder's unique string ID
            Integer newFolderType = new Integer(CdoDefaultFolderTypes.CdoDefaultFolderDeletedItems);
            Folder deletedItems = new FolderProxy(session.getDefaultFolder(newFolderType));
            String deletedItemsFolderID = deletedItems.getID().toString();
            // copy message
            Message copiedMessage = new MessageProxy(message.copyTo(deletedItemsFolderID, null));
            copiedMessage.update(new Boolean(true), new Boolean(true));
   
            // ***** The following demonstrates how to: *****            
            // ***** MOVE A MESSAGE *****
            // move original message to the Deleted Items folder
            message.moveTo(deletedItemsFolderID, null);

            // log off from the session
            session.logoff();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // clean up and release all references
            com.linar.jintegra.Cleaner.releaseAll();
        }
    }
}

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 3/23/2007.
Last Modified on 11/1/2007.
Last Modified by J-Integra KB Admin.
Article has been viewed 5317 times.
Rated 5 out of 10 based on 18 votes.
Print Article
Email Article