简体   繁体   English

将电子邮件附件保存到目录时出现问题

[英]Having issues saving email attachment to directory

I am working on an application that crawls my email and sniffs out any emails with attachments. 我正在开发一个可抓取我的电子邮件并嗅出所有带有附件的电子邮件的应用程序。 All attachment are being returned in the order they were received. 所有附件均按收到顺序返回。 Now I want to go a step further and would like to save any attachments in a local directory. 现在,我想更进一步,想将所有附件保存在本地目录中。 I have been looking for documentation or examples but I have come up empty. 我一直在寻找文档或示例,但是我空着。 I will show you a snippet of my code 我将向您展示我的代码片段

This Function will get Email Attachments 此功能将获得电子邮件附件

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 
    }

Ok so after looking at the attachment definition I figured I go through a byte array to achieve what I want. 好了,在查看了附件定义之后,我发现我要遍历一个字节数组来实现我想要的。 Here goes the Some code for my attachment loop. 这是我的附件循环的一些代码。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM