1
votes

I have created script task in SSIS and wrote C# program to send mail. it is like this.

public void Main()
    {

        MailMessage mail = new MailMessage("[email protected]", "[email protected]", "subject", "body");
        SmtpClient client = new SmtpClient("smtp.office365.com", 587);
        client.Credentials = new NetworkCredential("[email protected]", "password!!!");
        client.EnableSsl = true;
        client.Send(mail);

        Dts.TaskResult = (int)ScriptResults.Success;

    }

When i tried to run the program, it shows the following exception.

Exception has been thrown by the target of an invocation.

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

When i run this code in C# console mode it's working fine.

I tried send mail task in SSIS, but it is not working. So i used script task.

any ideas on how to solve this?

1
Does it have an inner exception? It might have a more helpful exception messageVolkan Paksoy
Which of your 5 lines is generating the exception? Fire Information events or use a messagebox for a quick and dirty answer. Does the C# console app work on the same box and under the same account that the SSIS package is not working?billinkc

1 Answers

2
votes

I finally found the solution.

i modified the code like this..

public void Main()
{

    MailMessage mail = new MailMessage("[email protected]", "[email protected]", "subject", "body");
    SmtpClient client = new SmtpClient("smtp.office365.com", 587);
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("[email protected]", "password!!!");
    client.Send(mail);

    Dts.TaskResult = (int)ScriptResults.Success;

}

then it worked. :)