运行 电子邮件 (POP3) 代码时,SSIS 中的 C# 脚本任务出错

Error in C# script task in SSIS when running code for email (POP3)

我有一个电子邮件脚本任务,我试图用它来通过 SSIS script task 传递变量。我确保我拼写正确并且我的代码可以编译。我收到此错误:

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

我承认我是新手,如果有简单的解决方法,我深表歉意。

我添加了以下两个命名空间:

using System.Net;
using System.Net.Mail;

在下面的代码中,我硬编码了凭据的用户名和密码。我可以将其更改为变量(我认为。)

当我直接在代码中将值硬编码到变量时,它仍然给出相同的错误。代码构建和清理都很好,因此感谢任何帮助。谢谢。

public void Main()
    {
        // TODO: Add your code here
        {

            string SendMailTo = Dts.Variables["SendMailTo"].Value.ToString();
            string SendMailFrom = Dts.Variables["SendMailFrom"].Value.ToString();
            string sSubject = Dts.Variables["sSubject"].Value.ToString();
            string sBody = Dts.Variables["sBody"].Value.ToString();
            string SmtpServer = Dts.Variables["SmtpServer"].Value.ToString();





            SendMailMessage(SendMailTo, SendMailFrom, sSubject, sBody, false, SmtpServer);

            Dts.TaskResult = (int)ScriptResults.Success;

        }
    }
    private void SendMailMessage(string SendTo, string From, string Subject, string Body, bool IsBodyHtml, string Server)
    {

        MailMessage htmlMessage;
        SmtpClient mySmtpClient;

        htmlMessage = new MailMessage(SendTo, From, Subject, Body);
        htmlMessage.IsBodyHtml = IsBodyHtml;

        mySmtpClient = new SmtpClient(Server);
        mySmtpClient.Credentials = new System.Net.NetworkCredential("myusername", "mypassword"); ;
        mySmtpClient.Send(htmlMessage);

    }

    }

    #region ScriptResults declaration
    /// <summary>
    /// This enum provides a convenient shorthand within the scope of this class for setting the
    /// result of the script.
    /// 
    /// This code was generated automatically.
    /// </summary>
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
    #endregion

}

菜鸟失误!代码在常规 C# 控制台中不起作用。在那里调试它,进行必要的调整,然后转瞬即逝!对于垃圾邮件,我深表歉意。