gpt4 book ai didi

c# - 从电子邮件直接下载附件到ftp服务器

转载 作者:行者123 更新时间:2023-11-30 21:52:26 24 4
gpt4 key购买 nike

我开发了成功将电子邮件附件下载到本地电脑的方法。
而且我还必须将它们从电子邮件直接下载到 ftp 服务器。
我尝试实现我认为应该起作用的功能。
当我成功获得 FileAttachment 对象时,我尝试使用其属性 ContentLocation 来获取附件 URL
然后我将该 URL 传递给 webClient.DownloadString() 方法。
但不幸的是,属性 ContentLocation 始终为 null。
所以我在这行代码中得到了一个异常(exception)。
下面是我的方法的完整列表和注释:

    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 的位置。

关于c# - 从电子邮件直接下载附件到ftp服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34790823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com