开发者

Need to access Outlook contact details from multiple Mail IDs using C#

Using an application, I'm fetching the contact details stored in the Micosoft Office Outlook using C#. I achieved it through the Microsoft.Office.Interop namespace.

The issues I'm facing now is when Outlook has multiple mail IDs configured to the same system I need to fetch contacts of the Individual mail IDs seperately. How do I do this?

Here is the sample code:

/// <summary>
/// Getting the contacts by passing the folder name.
/// </summary>
/// <param name="folderName"></param>
/// <returns></returns>
private List<MyContact> GetContactsFromFolder(string folderName)
{
    List<MyContact> contacts = null;
    object missing = System.Reflection.Missing.Value;

    //Create instance of Outlook application and Outlook Contacts folder.
    try
    {
        OutLook.MAPIFolder fldContacts = null;
        contacts = new List<MyContact>();
        OutLook._Application outlookObj = new OutLook.Application();
        /* if (folderName == "Default")
        {
            fldContacts = (OutLook.MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
        }
        else
        {
            OutLook.MAPIFolder contactsFolder = (OutLook.MAPIFolder)
            outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            //VERIFYING THE CUSTOM FOLDER IN OUT LOOK .
            foreach (OutLook.MAPIFolder subFolder in contactsFolder.Folders)
            {
                if (subFolder.Name == folderName)
                {
                    fldContacts = subFolder;
                    break;
                }
            }
        }
        * */

        fldContacts = (OutLook.MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

        //LOOPIN G THROUGH CONTACTS IN THAT FOLDER.
        foreach (Microsoft.Office.Interop.Outlook._MailItem contactItem in fldContacts.Ite开发者_如何转开发ms)
        {
            MyContact contact = new MyContact();

            contact.FromAddress = contactItem.SenderEmailAddress;
            contact.ToAddress = contactItem.To;
            contact.Subject = contactItem.Subject;
            contact.MailSize = contactItem.Size.ToString();
            contact.Received = contactItem.ReceivedTime.ToString();

            System.Data.SqlClient.SqlConnection con;
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = "Initial Catalog=sample;Integrated Security=True;Server=Test;Connect Timeout=900";
            try
            {
                con.Open();
                string to_address = "";
                string Cc = "";

                foreach (Microsoft.Office.Interop.Outlook.Recipient olRecipient in contactItem.Recipients)
                {
                    if (contactItem.To.ToLower().Contains(olRecipient.Address.ToLower()) == true ||
                        contactItem.To.ToLower().Contains(olRecipient.Name.ToLower()) == true)
                    {
                        if (to_address != "")
                        {
                            to_address = to_address + ";" + olRecipient.Name + " <" + olRecipient.Address + ">";
                        }
                        else
                        {
                            to_address =olRecipient.Name+" <"+olRecipient.Address+">";
                        }
                    }
                    else
                        if (contactItem.CC != null && contactItem.CC.ToString() != "")
                        {
                            if (contactItem.CC.ToLower().Contains(olRecipient.Address.ToLower()) == true ||
                                contactItem.CC.ToLower().Contains(olRecipient.Name.ToLower()) == true)
                            {
                                if (Cc != "")
                                {
                                    Cc = Cc + ";" + olRecipient.Name + " <" + olRecipient.Address + ">";
                                }
                                else
                                {
                                    Cc = olRecipient.Name + " <" + olRecipient.Address + ">";
                                }
                            }
                        }
                }
                //contact.
                contacts.Add(contact);
            }
        }


The point is in this call of your code:

fldContacts = (OutLook.MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

Here you are always opening the default folder.

You should try to enumerate the available folders and call another method of the Session object to open the one you need to use.

I don't know the answer right now, but this is the direction to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜