为什么在写入流时提取附件内容始终为空?

Why extract attacment content is always null while writing stream?

我知道这是一个简单的问题,但我找不到解决方案。我一直在尝试从电子邮件中提取附件,之后我将写入文件流以保存任何导演的文件。但是我的 fileAttachment.Content 总是空的。如何将电子邮件地址保存到我的目录中?

public static void ExtractAttachment(string targetDir)
{

    SearchFilter.IsEqualTo ffrom = new SearchFilter.IsEqualTo(EmailMessageSchema.From, "xxx@yyyy.com.tr");
    SearchFilter.ContainsSubstring fsubject = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "yyyyyyyy", ContainmentMode.Substring, ComparisonMode.IgnoreCaseAndNonSpacingCharacters);
    SearchFilter.IsEqualTo fattach = new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true);
    SearchFilter.IsGreaterThanOrEqualTo fdate = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, DateTime.Now.Date);
    SearchFilter.SearchFilterCollection mfilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, fdate, ffrom, fattach, fsubject);

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
    service.Credentials = new WebCredentials(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);
    service.Url = new Uri(ConfigurationManager.AppSettings["ExchangeService"]);
    ItemView view = new ItemView(24);
    view.PropertySet = new PropertySet(ItemSchema.DateTimeReceived, ItemSchema.Subject, EmailMessageSchema.HasAttachments, EmailMessageSchema.From);
    view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
    view.Traversal = ItemTraversal.Shallow;
    FindItemsResults<Item> searchitem = service.FindItems(WellKnownFolderName.Inbox, mfilter, view);
    if (searchitem.TotalCount > 0)
    {
        for (int i = 0; i < searchitem.Items.Count; i++)
        {
            Console.WriteLine("loop is working");

            searchitem.Items[i].Load(PropertySet.FirstClassProperties);

            foreach (Attachment part in searchitem.Items[i].Attachments)
            {
                if (part is FileAttachment && string.IsNullOrEmpty(part.Name) == false && Path.GetExtension(part.Name).Equals(".txt", StringComparison.InvariantCultureIgnoreCase))
                {

                    using (MemoryStream ms = new MemoryStream())
                    {
                        FileAttachment fileAttachment = part as FileAttachment;
                        fileAttachment.Load(ms);
                        ms.Position = 0;

                        string fname = Path.Combine(targetDir, fileAttachment.Name);
                        string fdir = Path.GetDirectoryName(fname);
                        if (!Directory.Exists(fdir)) Directory.CreateDirectory(fdir);

                        using (FileStream fstream = new FileStream(fname, FileMode.Create))
                        {
                            fstream.Write(fileAttachment.Content, 0, fileAttachment.Content.Length);
                        }

                    }
                }
                break;
            }
        }
    }

}

请替换 fstream.Write(fileAttachment.Content, 0, fileAttachment.Content.长度); 和 ms.CopyTo(fstream, 0, ms.Length);

应该可以。

您似乎在 fileAttachment.Load(ms) 行中阅读了 fileAttachment.Content 所以余数,因为流是 eof 是空的。