2013-08-21 31 views
0

我的要求是我需要發送包含錯誤細節的電子郵件,如果我的包中使用的任何一個組件失敗。如何發送關於ssis包失敗的電子郵件?

嘗試google搜索關於ssis中的優先約束和事件處理程序,但我的包很大,並且有很多組件(控制流項)。

我們可以爲包中使用的每個可執行文件定義許多事件處理程序嗎?如果是這樣,我爲每個組件定義事件處理程序並不是一個好的性能。

請建議要做到這一點的最好方式......

回答

0

使用事件處理程序選項卡設置要在特定事件發生什麼。您可以在包中的任何級別選擇事件(即跨整個包或僅在特定的可執行文件中)。在事件處理程序中,添加一個發送電子郵件任務。

1

您可以使用腳本任務來發送自己的自定義電子郵件在SSIS包
在此,你需要設置配置讀取惠變量作爲參數發送定製的電子郵件

編輯腳本,編寫代碼腳本

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Runtime; 
using System.Windows.Forms; 
using System.Text.RegularExpressions; 
using System.Net.Mail; 


    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] 
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase 
    { 
     #region VSTA generated code 
     enum ScriptResults 
     { 
      Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, 
      Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
     }; 
     #endregion 


     #region VSTA generated code 
     /*enum ScriptResults 
     { 
      Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, 
      Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
     };*/ 
     #endregion 

     /* 
     The execution engine calls this method when the task executes. 
     To access the object model, use the Dts property. Connections, variables, events, 
     and logging features are available as members of the Dts property as shown in the following examples. 

     To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value; 
     To post a log entry, call Dts.Log("This is my log text", 999, null); 
     To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true); 

     To use the connections collection use something like the following: 
     ConnectionManager cm = Dts.Connections.Add("OLEDB"); 
     cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"; 

     Before returning from this method, set the value of Dts.TaskResult to indicate success or failure. 

     To open Help, press F1. 
    */ 

     public void Main() 
     { 
      string sSubject = "Integration Task Result"; 

      int iPriority = 2; 

      if (SendMail(sSubject, iPriority)) 
      { 
       Dts.TaskResult = (int)ScriptResults.Success; 
      } 
      else 
      { 
       //Fails the Task 
       Dts.TaskResult = (int)ScriptResults.Failure; 
      } 
     } 
     public bool SendMail(string sSubject,int iPriority) 
     { 
      try 
      { 
       string Email_Server = Dts.Variables["Email_Server"].Value.ToString(); 
       string Email_Port = Dts.Variables["Email_Port"].Value.ToString(); 
       string Form_Email_User = Dts.Variables["Form_Email_User"].Value.ToString(); 
       string From_Pass = Dts.Variables["From_Pass"].Value.ToString(); 
       string To_Email = Dts.Variables["To_Email"].Value.ToString();  
       string CC_Email = Dts.Variables["CC_Email"].Value.ToString(); 
       string From_Email = Dts.Variables["From_Email"].Value.ToString(); 
       string Email_FromName = Dts.Variables["Email_FromName"].Value.ToString(); 


       string TaskID = Dts.Variables["TaskID"].Value.ToString(); 
       string TaskName = Dts.Variables["TaskName"].Value.ToString(); 
       string FailedConfigurations = Dts.Variables["FailedConfigurations"].Value.ToString(); 
       string PackageName = Dts.Variables["PackageName"].Value.ToString(); 
       string StartTime = Dts.Variables["StartTime"].Value.ToString(); 
       string CreatorComputerName = Dts.Variables["CreatorComputerName"].Value.ToString(); 
       string UserName = Dts.Variables["UserName"].Value.ToString(); 



       String sBody = "StartTime = " + StartTime + 
        "<br/>TaskID = " + TaskID + 
        "<br/>TaskName = " + TaskName + 
        "<br/>UserName = " + UserName + 
        "<br/>CreatorComputerName = " + CreatorComputerName + 
        "<br/>PackageName = " + PackageName; 

       if (FailedConfigurations.Equals("")) 
       { 
        sBody = sBody + "<br/><br/>Task Execution failure"; 
       } else { 
        sBody = sBody + "<br/><br/>FailedConfigurations = " + FailedConfigurations; 
       } 


       SmtpClient smtpClient = new SmtpClient(); 
       MailMessage message = new MailMessage(); 

       MailAddress fromAddress = new MailAddress(From_Email, Email_FromName); 

       //You can have multiple emails separated by ; 
       string[] sEmailTo = Regex.Split(To_Email, ";"); 
       string[] sEmailCC = Regex.Split(CC_Email, ";"); 
       int sEmailServerSMTP = int.Parse(Email_Port); 

       smtpClient.Host = Email_Server; 
       smtpClient.Port = sEmailServerSMTP; 
       smtpClient.EnableSsl = true; 

       System.Net.NetworkCredential myCredentials = 
       new System.Net.NetworkCredential(Form_Email_User, From_Pass); 
       smtpClient.Credentials = myCredentials; 

       message.From = fromAddress; 

       if (sEmailTo != null) 
       { 
        for (int i = 0; i < sEmailTo.Length; ++i) 
        { 
         if (sEmailTo[i] != null && sEmailTo[i] != "") 
         { 
          message.To.Add(sEmailTo[i]); 
         } 
        } 
       } 
       if (sEmailCC != null) 
       { 
        for (int i = 0; i < sEmailCC.Length; ++i) 
        { 
         if (sEmailCC[i] != null && sEmailCC[i] != "") 
         { 
          message.To.Add(sEmailCC[i]); 
         } 
        } 
       } 
       switch (iPriority) 
       { 
        case 1: 
         message.Priority = MailPriority.High; 
        break; 
        case 3: 
         message.Priority = MailPriority.Low; 
        break; 
        default: 
         message.Priority = MailPriority.Normal; 
        break; 
       } 
       message.Subject = sSubject; 
       message.IsBodyHtml = true; 
       message.Body = sBody; 

       smtpClient.Send(message); 
       return true;  
      } 
      catch (Exception ex) 
      { 
       return false; 
      } 
     } 
    } 
相關問題