SSIS Send Mail

在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使用的异常。

时间: 2024-10-08 22:22:17

SSIS Send Mail的相关文章

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. Your package may look something like this: Double-click the Send Mail Task to open the Send Mail Task Editor (shown below). Fill in all the relevant fie

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

发送邮件的三种方式: 1.VBS 执行vbs脚本文件的程序为: system32文件下的 NameSpace = "http://schemas.microsoft.com/cdo/configuration/" Set Email = createObject("CDO.Message") Email.From = "xxx" '發送者 Email.To = "xxx;xxx" '收件地址 Email.Subject = &

IBM supervessel power云平台 之 send mail 篇

声明 : 此文档只做学习交流使用,请勿用作其他商业用途 author : 朝阳_tony E-mail : [email protected] Create Date: 2015-3-9 22:55:43 Monday Last Change: 2015-3-9 22:55:52 Monday 转载请注明出处:http://blog.csdn.net/linzhaolover 摘要: 朝弟,咱们今天有个新的任务,在你的机器上运行一个测试实例,然后将结果通过mail发送到我的邮箱,方便后期查阅!先

how to send mail from 3rd

http://www.advancedintellect.com/post/2011/03/02/Exchange-2010-and-SMTP-settings.aspx Enviroment: Exchange 2010, 2013 which only have hub role and other roles 1. we should use the followign code to triggle sending mail script: http://www.daixiaorui.c

Python 3.4 send mail

#coding=utf-8 #Python 3.4 https://docs.python.org/3.4/library/ #IDE:Visual Studio 2015 Window10 import atexit import os import unicodedata import sys import time import unicodedata import winsound import code import codecs import math import csv impo

python send mail

plese find www.baidu.com

login SMTP send mail error : Unable to read data from the transport connection: net_io_connectionclosed

client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass); client.Credentials = new NetworkCredential([email protected], sSMTPpass); 用户名需要带上域名,真是脑残的邮件服务器: 亿邮.

EWS send mail meeting, read meeting message(一)

最近做了一个项目,负责的主要模块,是web系统跟 OutLook 数据相互同步,就是从web发送邮件,会议邀请到 OutLook,然后把OutLook客户端发送的Meeting同步到web系统中 首先是读取OutLook 1 用有权限的账号登陆EWS服务 读取指定邮箱 的Meeting.这个账号是一个权限比较大的账号,可以模拟别的邮箱,读取被模拟邮箱的邮件,如果是普通用户邮箱,要读取自己的邮件信息,只需要开通访问EWS服务的权限. 1 static void TestEws() 2 { 3 tr

html send mail

<html> <body> <script> var formattedBody = "FirstLine \n Second Line \n Third Line"; var mailToLink = "mailto:[email protected]?body=" + encodeURIComponent(formattedBody); window.location.href = mailToLink; </scrip