需要使用 C# 从多个邮件 ID 访问 Outlook 联系人详细信息

发布于 2024-11-29 08:43:28 字数 4220 浏览 1 评论 0原文

使用应用程序,我使用 C# 获取存储在 Micosoft Office Outlook 中的联系人详细信息。我通过 Microsoft.Office.Interop 命名空间实现了它。

我现在面临的问题是,当 Outlook 将多个邮件 ID 配置到同一系统时,我需要分别获取各个邮件 ID 的联系人。 我该怎么做?

这是示例代码:

/// <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.Items)
        {
            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);
            }
        }

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.Items)
        {
            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);
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2024-12-06 08:43:28

重点在于您的代码调用:

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

在这里您始终打开默认文件夹。

您应该尝试枚举可用的文件夹并调用 Session 对象的另一种方法来打开您需要使用的文件夹。

我现在不知道答案,但这就是前进的方向。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文