简体   繁体   中英

How to use SSIS variable outside Main() method in SSIS Script Task

I am using SSIS script task to send automated e-mail based on a few pre-conditions. As part of that I have a SendAutomatedEmail() method and in this method I am passing two variables mailServer and recipient. In doing so I run into the error "object reference not set to an instance of an object.".

Tried to use a constructor but that did not resolve the problem.

class Program
{
    public void Main()
    {
        string mailServer = Dts.Variables["User::varServer"].Value.ToString();  
        string recipient = Dts.Variables["User::varRecipient"].Value.ToString(); 

        server msr = new server(mserv, rec);
    }

    public class server
    {
        string ms;
    string r;

        public result(string mserv, string rec)
        {
           ms = mserv;
           r = rec;
        }
    }
}

using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    public void Main()
    {
    try
    {
        //do something
    }
    catch
    {
        //catch exception
    }
    }

public static void SendAutomatedEmail(string htmlString, string recipient = "user@domain.com")
{

 try
 {
     string mailServer = Dts.Variables["User::varServer"].Value.ToString();  //error "object reference not set to an instance of an object."
     string recipient = Dts.Variables["User::varRecipient"].Value.ToString();   //error "object reference not set to an instance of an object."

     MailMessage message = new MailMessage("it@domain.com", recipient);
     message .IsBodyHtml = true;
     message .Body = htmlString;
     message .Subject = "Test Email";

     SmtpClient client = new SmtpClient(mailServer);
     var AuthenticationDetails = new NetworkCredential("user@domain.com", "password");
     client.Credentials = AuthenticationDetails;
     client.Send(message);
 }
 catch (Exception e)
 {
     //catch exception
 }

}

}

I should be able to pass the value to the variable seamlessly in the SendAutomatedEmail() method.

Simplest way is to declare 2 variables in Program Class, read the values within the Main() function and assign these values to the local variables. Then you can use local variables outside of Main() function.

using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{

    string mailServer;
     string recipient;

    public void Main()
    {
    try
    {
        mailServer = Dts.Variables["User::varServer"].Value.ToString();
        recipient = Dts.Variables["User::varRecipient"].Value.ToString();

    }
    catch
    {
        //catch exception
    }
    }

 private class sendEMail
 {
    public static void SendAutomatedEmail(string htmlString, string recipient = "user@domain.com")
    {

     try
     {

         MailMessage message = new MailMessage("it@domain.com", recipient);
         message .IsBodyHtml = true;
         message .Body = htmlString;
         message .Subject = "Test Email";

         SmtpClient client = new SmtpClient(mailServer);
         var AuthenticationDetails = new NetworkCredential("user@domain.com", "password");
         client.Credentials = AuthenticationDetails;
         client.Send(message);
     }
     catch (Exception e)
     {
         //catch exception
     }

    }

    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM