从SSIS中的交换电子邮件中获取附件文件

时间:2016-02-08 13:35:05

标签: ssis email-attachments exchangewebservices

我有一个客户端会每天通过电子邮件附件文件发送给我们来处理它。 我有脚本任务,但它不起作用,脚本通过webmail连接到我的帐户,但当脚本尝试读取收件箱文件夹时,我收到一个错误。 在此特定行中: FindItemsResults results = service.FindItems(WellKnownFolderName.Inbox,querystring,view);

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);
                    }
                }
            }
        }
    }
}

此外,我不知道怎么做,脚本只是接收来自特定人或主题的电子邮件。

提前致谢。

0 个答案:

没有答案