如何在数据源是电子邮件中的每日Excel工作表时安排SSIS导入?

时间:2015-03-06 16:45:45

标签: sql-server excel ssis schedule

您好我有关于安排SSIS导入的具体问题:

我有一个数据源,每天都会将预定的Excel工作表发送到我的电子邮件收件箱。期望找到一种解决方案,将每日excel表单电子邮件发送到SSIS并安排自己导入SQL。

有可能吗?如果有人可以提供一些有用的链接或我将在哪里开始研究,将非常感激。

谢谢

1 个答案:

答案 0 :(得分:0)

感觉邮件客户端没有答案,但我只是想把它扔出去。

这适用于Gmail,并已针对此进行配置。

首先,您必须确保启用POP(这将允许进程读取您的收件箱)。建议您选择“从现在开启启用”,因为它只允许从该点开始查看项目。

完成后,您需要获取OpenPOP.net的Nuget包

现在是有趣的部分。请记住,这不是正确的编码实践,您有责任添加必要的安全预防措施和错误处理。这纯粹是一个概念证明。

    using OpenPop.Pop3;
    using OpenPop.Mime;
    using OpenPop.Mime.Header;

            // create the client to be used 
            Pop3Client client = new Pop3Client();

            // connect to the client via host server port and use ssl bool
            client.Connect("pop.gmail.com", 995, true);

            // log into the specific account to read
            client.Authenticate("username", "password");

            // generate count of emails in the inbox
            int msgcount = client.GetMessageCount();

    //loop thru available message numbers via message count 
    //incremented at the end of the while loop
    while (msgcount > 0)
            {
                // gets the message header info to, from and subject ect.
                MessageHeader header = client.GetMessageHeaders(msgcount);

                //read the subject line
                string subject = header.Subject;

                //compare subject to identify the correct email 
                if (subject.ToLower() == "subject to match")
                {
                    // gets message info based on message number from msgcount
                    var message = client.GetMessage(msgcount);

                    // creates list of the attachments available in the message
                    List<MessagePart> attachments = message.FindAllAttachments();

                    //loops thru attachments 
                    foreach (var file in attachments)
                    {
                        //assigns filename as string for stream
                        string filename = file.FileName;

                       //create a stream to download the file
                        var stream = new FileStream(@"destination path" + filename,FileMode.Create,FileAccess.ReadWrite);

                        // downloads file
                        file.Save(stream);

                        // closes stream to protect system and hung files
                        stream.Close();
                    }
                    // optional and must be configured to be allowed in your 
                    // email client.
                    client.DeleteMessage(msgcount);
                }
                 // increment message number 
                 msgcount--;
            }
            // this is extremely important if deleting or manipulating files in inbox
            //the above deletemessage command only marked the message to be deleted.
            // you must commit the change to have it take effect.
            // this command commits the changes you have made. 
            // this also closes your client connection so that no connections are left open. 
            client.Dispose();

这可以添加到导入中的脚本任务中,然后您可以像往常一样下载excel文件并将其导入,如果您手动将文件拉出并放在硬盘或网络驱动器上。