如何在SSIS中将Microsoft Exchange数据导入SQL

时间:2017-07-07 13:20:22

标签: sql sql-server ssis exchange-server

我的老板给了我一份工作,将旧的软件重写成SSIS包。我需要SSIS:

  • 访问我们的Microsoft Exchange服务器上的特定邮箱
  • 浏览所有UNREAD电子邮件,并根据文件类型
  • 从中下载附件
  • 将这些文件的内容(通过记事本可读的文本)导入SQL表
  • 将电子邮件主题导入另一个SQL表

我不确定上面描述的内容是否在标准SSIS包中可用(我对此表示怀疑) - 也许我需要下载一些库。到目前为止,我找到了this。它能解决这个问题吗?如果没有,您是否知道使用SSIS实现我想要的任何其他方式?

2 个答案:

答案 0 :(得分:3)

最好的方法是使用Microsoft Exchange Webservices(= EWS)下载和work with all kind of attachments(此处为example)。

但是,由于这是一个API,您需要根据EWS API在MS Exchange和SQL Server之间构建某种中间件。它可能是这样的:

//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

答案 1 :(得分:1)

事实证明,默认SSIS库没有与Exchange服务器建立连接所需的组件。我找到了2个允许你这样做的第三方库:

它们都是付费的,但也有免费的测试版本。