How to Retrieve and Store Attachments from a Message
The following pieces of code demonstrate how to extract an attachment (either a data file or an embedded message) from the most recent message in a user's Inbox.  For more information about Attachment objects, please refer to the Microsoft CDO reference.  Before running this code, please configure CDO first.
Retrieving and Storing an Attached Data File:
|  | import com.intrinsyc.cdo.*;
import com.linar.jintegra.Cleaner;
import java.io.*;
public class GetAttachment {
    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());
            Integer count = (Integer)messages.getCount();
            Message message = new MessageProxy(messages.getItem(count));
            String body = message.getText().toString();
            System.out.println("BODY: " + body +"\n");
   
            // process attachment of first message
            Attachments attachments = new AttachmentsProxy(message.getAttachments());   
            if ( ((Integer)attachments.getCount()).intValue() > 0 ) {
                System.out.println("Attachment(s) found!");
                Attachment attachment = new AttachmentProxy(attachments.getItem(new Integer(1)));
   
                String attachmentName = attachment.getName().toString();
                System.out.println("attachmentName: " + attachmentName);
                String writeDirectory = "c:\\GetAttachmentExample\\";  // relative to CDO.dll.
                String fullPath = writeDirectory + attachmentName;
    
                (new File(writeDirectory)).mkdir();
                System.out.println("writeDirectory: " + writeDirectory);    
    
                System.out.println("fullPath: " + fullPath);
                Integer attachmentType = (Integer) attachment.getType();
                System.out.println("attachmentType: " + attachmentType);
                attachment.writeToFile(fullPath);
                System.out.println("success!");
            }   
               
            // log off from the session
            session.logoff();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // clean up and release all references
            com.linar.jintegra.Cleaner.releaseAll();
        }
    }
} |  | 
Before running the above code, please ensure the following:
- Note that the specified write path is always relative to the CDO machine (more specifically, CDO.DLL). 
- Ensure proper access and write privileges are in order (when writing to a non-CDO machine).
Retrieving an Embedded Message:
|  | import com.intrinsyc.cdo.*;
import com.linar.jintegra.Cleaner;
import java.io.*;
public class GetAttachment {
    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());
            Integer count = (Integer)messages.getCount();
            Message message = new MessageProxy(messages.getItem(count));
            String body = message.getText().toString();
            System.out.println("BODY: " + body +"\n");
   
            // process attachment of first message
            Attachments attachments = new AttachmentsProxy(message.getAttachments());   
            if ( ((Integer)attachments.getCount()).intValue() > 0 ) {
                System.out.println("Attachment(s) found!");
                Attachment attachment = new AttachmentProxy(attachments.getItem(new Integer(1)));
   
                String attachmentName = attachment.getName().toString();
                System.out.println("attachmentName: " + attachmentName);
                Integer attachmentType = (Integer) attachment.getType();
                System.out.println("attachmentType: " + attachmentType);
                Message embeddedMsg = new MessageProxy(attachment.zz_getSource());
                String embeddedBody = embeddedMsg.getText().toString();
                System.out.println("Embedded Body:\n" + embeddedBody);
            }   
               
            // log off from the session
            session.logoff();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // clean up and release all references
            com.linar.jintegra.Cleaner.releaseAll();
        }
    }
} |  |