,當我有我嘗試使用通過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
}