Sending Emails From SQL Server 2012 Stored Procedures

Paul Brown picture Paul Brown · Jan 4, 2013 · Viewed 9.1k times · Source

We require the ability to sent emails from stored procedures (SQL Server 2012) via SMTP.

We need to retain control over the emails "FROM" address and "Display Name" so this I believe rules out Database Mail as these are hard set in the created profiles (would be great to over ride tho! Anyone know how?)

What other options are available to us?

Thanks Paul

UPDATE: Would it be stupid to, in code, create the required temporary Database Mail profile, use it to send the required email once, and then delete the profile?

Answer

Alex K. picture Alex K. · Jan 4, 2013

From 2008 onwards you can create a basic profile and then override the details with @from_address/reply_to;

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'my profile',
    ...
    @subject = 'The Spice must flow',
    @from_address = '"Bob Smith" <[email protected]>',
    @reply_to = '[email protected]'