从SSIS中的exchange电子邮件获取附件文件
我有一个客户,将发送电子邮件附件文件,每天我们来处理它。 我有脚本任务,但它不工作,脚本通过webmail连接到我的帐户,但当脚本尝试读取收件箱文件夹时,我得到一个错误。 在此特定行中:FindItemsResults=service.FindItems(WellKnownFolderName.Inbox、querystring、view)强>从SSIS中的exchange电子邮件获取附件文件,ssis,email-attachments,exchangewebservices,Ssis,Email Attachments,Exchangewebservices,我有一个客户,将发送电子邮件附件文件,每天我们来处理它。 我有脚本任务,但它不工作,脚本通过webmail连接到我的帐户,但当脚本尝试读取收件箱文件夹时,我得到一个错误。 在此特定行中:FindItemsResults=service.FindItems(WellKnownFolderName.Inbox、querystring、view) using System; using System.Collections.Generic; using System.Linq; using Syste
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.Exchange.WebServices.Data;
namespace testmailattachment
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ExchangeService service = new ExchangeService();
service.Credentials = new WebCredentials("user", "pass", "domain");
/*service.Url = new Uri("https://webmail.domain.es/owa/");*/
service.AutodiscoverUrl("birep@domain.es");
GetAttachments(service);
}
private static void GetAttachments(ExchangeService service)
{
// Return a single item.
ItemView view = new ItemView(1);
string querystring = "HasAttachments:true Subject:'Message with Attachments' Kind:email";
//Console.WriteLine("check test");
// Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, querystring, view);
if (results.TotalCount > 0)
{
EmailMessage email = results.Items[0] as EmailMessage;
// Request all the attachments on the email message. This results in a GetItem operation call to EWS.
email.Load(new PropertySet(EmailMessageSchema.Attachments));
foreach (Attachment attachment in email.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
// Load the file attachment into memory. This gives you access to the attachment content, which
// is a byte array that you can use to attach this file to another item. This results in a GetAttachment operation
// call to EWS.
fileAttachment.Load();
//Console.WriteLine("Load a file attachment with a name = " + fileAttachment.Name);
// Load attachment contents into a file. This results in a GetAttachment operation call to EWS.
fileAttachment.Load("\\\\server\\folder" + fileAttachment.Name);
// Put attachment contents into a stream.
using (FileStream theStream = new FileStream("C:\\temp\\Stream_" + fileAttachment.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
//This results in a GetAttachment operation call to EWS.
fileAttachment.Load(theStream);
}
}
else // Attachment is an item attachment.
{
ItemAttachment itemAttachment = attachment as ItemAttachment;
// Load the item attachment properties. This results in a GetAttachment operation call to EWS.
itemAttachment.Load();
//Console.WriteLine("Loaded an item attachment with Subject = " + itemAttachment.Item.Subject);
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
使用Microsoft.Exchange.WebServices.Data;
命名空间testmailattachment
{
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
ExchangeService服务=新的ExchangeService();
service.Credentials=新的WebCredentials(“用户”、“通行证”、“域”);
/*service.Url=新的Uri(“https://webmail.domain.es/owa/");*/
服务。自动发现URL(“birep@domain.es");
获取附件(服务);
}
专用静态void GetAttachments(ExchangeService服务)
{
//返回单个项目。
ItemView视图=新的ItemView(1);
string querystring=“HasAttachments:true主题:'messagewithattachments'种类:email”;
//控制台写入线(“检查测试”);
//在收件箱中查找第一封包含附件的电子邮件。这将导致对EWS的FindItem操作调用。
FindItemsResults=service.FindItems(WellKnownFolderName.Inbox、querystring、view);
如果(results.TotalCount>0)
{
EmailMessage email=结果。邮件[0]为EmailMessage;
//请求电子邮件上的所有附件。这将导致对EWS的GetItem操作调用。
Load(新属性集(EmailMessageSchema.Attachments));
foreach(电子邮件中的附件。附件)
{
如果(附件是文件附件)
{
FileAttachment FileAttachment=作为文件附件的附件;
//将文件附件加载到内存中。这使您可以访问附件内容,其中
//是一个字节数组,可用于将此文件附加到另一项。这将导致GetAttachment操作
//打电话给EWS。
fileAttachment.Load();
//Console.WriteLine(“加载名为=“+fileAttachment.name”)的文件附件);
//将附件内容加载到文件中。这将导致对EWS的GetAttachment操作调用。
加载(“\\\\server\\folder”+fileAttachment.Name);
//将附件内容放入流中。
使用(FileStream theStream=newfilestream(“C:\\temp\\Stream\\”+fileAttachment.Name,FileMode.OpenOrCreate,FileAccess.ReadWrite))
{
//这将导致对EWS的GetAttachment操作调用。
文件附件。加载(流);
}
}
else//附件是项目附件。
{
ItemAttachment ItemAttachment=作为ItemAttachment的附件;
//加载项目附件属性。这将导致对EWS的GetAttachment操作调用。
itemAttachment.Load();
//Console.WriteLine(“加载了主题为=“+itemAttachment.item.Subject”的项目附件);
}
}
}
}
}
}
此外,我也不知道该怎么做,脚本只是从特定的人或主题那里获取电子邮件
提前感谢。如果您遇到错误,错误是什么?您正在使用哪个版本的Exchange?如果出现错误,错误是什么?您正在使用哪个版本的Exchange?