Wednesday, March 7, 2012

Change "from" portion of the email when calling sp_send_dbmail?

I want to be able to call sp_send_dbmail and be able to change the "from" portion of the email (the email and the display name) programmatically/data-driven as a sproc constructs and sends the email.

Is this possible?
I believe the only way to do this is to use different profiles|||Take a look at xp_smtp_sendmail http://www.sqldev.net/xp/xpsmtp.htm.|||To enhance Euan's reply refer to - http://www.sql-server-performance.com/da_email_functionality.asp & http://www.sqlservercentral.com/columnists/cBunch/introtodatabasemailinsql2005.asp links.|||

Your answer is not very practical when you have hundreds of users.

In the older SQL Server 2000 we used

Code Snippet

exec @.rc = master.dbo.xp_smtp_sendmail

@.FROM = @.in_FROM_ADDRESS,

@.FROM_NAME = @.in_FROM_NAME,

@.TO = @.in_TO_ADDRESS,

@.CC = @.in_CC,

@.BCC = @.in_BCC,

@.priority = @.in_priority,

@.subject = @.in_subject,

@.type = @.in_type,

@.message = @.in_message

Which had the first paramater @.From and the second one was @.From_Name.

Both of which are now gone in the new send dbmail

Code Snippet

exec @.rc = msdb.dbo.sp_send_dbmail

@.profile_name = 'MIS',

@.recipients = @.in_TO_ADDRESS,

@.copy_recipients = @.in_CC,

@.blind_copy_recipients = @.in_BCC,

@.importance = @.in_priority,

@.subject = @.in_subject,

@.body = @.in_message

The resulting emails are coming from whatever you set the Profile name to be. As you would by default set the profile to be generic, something like 'MIS Server Mailer' or whatever your application name is. Hense all the mails now come from this generic user.

This is a problem, and MS should extend the DB Mail to include this rather simple option.

No comments:

Post a Comment