我开发了将电子邮件附件成功下载到本地PC的方法。
而且我也必须直接从电子邮件将其下载到ftp服务器。
我试图实现我认为应该起作用的功能。
成功获取FileAttachment对象时,我尝试使用其属性ContentLocation获取附件URL。
然后,将该URL传递给webClient.DownloadString()方法。
但不幸的是,ContentContent属性始终为null。
因此,这行代码中有一个例外。
这是我的方法的完整列表,并带有注释:

    var service = new ExchangeService(ExchangeVersion.Exchange2013);
    service.Credentials = new NetworkCredential(serviceUserName, servicePassword);

    service.Url = new Uri(serviceUrl);
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    var path = System.IO.Directory.CreateDirectory(Path.Combine(applicationPath, name));

    //Filters
    List<SearchFilter> searchANDFilterCollection = new List<SearchFilter>();
    searchANDFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
    searchANDFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.From, new EmailAddress(email)));
    SearchFilter sf =
        new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchANDFilterCollection.ToArray());
    FindItemsResults<Item> findResults;
    ItemView view = new ItemView(10);
    do
    {
        findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view);
        if (findResults != null && findResults.Items != null && findResults.Items.Count > 0)
            foreach (EmailMessage item in findResults)
            {
                var message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments));
                foreach (Attachment attachment in message.Attachments)
                {
                    if (attachment is FileAttachment)
                    {
                        if (attachment.Name.Contains(".xlsx") || attachment.Name.Contains(".xlsb") || attachment.Name.Contains(".xls") || attachment.Name.Contains(".csv"))
                        {
                            var fileAttachment = attachment as FileAttachment;

                            //Download to ftp
                            if (isSaveToFTP)
                            {
                                var webClient = new WebClient();
                                string readHtml = webClient.DownloadString(fileAttachment.ContentLocation);
                                byte[] buffer = Encoding.Default.GetBytes(readHtml);

                                WebRequest makeDirectory = WebRequest.Create(Path.Combine(ftpAddress, name));
                                makeDirectory.Method = WebRequestMethods.Ftp.MakeDirectory;
                                makeDirectory.Credentials = new NetworkCredential(username, password);
                                WebRequest uploadFile = WebRequest.Create(ftpAddress + name + "/" + fileAttachment.Name);
                                uploadFile.Method = WebRequestMethods.Ftp.UploadFile;
                                uploadFile.Credentials = new NetworkCredential(username, password);
                                Stream reqStream = uploadFile.GetRequestStream();
                                reqStream.Write(buffer, 0, buffer.Length);
                                reqStream.Close();
                            }
                            //Download to pc
                            else fileAttachment.Load(String.Format(@"{0}\{1}", path, fileAttachment.Name));
                        }
                    }
                    //fileAttachment.Load(path.Name);
                }
            }


        // mark email as read
        item.IsRead = true;
        item.Update(ConflictResolutionMode.AlwaysOverwrite);
      }
    }
    while (findResults.MoreAvailable);


我应该改变什么才能完成这项工作?
还是有其他解决方案?

最佳答案

我认为ContentLocation取决于消息创建者。我认为您要做的是使用Load(Stream)将内容拉入Stream,然后基于isSaveToFTP标志选择将Stream写入何处。

10-07 13:57