如何在 SSIS 中将 Microsoft Exchange 数据导入 SQL
How to import Microsoft Exchange data to SQL in SSIS
我的老板给了我一份工作,将一个旧软件重写成一个 SSIS 包。我需要 SSIS 来:
- 访问我们 Microsoft Exchange 服务器上的特定邮箱
- 查看所有未读电子邮件并根据文件类型从中下载附件
- 将这些文件的内容(可通过记事本读取的文本)导入 SQL table
- 将电子邮件主题导入另一个 SQL table
我不确定我上面描述的内容是否在标准 SSIS 包中可用(我怀疑)- 也许我需要下载一些库。到目前为止我找到了this。它会成功吗?如果没有,您是否知道使用 SSIS 实现我想要的任何其他方法?
最好的方法是使用 Microsoft Exchange Webservices (=EWS) to download and work with all kind of attachments (here is an example).
然而,由于这是 API,您需要在 MS Exchange 和基于 EWS API 的 SQL 服务器之间构建某种中间件。它可能是这样的:
//TODO: Replace these with your values
NetworkCredential exchangeAccessAccount = new NetworkCredential(@"UserName", @"Password", @"Domain");
Uri OutlookWebAccessUri = new Uri(@"[[Outlook Web Access Url]]/EWS/Exchange.asmx");
DateTime CalanderStart = new DateTime();
DateTime CalanderEnd = new DateTime();
int MaxItemsToReturn = 99999;
try
{
#region create service binding
// Create the service binding.
ExchangeService esb = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
esb.Credentials = exchangeAccessAccount;
esb.Url = OutlookWebAccessUri;
esb.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, Variables.UserDomainID.ToString());
#endregion
#region create CalendarView
CalendarView calendarView = new CalendarView(CalanderStart, CalanderEnd, MaxItemsToReturn);
calendarView.PropertySet = PropertySet.IdOnly;
#endregion
#region retrieve responce
// Do the EWS Call...
FindItemsResults<Appointment> findItemResponse = esb.FindAppointments(WellKnownFolderName.Calendar, calendarView);
if (findItemResponse == null)
{
return;
}
#endregion
#region load atendee data
//get additional properties for each item returned by view, do this as view cannot return a lot of useful stuff like attendees
ServiceResponseCollection<ServiceResponse> addProperties =
esb.LoadPropertiesForItems(from Item item in findItemResponse select item,
new PropertySet(
BasePropertySet.IdOnly,
AppointmentSchema.Resources,
AppointmentSchema.RequiredAttendees,
AppointmentSchema.OptionalAttendees,
AppointmentSchema.Subject,
AppointmentSchema.Start,
AppointmentSchema.End,
AppointmentSchema.IsCancelled
));
List<Appointment> additionalProperties = new List<Appointment>(addProperties.Count);
if (addProperties != null)
{
foreach (ServiceResponse currentResponce in addProperties)
{
additionalProperties.Add(((Appointment)((GetItemResponse)currentResponce).Item));
}
}
#endregion
#region process appts
Appointment currentAppointmentAddProps = null;
foreach (Appointment currentAppointment in findItemResponse)
{
#region find additional properties for current Appointment
currentAppointmentAddProps = additionalProperties.Find(delegate(Appointment arg)
{ return arg.Id == currentAppointment.Id; });
#endregion
//add data to output here
OutputRecordSetBuffer.ActualEndDate = currentAppointmentAddProps.End;
}
#endregion
}
catch (Exception e)
{
}
(更多信息 here)
事实证明,默认的 SSIS 库没有连接到 Exchange 服务器所需的组件。我找到了 2 个允许你这样做的第三方库:
两者都是付费的,但也有免费的测试版本。
我的老板给了我一份工作,将一个旧软件重写成一个 SSIS 包。我需要 SSIS 来:
- 访问我们 Microsoft Exchange 服务器上的特定邮箱
- 查看所有未读电子邮件并根据文件类型从中下载附件
- 将这些文件的内容(可通过记事本读取的文本)导入 SQL table
- 将电子邮件主题导入另一个 SQL table
我不确定我上面描述的内容是否在标准 SSIS 包中可用(我怀疑)- 也许我需要下载一些库。到目前为止我找到了this。它会成功吗?如果没有,您是否知道使用 SSIS 实现我想要的任何其他方法?
最好的方法是使用 Microsoft Exchange Webservices (=EWS) to download and work with all kind of attachments (here is an example).
然而,由于这是 API,您需要在 MS Exchange 和基于 EWS API 的 SQL 服务器之间构建某种中间件。它可能是这样的:
//TODO: Replace these with your values
NetworkCredential exchangeAccessAccount = new NetworkCredential(@"UserName", @"Password", @"Domain");
Uri OutlookWebAccessUri = new Uri(@"[[Outlook Web Access Url]]/EWS/Exchange.asmx");
DateTime CalanderStart = new DateTime();
DateTime CalanderEnd = new DateTime();
int MaxItemsToReturn = 99999;
try
{
#region create service binding
// Create the service binding.
ExchangeService esb = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
esb.Credentials = exchangeAccessAccount;
esb.Url = OutlookWebAccessUri;
esb.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, Variables.UserDomainID.ToString());
#endregion
#region create CalendarView
CalendarView calendarView = new CalendarView(CalanderStart, CalanderEnd, MaxItemsToReturn);
calendarView.PropertySet = PropertySet.IdOnly;
#endregion
#region retrieve responce
// Do the EWS Call...
FindItemsResults<Appointment> findItemResponse = esb.FindAppointments(WellKnownFolderName.Calendar, calendarView);
if (findItemResponse == null)
{
return;
}
#endregion
#region load atendee data
//get additional properties for each item returned by view, do this as view cannot return a lot of useful stuff like attendees
ServiceResponseCollection<ServiceResponse> addProperties =
esb.LoadPropertiesForItems(from Item item in findItemResponse select item,
new PropertySet(
BasePropertySet.IdOnly,
AppointmentSchema.Resources,
AppointmentSchema.RequiredAttendees,
AppointmentSchema.OptionalAttendees,
AppointmentSchema.Subject,
AppointmentSchema.Start,
AppointmentSchema.End,
AppointmentSchema.IsCancelled
));
List<Appointment> additionalProperties = new List<Appointment>(addProperties.Count);
if (addProperties != null)
{
foreach (ServiceResponse currentResponce in addProperties)
{
additionalProperties.Add(((Appointment)((GetItemResponse)currentResponce).Item));
}
}
#endregion
#region process appts
Appointment currentAppointmentAddProps = null;
foreach (Appointment currentAppointment in findItemResponse)
{
#region find additional properties for current Appointment
currentAppointmentAddProps = additionalProperties.Find(delegate(Appointment arg)
{ return arg.Id == currentAppointment.Id; });
#endregion
//add data to output here
OutputRecordSetBuffer.ActualEndDate = currentAppointmentAddProps.End;
}
#endregion
}
catch (Exception e)
{
}
(更多信息 here)
事实证明,默认的 SSIS 库没有连接到 Exchange 服务器所需的组件。我找到了 2 个允许你这样做的第三方库:
两者都是付费的,但也有免费的测试版本。