SSIS 脚本任务:发送 Outlook 电子邮件(身份验证方法问题)
SSIS Script Task: Send Outlook E-mail (Authentication Method Issue)
身份验证方法发生了变化,导致下面的代码已过时。
我尝试在网上搜索有关如何修复错误的清晰简明的内容,但我找不到任何内容。但是,通过与我的 IT 团队合作,我能够更新代码并且现在可以使用了。
这是身份验证更改前的代码(注意凭据部分):
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
#endregion
namespace ST_502210b8c9334bfab4b37133997b2a7f
{
public void Main()
{
// C# Script for Script Task in SSIS
String SendMailFrom = "YourUserName@domain.edu";
String SendMailTo = Dts.Variables["ReceipientEmail"].Value.ToString();
String SendMailSubject = "Report Title";//Dts.Variables["EmailSubject"].Value.ToString();
String SendMailBody = "Please see attached";
string attachFile = Dts.Variables["FileName"].Value.ToString(); ;
try
{
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(attachFile);
MailMessage email = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.office365.com");
email.From = new MailAddress(SendMailFrom);
email.To.Add(SendMailTo);
email.Subject = SendMailSubject;
email.Body = SendMailBody;
email.Attachments.Add(attachment);
//email.Attachments = attachment;
// START
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(SendMailFrom, "Password");
SmtpServer.EnableSsl = true;
//END
SmtpServer.Send(email);
MessageBox.Show("Email was Successfully Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
这是解决方案(这将替换凭据部分):
SmtpServer.Port = 25;
SmtpServer.Host = "Host Name";
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
身份验证方法发生了变化,导致下面的代码已过时。
我尝试在网上搜索有关如何修复错误的清晰简明的内容,但我找不到任何内容。但是,通过与我的 IT 团队合作,我能够更新代码并且现在可以使用了。
这是身份验证更改前的代码(注意凭据部分):
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
#endregion
namespace ST_502210b8c9334bfab4b37133997b2a7f
{
public void Main()
{
// C# Script for Script Task in SSIS
String SendMailFrom = "YourUserName@domain.edu";
String SendMailTo = Dts.Variables["ReceipientEmail"].Value.ToString();
String SendMailSubject = "Report Title";//Dts.Variables["EmailSubject"].Value.ToString();
String SendMailBody = "Please see attached";
string attachFile = Dts.Variables["FileName"].Value.ToString(); ;
try
{
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(attachFile);
MailMessage email = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.office365.com");
email.From = new MailAddress(SendMailFrom);
email.To.Add(SendMailTo);
email.Subject = SendMailSubject;
email.Body = SendMailBody;
email.Attachments.Add(attachment);
//email.Attachments = attachment;
// START
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(SendMailFrom, "Password");
SmtpServer.EnableSsl = true;
//END
SmtpServer.Send(email);
MessageBox.Show("Email was Successfully Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
这是解决方案(这将替换凭据部分):
SmtpServer.Port = 25;
SmtpServer.Host = "Host Name";
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;