我使用EWS API 1.2访问Exchange Server上的邮箱。 这只是工作正常,但有一件事我无法实现:获取邮件附件。
我写了以下几行:
class Program
{
public static void Main(string[] args)
{
try {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Credentials = new WebCredentials("login","password");
service.AutodiscoverUrl("mail@domaine.fr");
ItemView view = new ItemView(10);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
if (findResults != null && findResults.Items != null && findResults.Items.Count > 0)
foreach (Item item in findResults.Items)
{
if (item.Attachments != null)
{
IEnumerator<Attachment> e = item.Attachments.GetEnumerator();
}
Console.WriteLine(item.Subject);
}
else
Console.WriteLine("no items");
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
我收到了测试邮箱中的所有邮件,但IEnumerator<Attachment> e = item.Attachments.GetEnumerator();
似乎没有“看到”附件。
你知道我错过了什么吗?
非常感谢。
答案 0 :(得分:18)
我终于设法获得了电子邮件附件。我修改了我的代码,如下所示
class Program
{
public static void Main(string[] args)
{
try {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Credentials = new WebCredentials("login","pwd");
service.AutodiscoverUrl("mail@domaine.com");
ItemView view = new ItemView(10);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
if (findResults != null && findResults.Items != null && findResults.Items.Count > 0)
foreach (Item item in findResults.Items)
{
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments));
foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load();
Console.WriteLine("Attachment name: " + fileAttachment.Name);
}
}
Console.WriteLine(item.Subject);
}
else
Console.WriteLine("no items");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}