Home : How to Retrieve the SMTP Address of the Sender of a Message
Q121665 - HOWTO: How to Retrieve the SMTP Address of the Sender of a Message

How to Retrieve the SMTP Address of the Sender of a Message

 

There are instances when a call to Sender.getAddress() does not output the fully-qualified e-mail address (SMTP address) of the sender of an e-mail message. The following Java program demonstrates how to retrieve the SMTP address of the sender using CDO. The Java code is based on this MSDN article How to Programmatically Retrieve Fully Qualified E-mail Addresses.

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

public class GetSMTPAddressUsingCDO {
    // Modify the following variables based on your specific setup 

    static String domain     = "DOMAIN";    //domain where the CDO host machine belongs
                                            //make sure it's all capitalized 

    static String user       = "username";  //username of Java-Exchange user 

    static String password   = "password";  //password of Java-Exchange user 

    static String mailbox    = "mailbox";   //mailbox of Java-Exchange user 

    static String CDOmachine = "000.000.000.000";   // IP address of the CDO host machine 

    // or name of the machine (e.g. "machine1")
    static String exchangeServer = "000.000.000.000"; // IP address of the Exchange Server 

    // or name of the Exchange Server (e.g. "mailServer")
    private static final int g_PR_SMTP_ADDRESS_W = 972947487; // field ID for SMTP address 

    public static void main(String[] args) {
        try {
            // Authenticate to NT domain via NTLM
            AuthInfo.setDefault(domain, user, password); 

            // Start a session
            Session session = new Session(CDOmachine); 

            // Logon to the Exchange Server
            session.logon(null, null, null, null, null, null,
                          exchangeServer + "\n" + mailbox); 

            // Retrieve messages from Inbox
            Integer inboxID = new Integer(CdoDefaultFolderTypes.CdoDefaultFolderInbox);
            Folder inbox = new FolderProxy(session.getDefaultFolder(inboxID));
            Messages messages = new MessagesProxy(inbox.getMessages()); 

            // Loop through the messages and print out the message subject
            // and the SMTP address of the sender
            int count = ((Integer)messages.getCount()).intValue(); 

            for(int i=1; i<=count; i++){
                Message message = new MessageProxy(messages.getItem(new Integer(i)));
                String subject = message.getSubject().toString();
                System.out.println("--------------------------------------------------");
                System.out.println("SUBJECT: " + subject); 

                AddressEntry sender = new AddressEntryProxy(message.getSender());
                String address = sender.getAddress().toString(); 

                if(address.indexOf("@") < 1 ){ // address is not in SMTP format
                    Fields fields = new FieldsProxy(sender.getFields());
                    Field field = new FieldProxy(fields.getItem(new Integer(g_PR_SMTP_ADDRESS_W), null));
                    address = field.getValue().toString();
                    System.out.println("Retrieving SMTP address through the field property . . . ");
                } 

                System.out.println("SENDER: " + address);
            } 

            // Logoff and release all COM references
            session.logoff();
            Cleaner.releaseAll();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
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 6/23/2006.
Last Modified on 7/10/2006.
Last Modified by No Author Name Available!.
Article has been viewed 4025 times.
Rated 3 out of 10 based on 94 votes.
Print Article
Email Article