为什么在编写流时提取attacment内容始终是无效的



我知道这是简单的问题,但是我找不到解决方案。我一直在尝试从电子邮件中提取附件,之后我将写信给FileStream,以保存任何董事。但是我的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.length);和 Ms.Copyto(Fstream,0,Ms.Length);

应该有效的。

看起来您在Line FileatTachment.load(MS)中阅读FileatTachment.content因此,其余的,因为流为eof为null。

相关内容

  • 没有找到相关文章

最新更新