错误在SSIS C#脚本任务运行的电子邮件(POP3)代码

问题描述:

,当我有我尝试使用通过SSIS script task将变量传递到电子邮件脚本任务。我确信我拼写正确,我的代码编译。我得到这个错误:错误在SSIS C#脚本任务运行的电子邮件(POP3)代码

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#控制台中不起作用。在那里调试它,进行必要的调整,然后发生!我对这些垃圾邮件表示歉意。