我开发了成功将电子邮件附件下载到本地电脑的方法
我也必须将它们从电子邮件直接下载到ftp服务器
我试图实现我认为应该工作的功能。
当我成功获取FileAttachment对象时,我尝试使用其属性ContentLocation来获取附件URL
然后我将该URL传递给webClient.DownloadString()方法
但不幸的是,属性ContentLocation始终为空
所以我在这行代码中有一个例外。
以下是我的方法的完整列表和评论:
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);
我应该改变什么来完成这项工作? 或者还有其他解决方案吗?
答案 0 :(得分:2)
我认为ContentLocation依赖于消息创建者。我认为您要做的是使用Load(Stream)将内容拉入流中,然后根据isSaveToFTP标志选择在哪里写入该流。
答案 1 :(得分:1)
以下是基于richrdsonmarkj答案的解决方案:
if (isSaveToFTP)
{
using (WebClient webClient = new WebClient())
{
Stream stream = new MemoryStream();
fileAttachment.Load(stream);
using (var reader = new StreamReader(stream))
{
byte[] buffer = Encoding.Default.GetBytes(reader.ReadToEnd());
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();
}
}