簡體   English   中英

將電子郵件附件保存到目錄時出現問題

[英]Having issues saving email attachment to directory

我正在開發一個可抓取我的電子郵件並嗅出所有帶有附件的電子郵件的應用程序。 所有附件均按收到順序返回。 現在,我想更進一步,想將所有附件保存在本地目錄中。 我一直在尋找文檔或示例,但是我空着。 我將向您展示我的代碼片段

此功能將獲得電子郵件附件

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 
    }

好了,在查看了附件定義之后,我發現我要遍歷一個字節數組來實現我想要的。 這是我的附件循環的一些代碼。

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