将问题保存到目录时遇到问题

时间:2016-01-25 14:54:50

标签: c# api office365 email-attachments email-client

我正在开发一个抓取我的电子邮件并嗅出任何带附件的电子邮件的应用程序。所有附件按照收到的顺序退回。现在我想更进一步,并希望将任何附件保存在本地目录中。我一直在寻找文档或示例,但我已经空了。我将向您展示我的代码片段

此功能将获得电子邮件附件

public static List<IMessage> GetEmailAttachments()
    {
        OutlookServicesClient star_Mail_Box = Start_OutLook_Services();

        try
        {

            var Email_Box = star_Mail_Box.Users["*****@dell.com"].Folders["Inbox"].Messages.Where(m => m.HasAttachments == true).Expand(m => m.Attachments).ExecuteAsync();

            var messages = Email_Box.Result.CurrentPage;

            foreach (var message in messages.OrderByDescending(m=> m.DateTimeReceived))
            {
                var attachments = message.Attachments.CurrentPage;

                foreach (var attachment in attachments)
                {
                  ///This is where I will need to put my Logic.                       
                }
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine("Not Able To Get This Mail Box" + ex.Message + "\n\nDetails : \n\n " + ex.InnerException);
            Console.ReadLine();

        }

        return null; // returning null right now for testing 
    }

1 个答案:

答案 0 :(得分:0)

好的,看完附件定义后,我想我会通过一个字节数组来实现我想要的。这是我的附件循环的一些代码。

foreach (FileAttachment attachment in attachments)
{
    byte[] bytefiles = attachment.ContentBytes;
    string path = @"C:\Top-Level\" + attachment.Name;

    if (!string.IsNullOrEmpty(message.Subject))
    {
        path =  @"C:\Top-Level\" + message.Subject + "." + attachment.ContentType;
    }
    File.WriteAllBytes(path, bytefiles);
}