使用应用程序,我正在使用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);
}
}
答案 0 :(得分:0)
重点在于您的代码调用:
fldContacts = (OutLook.MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
在这里,您始终打开默认文件夹。
您应该尝试枚举可用文件夹并调用Session
对象的另一个方法来打开您需要使用的文件夹。
我现在不知道答案,但这是方向。