无法使用脚本任务从SSIS发送邮件

时间:2015-07-31 07:02:57

标签: c# ssis

我在SSIS中创建了脚本任务并编写了C#程序来发送邮件。就像这样。

public void Main()
    {

        MailMessage mail = new MailMessage("from@email.com", "to@gmail.com", "subject", "body");
        SmtpClient client = new SmtpClient("smtp.office365.com", 587);
        client.Credentials = new NetworkCredential("from@email.com", "password!!!");
        client.EnableSsl = true;
        client.Send(mail);

        Dts.TaskResult = (int)ScriptResults.Success;

    }

当我尝试运行程序时,它显示以下异常。

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()

当我在C#控制台模式下运行此代码时,它工作正常。

我尝试在SSIS中发送邮件任务,但它无法正常工作。所以我使用了脚本任务。

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:2)

我终于找到了解决方案。

我修改了这样的代码..

public void Main()
{

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

    Dts.TaskResult = (int)ScriptResults.Success;

}

然后它奏效了。 :)