【发布时间】:2016-01-15 08:48:52
【问题描述】:
我开发了成功将电子邮件附件下载到本地电脑的方法。
我也必须将它们从电子邮件直接下载到 ftp 服务器。
我尝试实现我认为应该可以工作的功能。
当我成功获得 FileAttachment 对象时,我尝试使用其属性 ContentLocation 来获取附件 URL
然后我将该 URL 传递给 webClient.DownloadString() 方法。
但不幸的是,属性 ContentLocation 始终为空。
所以我在这行代码中有一个例外。
以下是我使用 cmets 方法的完整列表:
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);
我应该改变什么来完成这项工作?
还是有其他解决方案?
【问题讨论】:
标签: c# ftp exchangewebservices