需要使用 C# 从多个邮件 ID 访问 Outlook 联系人详细信息
使用应用程序,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重点在于您的代码调用:
在这里您始终打开默认文件夹。
您应该尝试枚举可用的文件夹并调用 Session 对象的另一种方法来打开您需要使用的文件夹。
我现在不知道答案,但这就是前进的方向。
The point is in this call of your code:
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.