无法使用脚本任务从SSIS发送邮件(Unable to send mail from SSIS us

2019-10-24 09:06发布

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

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

Answer 1:

我终于找到了解决办法。

我修改了这样的代码..

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;

}

然后它的工作。 :)



文章来源: Unable to send mail from SSIS using script task
标签: c# ssis