SSIS 发送邮件

简介:

在SSIS中Send Mail的方法主要有三种,使用Send Mail Task,使用Script Task和使用存储过程msdb.dbo.sp_send_dbmail。

一,使用Send Mail Task

Send Mail Task 是SSIS提供的Task,使用非常简单,但有限制:

  1. 只能发送普通的文本格式的邮件,不支持 HTML 格式的邮件。
  2. 链接到SMTP Server有两种验证方式,在域中使用 Windows 方式验证,或使用匿名验证。
  3. SMTP Server 使用默认的端口号25

Send Mail Task的限制是跟SMTP connection manager的配置有关

SMTP Sever:输入SMTP Server的URL

Authentication:如果选择Use Winodows Authentication,那么用户必须在域中,使用Windows账户验证;如果不选择Use Winodows Authentication,那么验证方式就是使用匿名访问SMTP Server,如果SMTP Server支持匿名访问,那么验证失败。

Enable Secure Sockerts Layer(SSL):是否对数据加密,SSL用以保障在Internet上数据传输之安全,利用数据加密(Encryption)技术,可确保数据在网络上之传输过程中不会被截取及窃听

Timeout:超时时间

 

Package中使用Send Mail Task发送mail,这个mail带excel的附件,当成功发送一定数量的mail之后,发现一个问题,错误信息是

System.IO.IOException:Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.

System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host.

 

经过测试,错误原因可能是SMTP Server为链接预留缓存空间(8M),当附件的数据量到达8M阈值时,SMTP返回错误信息,将链接强制关闭。

Send Mail Task在循环使用SMTP Server的链接时,打开链接后,不会自动关闭。

我的疑问:为什么SMTP Server的链接不会自动关闭?

 

二,使用存储过程msdb.dbo.sp_send_dbmail

在Execute SQL Task中使用存储过程msdb.dbo.sp_send_dbmail 发送数据库邮件

 

三,使用Script Task,编写脚本发送mail

编写C#代码发送mail,主要是使用System.Net.Mail 类库来实现,需要添加命名空间

using System.Net.Mail; 

使用Script Task,能够使用HTML格式,也能使用密码进行验证,因此适用面光,能够编写格式比较丰富的邮件。

示例代码

复制代码
 public void Main()
        {
            //Define Smtp client
            int smtpPort = 25;
            String smtpServer = "smtp server url";
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = smtpServer;
            smtpClient.Port = smtpPort;

            //Use Password Authentication
            string loginUser = "";
            string loginPwd = "";

            System.Net.NetworkCredential myCredentials =
                new System.Net.NetworkCredential(loginUser, loginPwd);
            smtpClient.Credentials = myCredentials;

            //Use anonymous Authentication
            //smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;


            //define mail message
            MailMessage message = new MailMessage();

            //Define mail address
            MailAddress fromAddress = new MailAddress("Sender Address", "Sender Name");
            MailAddress toAddress = new MailAddress("Recipient Address", "Recipient Name");

            message.From = fromAddress;
            message.To.Add(toAddress);
            //message.To.Add("Recipient Address");

            message.Subject = "mail subject";

            //Define Mail Body
            message.Body = "text Mail Body";
            message.IsBodyHtml = false;

            //message.Body = "html mail body";
            //message.IsBodyHtml = true;

            // add attachment
            string strAttachedFilePath="";
            Attachment attachment = new Attachment(strAttachedFilePath);
            message.Attachments.Add(attachment);

            //Define Mail Priority
            int iPriority = 1;
            switch (iPriority)
            {
                case 1:
                    message.Priority = MailPriority.High;
                    break;
                case 3:
                    message.Priority = MailPriority.Low;
                    break;
                default:
                    message.Priority = MailPriority.Normal;
                    break;
            }

            smtpClient.Send(message);

            //Dispose attachment
            attachment.Dispose();

            //Dispose Message
            message.Dispose();

            //Dispose Stmp client
            smtpClient.Dispose();


            // Close Script Task with success 
            Dts.TaskResult = (int)ScriptResults.Success;
        }
复制代码

注意:如果不将Attachment Dispose,循环使用附件时,SSIS会报附件正在被其他process使用的异常。

 

参照资料:

http://www.cnblogs.com/biwork/p/3999123.html

http://www.codeproject.com/Articles/85172/Send-Email-from-SSIS-with-option-to-indicate-Email

 

作者悦光阴
本文版权归作者和博客园所有,欢迎转载,但未经作者同意,必须保留此段声明,且在文章页面醒目位置显示原文连接,否则保留追究法律责任的权利。
分类: SSIS 组件




本文转自悦光阴博客园博客,原文链接:http://www.cnblogs.com/ljhdo/p/4844119.html,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
小程序 PHP Perl
laravel8(六)使用自定义邮件类发送邮件
当登录邮箱为腾讯企业邮箱的时候。 Phpmailer发送邮件就不好用了,具体哪里不好用,我没真没找到。 但是,邮件得发啊,怎么办呢?
26 2
|
SQL IDE 关系型数据库
Python2连接Mysql数据库并发送带附件的邮件
使用python连接mysql数据库读取数据,并发送带附件邮件
194 0
Python2连接Mysql数据库并发送带附件的邮件
VBS 自动发送邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 ...
911 0
|
安全 应用服务中间件 数据安全/隐私保护
为 Confluence 6 配置发送邮件消息
如何配置 Confluence 向外发送邮件: 进入  > 基本配置(General Configuration) > 邮件服务器(Mail Servers)。
2374 0
|
关系型数据库 Oracle BI
|
数据安全/隐私保护