在SMTP连接管理器上使用Foreach Loop中的脚本任务发送电子邮件错误

时间:2019-06-11 15:27:46

标签: ssis html-email etl ssis-2012 foreach-loop-container

我有一个Foreach循环容器,该容器具有使用SMTP连接管理器成功发送电子邮件的发送邮件任务。我需要发送HTML电子邮件,并且了解“发送邮件任务”不会执行此操作。我添加了一个脚本任务,将HTML文本合并到电子邮件正文中,但是

导致脚本错误
  

无法解析远程名称

使用与“发送邮件任务”中相同的SMTP连接管理器。如何将多封电子邮件发送到电子邮件地址列表?任何帮助将不胜感激。

整个错误:

HKWorkoutConfiguration

解决方案:

System.Net.Mail.SmtpException was unhandled by user code
  HResult=-2146233088
  Message=Failure sending mail.
  Source=System
  StackTrace:
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at ST_209a900aa97949759ea05ad934ade067.ScriptMain.Main() in c:\Users\sschildt\AppData\Local\Temp\2\Vsta\e2ae5c29103d447fad1855720cffe591\ScriptMain.cs:line 117
  InnerException: System.Net.WebException
       HResult=-2146233079
       Message=The remote name could not be resolved: 'SmtpServer=xxxx.xxxx.xxxx.edu;UseWindowsAuthentication=True;EnableSsl=False;'
       Source=System
       StackTrace:
            at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
            at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
            at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
            at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
            at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
            at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
            at System.Net.Mail.SmtpClient.GetConnection()
            at System.Net.Mail.SmtpClient.Send(MailMessage message)
       InnerException: 

1 个答案:

答案 0 :(得分:0)

我看到您尚未发布完整的错误日志,但是您可以尝试执行以下操作:

private static void sendEmail()
    {
        // Change below parameters
        string ToEmailAddress = "toemailaddress";
        string FromEmailAddress = "fromemailaddress";
        string FromEmailPassword = "fromemailpassworda";
        string EmailSubject = "Email Subject";
        string SmtpServerName = "smtpservername";
        string LogoFilePath = @"SomeLogoPicturePathWithFileName";
        int PortNumber = addportnumberhere;

        try
        {
            var mail = new MailMessage();

            // Email Text HTML
            StringBuilder mailBody = new StringBuilder();
            mailBody = CreatEmailBodyTextHtml(LogoFilePath);

            // Add Logo
            mail.IsBodyHtml = true;
            mail.Body = mailBody.ToString();
            mail.AlternateViews.Add(getEmbeddedImage(LogoFilePath, mailBody.ToString()));

            // Send Email
            SmtpClient SmtpServer = new SmtpClient(SmtpServerName);

            mail.From = new MailAddress(FromEmailAddress);
            mail.To.Add(ToEmailAddress);
            mail.Subject = EmailSubject;

            SmtpServer.Port = PortNumber;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new NetworkCredential(FromEmailAddress, FromEmailPassword);
            SmtpServer.EnableSsl = false;
            SmtpServer.Send(mail);
        }
        catch (Exception s)
        {
            Console.WriteLine(s);
        }

    }

private static StringBuilder CreatEmailBodyTextHtml(string imgPath)
    {
        StringBuilder mailBody = new StringBuilder();
        mailBody.AppendFormat("<h1>Hi, This is a Test Email.</h1>");
        mailBody.AppendFormat("Dear Sir/Madam, ");
        mailBody.AppendFormat("<br />");
        mailBody.AppendFormat("<br />");
        mailBody.AppendFormat("With Regards");
        mailBody.AppendFormat("<br />");
        mailBody.AppendFormat("<br />");
        mailBody.AppendFormat("Tester.</p>");
        mailBody.AppendFormat("<br />");
        mailBody.AppendFormat("<img src=\"" + imgPath +"\">");

        return mailBody;
    }
public static AlternateView getEmbeddedImage(string filePath, string mailBody)
    {
        LinkedResource res = new LinkedResource(filePath);
        res.ContentId = Guid.NewGuid().ToString();
        string htmlBody = @"<img src='cid:" + res.ContentId + "'" + "width= \"700\" height = \"300\"" + @"/>" + mailBody;
        AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
        alternateView.LinkedResources.Add(res);
        return alternateView;
    }

它适用于我和gmail服务器以及SMTP服务器。
您可以更改sendEmail方法以接受toEmailAddress并在循环中调用它。 toEmailAddress可以来自txt文件,SQL数据库表或其他东西。

我使用Gmail SMTP在下面的示例数据中尝试了上面的代码,并在1秒内收到了电子邮件。

string ToEmailAddress = "xyzto@gmail.com";
string FromEmailAddress = "xyz@gmail.com";
string FromEmailPassword = "addyourpassword";
string EmailSubject = "Email Subject";
string SmtpServerName = "smtp.gmail.com";
string LogoFilePath = @"someimagepathwithfilename";
int PortNumber = 587;