运行电子邮件代码(POP3)时,SSIS中的C#脚本任务出错
我有一个电子邮件脚本任务,我正试图使用它通过运行电子邮件代码(POP3)时,SSIS中的C#脚本任务出错,c#,email,ssis,pop3,C#,Email,Ssis,Pop3,我有一个电子邮件脚本任务,我正试图使用它通过SSIS脚本任务传递变量。我已经确保拼写正确,代码也能编译。我得到这个错误: 调用的目标已引发异常。 位于System.RuntimeMethodHandle.InvokeMethod(对象目标、对象[]参数、签名符号、布尔构造函数) 位于System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象obj,对象[]参数,对象[]参数) 在System.Reflection.RuntimeMeth
SSIS脚本任务
传递变量。我已经确保拼写正确,代码也能编译。我得到这个错误:
调用的目标已引发异常。
位于System.RuntimeMethodHandle.InvokeMethod(对象目标、对象[]参数、签名符号、布尔构造函数)
位于System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象obj,对象[]参数,对象[]参数)
在System.Reflection.RuntimeMethodInfo.Invoke(对象obj、BindingFlags invokeAttr、绑定器绑定器、对象[]参数、CultureInfo区域性)
位于System.RuntimeType.InvokeMember(字符串名称、BindingFlags BindingFlags、绑定器绑定器、对象目标、对象[]提供的参数、参数修改器[]修饰符、CultureInfo区域性、字符串[]namedParams)
位于Microsoft.SqlServer.Dts.Tasks.ScriptTask.vstatasksscriptingengine.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
}
public void Main()
{
//TODO:在此处添加代码
{
字符串SendMailTo=Dts.Variables[“SendMailTo”].Value.ToString();
字符串SendMailFrom=Dts.Variables[“SendMailFrom”].Value.ToString();
字符串SSObject=Dts.Variables[“SSObject”].Value.ToString();
字符串sBody=Dts.Variables[“sBody”].Value.ToString();
字符串SmtpServer=Dts.Variables[“SmtpServer”].Value.ToString();
SendMailMessage(SendMailTo、SendMailFrom、ssobject、sBody、false、SmtpServer);
Dts.TaskResult=(int)ScriptResults.Success;
}
}
私有void SendMailMessage(字符串SendTo、字符串From、字符串主题、字符串正文、bool IsBodyHtml、字符串服务器)
{
邮件信息;
SmtpClient mySmtpClient;
htmlMessage=新邮件消息(发送到、从、主题、正文);
htmlMessage.IsBodyHtml=IsBodyHtml;
mySmtpClient=新的SmtpClient(服务器);
mySmtpClient.Credentials=new System.Net.NetworkCredential(“myusername”、“mypassword”);
mySmtpClient.Send(htmlMessage);
}
}
#区域脚本结果声明
///
///这个枚举在这个类的范围内提供了一个方便的速记来设置
///脚本的结果。
///
///此代码是自动生成的。
///
枚举脚本结果
{
Success=Microsoft.SqlServer.Dts.Runtime.dtsesecresult.Success,
Failure=Microsoft.SqlServer.Dts.Runtime.dtsesecresult.Failure
};
#端区
}
新手犯了错误!代码在普通的C#控制台中不起作用。在那里调试,做了必要的调整,然后开始!我为垃圾邮件道歉