用于发送电子邮件的 SSIS 脚本任务

问题描述 投票:0回答:0

我在 SSIS 2014 中使用 C# 中的脚本任务运行良好,直到它引发错误然后停止。我想记录错误并继续。我已将属性 FailPackageOnFailure 和 FailParentOnFailure 设置为 false,并将 ForceExecutionResult 属性设置为无。我正在使用 catch (SmtpException ex) 将错误传递给变量并在表中写入错误消息,但不知道这是否会停止执行。

顺便说一句,我正在使用 Mandrill 发送电子邮件,这是代码:

``#region Help:  Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in
 * a .Net application within the context of an Integration Services control flow. 
 * 
 * Expand the other regions which have "Help" prefixes for examples of specific ways to use
 * Integration Services features within this script task. */
#endregion


#region Namespaces
using System;
using System.IO;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
#endregion

namespace ST_ed6f43944ba144538041128567b3d023
{
    /// <summary>
    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,
    /// or parent of this class.
    /// </summary>
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        #region Help:  Using Integration Services variables and parameters in a script

        #endregion

        #region Help:  Firing Integration Services events from a script
    

        #endregion

        #region Help:  Using Integration Services connection managers in a script
        /* Some types of connection managers can be used in this script task.  See the topic 
         * "Working with Connection Managers Programatically" for details.
         * 
         * Example of using an ADO.Net connection manager:
         *  object rawConnection = Dts.Connections["Sales DB"].AcquireConnection(Dts.Transaction);
         *  SqlConnection myADONETConnection = (SqlConnection)rawConnection;
         *  //Use the connection in some code here, then release the connection
         *  Dts.Connections["Sales DB"].ReleaseConnection(rawConnection);
         *
         * Example of using a File connection manager
         *  object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);
         *  string filePath = (string)rawConnection;
         *  //Use the connection in some code here, then release the connection
         *  Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);
         * */
        #endregion


    /// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
        public void SendEmailWithAttachment()
        {
            // Email from
            //Dts.Variables["User::st_SendFrom"].Value.ToString();
            string fromAddress = Dts.Variables["User::st_SendFrom"].Value.ToString();
            // Email to
            string toAddress = Dts.Variables["User::Correo_Electronico"].Value.ToString();
            // Password
            string fromPassword = Dts.Variables["User::st_Password"].Value.ToString();
            // Subject
            string subject = Dts.Variables["User::st_Subject"].Value.ToString();
            // Body
            string body = Dts.Variables["User::st_Message"].Value.ToString(); ;
            // Attachment path
            string attachmentPath = Dts.Variables["User::Ruta_PDF"].Value.ToString();

            // Create instance class SmtpClient
            SmtpClient smtp = new SmtpClient
            {
                Host = Dts.Variables["User::st_Server"].Value.ToString(),
                Port = int.Parse(Dts.Variables["User::st_Port"].Value.ToString()),
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(fromAddress, fromPassword),
                Timeout = 20000
            };

            // Create instance class MailMessage
            MailMessage message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            };

            // Create instance
            Attachment attachment = new Attachment(attachmentPath);
            message.Attachments.Add(attachment);

            try
            {
                //Attachment file size:
                FileInfo fi = new FileInfo(attachmentPath);
                long tArchivo = fi.Length/1024;

                //pre send log info
                bool fireAgain = false;
                Dts.Events.FireInformation(0, "Pre send: ", "Email: " + Dts.Variables["User::Correo_Electronico"].Value.ToString() + " Attachment: " + Dts.Variables["User::Ruta_PDF"].Value.ToString()+ " Tamaño: "+tArchivo+" Kb", string.Empty, 0, ref fireAgain);

                //Attachment size warning
                if(tArchivo>10000)
                    {
                    Dts.Events.FireInformation(0, "Pre send: ", "Email: " + Dts.Variables["User::Correo_Electronico"].Value.ToString() + " WARNING: Attachment exceds 10Mb", string.Empty, 0, ref fireAgain);
                     }

                // Send the email
                smtp.Send(message);
                System.Threading.Thread.Sleep(1000); //Pause for coffee
                ///////////////////////////////////////////// 

                //post send info
                Dts.Events.FireInformation(0, "Post send: ", "Email send to: " + Dts.Variables["User::Correo_Electronico"].Value.ToString(), string.Empty, 0, ref fireAgain);
                Dts.Variables["User::st_Result"].Value = "S";

            }
            catch (SmtpException ex)
            {

                //Show error in log and console.
                bool fireAgain = false;
                Dts.Events.FireInformation(0, "**ERROR** ", "SEND MAIL: " + Dts.Variables["User::Correo_Electronico"].Value.ToString()+" "+ex.Message, string.Empty, 0, ref fireAgain);
                Dts.Variables["User::st_Result"].Value = "N";
                Dts.Variables["User::st_SMTP_Result"].Value = ex.Message;
                Console.WriteLine("**ERROR SENDING MAIL: " + ex.Message);

            }

        }

        #region ScriptResults declaration
            /// <summary>
            /// This enum provides a convenient shorthand within the scope of this class for setting the
            /// result of the script.
            /// 
            /// This code was generated automatically.
            /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

    }
}`
`

我一直在谷歌中寻找解决方案,但没有成功。阅读 C# 知识库。

c# ssis sql-server-2014
© www.soinside.com 2019 - 2024. All rights reserved.