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

Unable to send mail from SSIS using script task

我在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 中发送邮件任务,但它不起作用。所以我使用了脚本任务。

有什么解决办法吗?

我终于找到了解决办法。

我这样修改了代码..

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;

}

然后就成功了。 :)