sp_send_dbmail(T-SQL)

时间:2013-11-26 20:31:47

标签: sql-server tsql email send

对于我需要查询表格并使用结果填充电子邮件消息的CC列表,我对这类事情还是比较陌生的。我找不到任何具体的例子。我在SQL服务器中使用Job来完成此任务。发送给一个收件人和一个CC收件人工作得很好,但我需要发送多个CC。任何建议都会非常感激。

这是我向一位收件人发送电子邮件的代码:

declare     @chvFirstName nvarchar(100),
@chvLastName nvarchar(100),
@chvEArea nvarchar(5),
@chvAppDate nvarchar(20),
@chvExpDate nvarchar(20),
@chvEmail varchar(50),
@chvEmailMessage varchar(800)

declare @CrsrVar Cursor

Set @CrsrVar = Cursor For
Select App.dbo.CommitteeAPC.AppointDate, App.dbo.CommitteeAPC.ExpiryDate, App.dbo.CommitteeAPC.ElectoralArea, App.dbo.CommitteeMemberContact.FirstName,  App.dbo.CommitteeMemberContact.LastName, App.dbo.CommitteeAPC.StaffEmail
From RDOS_App.dbo.CommitteeAPC INNER JOIN App.dbo.CommitteeMemberContact ON App.dbo.CommitteeAPC.MemberID = App.dbo.CommitteeMemberContact.MemberID
Where CurrentDate > NotifDt

Open @CrsrVar

 Fetch Next From @CrsrVar
Into @chvAppDate, @chvExpDate, @chvEArea, @chvFirstName, @chvLastName, @chvEmail

While(@@FETCH_STATUS = 0)

Begin

set @chvEmailMessage = 'DO NOT REPLY TO THIS MESSAGE! This is an automatic e-mail notification ' +
    'message sent to you from the Committees Database.' +
    CHAR(13) + CHAR(10)+
    CHAR(13) + CHAR(10)+
    'The following APC membership will expire in approximately 30 days.' +
    CHAR(13) + CHAR(10)+
    CHAR(13) + CHAR(10)+
    'Name: ' + @chvFirstName + ' ' + @chvLastName +
    CHAR(13) + CHAR(10)+
    CHAR(13) + CHAR(10)+
    'Area: ' + @chvEArea + ' Advisory Planning Commission' +
    CHAR(13) + CHAR(10)+
    'Date Appointed: ' + @chvAppDate +
    CHAR(13) + CHAR(10)+
    'Date of Expiry: ' + @chvExpDate 

--Use Master

Exec msdb.dbo.sp_send_dbmail
@profile_name = 'App Database Mail Profile',
@recipients = @chvEmail,
@subject = 'An APC Membership will expire within 30 days',
@body = @chvEmailMessage

Fetch Next From @CrsrVar
Into @chvAppDate, @chvExpDate, @chvEArea, @chvFirstName, @chvLastName, @chvEmail

End

Close @CrsrVar
Deallocate @CrsrVar

3 个答案:

答案 0 :(得分:2)

您可以在@chvEmail中连接多个收件人。用分号分开它们。

SET @chvEmail = 'user1@domain1.com; user2@domain2.com'

sp_send_dbmail只是将@recipients字符串按原样传递给MX,没有验证。

如果你有一个电子邮件地址表,你可以将它们全部连接成一个字符串:

DECLARE @email_to varchar(max);
SELECT @email_to = COALESCE(@email_to + ';', '') + [email] FROM [email_addresses]

答案 1 :(得分:0)

宣布@cc_emailaddress 设置@cc_emailaddress ='a@abc.com; b@abc.com'

答案 2 :(得分:0)

答案取决于:

  1. 如果您想向所有人发送相同的信件 - 您可以看到答案1

  2. 如果您使用发送SPECIFIC字母(@chvAppDate,@ chpExpDate,@ chvEArea,例如可以从一个用户更改为其他用户) 看到我的回答:)

    declare @chvFirstName nvarchar(100),
    @chvLastName nvarchar(100),
    @chvEArea nvarchar(5),
    @chvAppDate nvarchar(20),
    @chvExpDate nvarchar(20),
    @chvEmail varchar(256),
    @chvEmailMessage nvarchar(800)
    
    declare CrsrVar Cursor FAST_FORWARD For
    Select APC.AppointDate, APC.ExpiryDate, APC.ElectoralArea, CMC.FirstName, CMC.LastName, APC.StaffEmail
    From RDOS_App.dbo.CommitteeAPC APC
    JOIN App.dbo.CommitteeMemberContact CMC ON APC.MemberID = CMC.MemberID
    Where CurrentDate > NotifDt
    
    Open CrsrVar
    
    Fetch Next From CrsrVar
    Into @chvAppDate, @chvExpDate, @chvEArea, @chvFirstName, @chvLastName, @chvEmail
    
    While(@@FETCH_STATUS = 0)
    
    Begin
    select @chvEmailMessage = 'DO NOT REPLY TO THIS MESSAGE! '+
    'This is an automatic e-mail notification message sent to you from the Committees Database.' + CHAR(10) + CHAR(10)+
    'The following APC membership will expire in approximately 30 days.' + CHAR(10) + CHAR(10)+
    'Name: ' + COALESCE(@chvFirstName+' ','') + isnull(@chvLastName,'') + CHAR(10) + CHAR(10)+
    COALESCE('Area: ' + @chvEArea + ' Advisory Planning Commission' + CHAR(10),'') + 
    'Date Appointed: ' + isnull(@chvAppDate,'') + CHAR(10)+
    'Date of Expiry: ' + isnull(@chvExpDate,'') 
    
    --Use Master
    
    Exec msdb.dbo.sp_send_dbmail
                    @profile_name = 'App Database Mail Profile',
                    @recipients = @chvEmail,
                    @subject = 'An APC Membership will expire within 30 days',
                    @body = @chvEmailMessage
    
    Fetch Next From CrsrVar
    Into @chvAppDate, @chvExpDate, @chvEArea, @chvFirstName, @chvLastName, @chvEmail
    
    End
    
    Close CrsrVar
    Deallocate CrsrVar
    
  3. 欢迎您提出任何问题