首页 > 代码库 > 使用数据库邮件发送会议邀请

使用数据库邮件发送会议邀请

http://social.microsoft.com/Forums/it-IT/3ba417d6-6548-48ed-9e9f-1a6549edc569

大家好,

在此提供一个示例展示如何使用数据库邮件发送会议邀请。
创建CLR 存储过程,用来生成.ics文件:
1. 在 Visual Studio中,创建一个Visual C# SQL CLR Database Project,请看:


2. 添加一个数据库引用,请看:



3. 创建一个CLR存储过程,命名为CLROutLook,请看:
 

4. 使用System.IO.File.WriteAllLines 方法,创建一个.ics 文件,请看:
 [Microsoft.SqlServer.Server.SqlProcedure]
  public static void CLROutlook(string MeetingLocation,string MeetingSubject,string MeetingDescription,DateTime BeginDate,DateTime EndDate)
  {
    // Put your code here
    String[] contents = { "BEGIN:VCALENDAR",
               "PRODID:-//Flo Inc.//FloSoft//EN",
               "BEGIN:VEVENT",
               "DTSTART:" + BeginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"), 
               "DTEND:" + EndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"), 
               "LOCATION:" + MeetingLocation, 
              "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + MeetingDescription,
               "SUMMARY:" + MeetingSubject, "PRIORITY:3", 
              "END:VEVENT", "END:VCALENDAR" };
    System.IO.File.WriteAllLines(@"E:\Meeting\OutLookMeeting.ics", contents);
  }


注明:我们需要使用System.IO 命名空间,在SQL Server 中,CLR集成不支持该命名空间,当我们部署该CLR 存储过程时,我们可以打开该工程文件属性,设置权限,如下图:
 


5. 我们也可以使用T-SQL 语句来注册该程序集,请参阅下面的语句:

--Set the database to TRUSTWORTHY mode
ALTER DATABASE Test
SET TRUSTWORTHY ON;
GO

CREATE ASSEMBLY CLROutLook
FROM ‘E:\CLR\OutLook\CLROutLook\CLROutLook\bin\Debug\CLROutLook.dll‘
WITH PERMISSION_SET = UNSAFE

6. 创建存储过程,请参阅下面的语句:

CREATE PROCEDURE [dbo].[CLROutlook]
 @MeetingLocation [nvarchar](4000),
 @MeetingSubject [nvarchar](4000),
 @MeetingDescription [nvarchar](4000),
 @BeginDate [datetime],
 @EndDate [datetime]
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [CLROutLook].[StoredProcedures].[CLROutlook]
GO

配置数据邮件:

1. 配置文件和邮件账号:

 



2. 使用sp_send_dbmail 发送邮件:
--create a .ics file
DECLARE @MeetingLocation NVARCHAR(50)
DECLARE @MeetingSubject NVARCHAR(50)
DECLARE @MeetingDescription NVARCHAR(500)
DECLARE @BeginDate DATETIME
DECLARE @EndDate DATETIME
SET @MeetingLocation=‘3106‘
SET @MeetingSubject=‘SQL Replication Training‘
SET @MeetingDescription=‘Replication is a set of technologies for copying and distributing data and database objects from one database to another and then synchronizing between databases to maintain consistency‘
SET @BeginDate=‘2010-12-25 10:00:000‘
SET @EndDate=‘2010-12-25 11:00:000‘
EXEC dbo.CLROutlook @MeetingLocation,@MeetingSubject,@MeetingDescription,@BeginDate,@EndDate
GO
--Send Email

EXEC msdb.dbo.sp_send_dbmail
   @profile_name = ‘TestProfile‘,
   @recipients = ‘Test@example.com‘,
   @subject = ‘SQL Replication Training invitation‘,
   @file_attachments=‘E:\Meeting\OutLookMeeting.ics‘

结果为:
 

关于数据库邮件的小贴士:
1. 关于数据库邮件的更多信息,请参阅:
数据库邮件
http://msdn.microsoft.com/zh-cn/library/ms175887(v=SQL.90).aspx

谢谢。