SSIS Send Mail

时间:2022-10-01 15:14:31

在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的配置有关

SSIS Send Mail

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 = ;
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 = ;
switch (iPriority)
{
case :
message.Priority = MailPriority.High;
break;
case :
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 Send Mail的更多相关文章

  1. How to attach multiple files in the Send Mail Task in SSIS

    Let’s say you need to create a SSIS package that creates 2 files and emails the files to someone. Yo ...

  2. mailsend - Send mail via SMTP protocol from command line

    Introduction mailsend is a simple command line program to send mail via SMTP protocol. I used to sen ...

  3. 发送邮件的三种方式:Send Mail Message

    发送邮件的三种方式: 1.VBS 执行vbs脚本文件的程序为: system32文件下的 NameSpace = "http://schemas.microsoft.com/cdo/conf ...

  4. Send Mail using C# code

    using System.Net.Mail; public class MailHelp { public static void Send(string subject, string body) ...

  5. golang:send mail using smtp package

    go语言发送邮件,可以使用smtp包,两个关键函数: func PlainAuth(identity, username, password, host string) Auth func SendM ...

  6. Python 3.4 send mail

    #coding=utf-8 #Python 3.4 https://docs.python.org/3.4/library/ #IDE:Visual Studio 2015 Window10 impo ...

  7. python * development 1st —— use python to send mail and caputre the screen then combine them

    import smtplib from email.mime.text import MIMEText msg_from='1@qq.com' #发送方邮箱 passwd='bd' #填入发送方邮箱的 ...

  8. C# send mail with outlook and word mailmerge

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.document_members(v=office.15). ...

  9. Send Mail 网址

    http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp http://www.c- ...

随机推荐

  1. 深入理解JavaScript的闭包特性如何给循环中的对象添加事件

    初学者经常碰到的,即获取HTML元素集合,循环给元素添加事件.在事件响应函数中(event handler)获取对应的索引.但每次获取的都是最后一次循环的索引.原因是初学者并未理解JavaScript ...

  2. PHP小总结

    <?php //1.php基础语法 //输出语句 echo print print_r var_dump() //2.php是弱类型语言 //强制转换类型:(类型)变量 settype(变量,类 ...

  3. HTML &lt&semi;map&gt&semi; 标签-创建带有可点击区域的图像映射

    定义和用法 定义一个客户端图像映射.图像映射(image-map)指带有可点击区域的一幅图像. 所有主流浏览器都支持 <map> 标签. 注释:area 元素永远嵌套在 map 元素内部. ...

  4. CodeForces 34B Sale

    Sale Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status ...

  5. sqlmap基本命令

    ./sqlmap.py –h //查看帮助信息./sqlmap.py –u “http://www.anti-x.net/inject.asp?id=injecthere” //get注入./sqlm ...

  6. Python自学笔记-map和reduce函数(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. Python内 ...

  7. html统计

    <!doctype html><html lang="en"> <head>  <meta charset="UTF-8&quo ...

  8. Windows Server 2012 R2安装Oracle 11g问题

      1.[INS-13001]环境不满足最低要求 如图:   oracle11g早于Windows Server 2012 R2 解决方法:找到解压目录../win64_11gR2_database\ ...

  9. grafana安装使用及与zabbix集成

    grafana简介Grafana是一个完全开源的度量分析与可视化平台,可对来自各种各种数据源的数据进行查询.分析.可视化处理以及配置告警. Grafana支持的数据源:官方:Graphite,Infl ...

  10. react-native中的setNativeProps

    如果你通过React.createClass方法自定义了一个组件,直接给它设置样式 prop 是不会生效的,你得把样式 props 层层向下传递给子组件 ,直到子组件是一个能够直接定义样式的原生组件. ...