gpt4 book ai didi

c# - 如何检索 outlook 联系人的图片

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:58 24 4
gpt4 key购买 nike

Microsoft.Office.Interop.Outlook.Items OutlookItems;
Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();
MAPIFolder Folder_Contacts;
Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
OutlookItems = Folder_Contacts.Items;

for (int i = 0; i < OutlookItems.Count; i++)
{
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];

bool to = contact.HasPicture; //returns false although there is a picture in Outlook.
string h = contact.FirstName; //returns my first name
}

为什么上面的代码看不到图片,但是可以拉取名字,如何获取图片。

错误:

enter image description here

enter image description here

最佳答案

Attachment.GetTemporaryFilePath Method (Outlook)

var attachment = contact.Attachments["ContactPicture.jpg"] as Attachment;
var path = attachment.GetTemporaryFilePath();

Attachment.SaveAsFile Method (Outlook)

var attachment = contact.Attachments["ContactPicture.jpg"] as Attachment;
attachment.SaveAsFile(savePath);

注意这里的“路径”包括文件名,而不仅仅是目录。

例子:

var savePath = @"C:\Users\User\Documents\OutlookPhotos\YourDesiredFileName.jpg";

编辑:

这是一个方法。它会为您处理空值。

static void WriteOutlookContactPhotoToFile(ContactItem contact, string directory, string fileName)
{
if (contact != null && contact.HasPicture)
{
var attachment = contact.Attachments["ContactPicture.jpg"] as Attachment;
if (attachment != null)
{
string writePath = Path.Combine(directory, fileName);
attachment.SaveAsFile(writePath);
}
}
}

关于c# - 如何检索 outlook 联系人的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35903185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com