February 23, 2008

How to send an email using SQL Server 2005




With SQL Server 2005, there is no need to use MAPI client to send emails. Fellow developers who have used MAPI in the previous versions of SQL Server are well aware of the challenges it had. However in Sql Server 2005, we can now use the Database Mail to send emails.

[Note: I assume you have set up and configured Database Mail. If not, check this link Database Mail Configuration Stored Procedures to use various stored procedures required to configure Database Mail. As an alternative, you could also use the SQL Server Surface area configuration tool to configure Database Mail]

Use the following script to send a mail from your Sql Server

USE [YourDB]
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'admin@xyz.com; xyz@xyz.com; pqr@xyz.com',
@body = 'Just testing the mail',
@subject = 'Sending Mail using Database Mail' ;
GO


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

9 Responses to "How to send an email using SQL Server 2005"
  1. Chris said...
    June 19, 2008 at 3:35 AM

    Thanks, dude - this was exactly what I was looking for! Much appreciated...

    Chris

  2. Kapil Jain said...
    October 20, 2008 at 10:38 AM

    Thanks a lot for this starter kit!

  3. Paúl said...
    November 17, 2008 at 8:17 AM

    Thanks for your help, it works perfect. But If you let me a little comment, one parameter is missing:

    USE [YourDB]

    EXEC msdb.dbo.sp_send_dbmail

    @profile_name = 'Your Profile',
    @recipients = 'yourmail@yourdomain.com',
    @body = 'Just testing the mail',
    @subject = 'Sending Mail using Database Mail' ;

    GO

    @profile_name is the name of the profile that you've setup on DataBase Mail before try to sending
    mails.

    Hope this helps.

    Once again thanks for your help and advice.

    Makron

  4. Suprotim Agarwal said...
    November 30, 2008 at 2:41 AM

    Thanks for that input Paul.

  5. Jay Edgar said...
    January 20, 2009 at 3:03 PM

    Yeah, thanks!

  6. Anonymous said...
    March 10, 2009 at 12:22 AM

    Can Anyone Tell me "how to send an audit report generated by auditing tool( Which i am developing) to a mail address automatically after span of 1 month"

  7. Suprotim Agarwal said...
    March 12, 2009 at 7:11 AM

    You would have to create a job using SQL Server Management Studio or using T-SQL to send emails automatically after a given period of time.

  8. priyab said...
    March 13, 2009 at 1:06 PM

    I also want to know how make this run every day and send email alerts with the query results

  9. Suprotim Agarwal said...
    March 13, 2009 at 11:45 PM

    Priyab: Some links to help you out
    http://support.microsoft.com/kb/908360

    If you intend using a trigger
    http://blog.netnerds.net/2008/02/create-a-basic-sql-server-2005-trigger-to-send-e-mail-alerts/

 

Copyright © 2009-2014 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions