How to send email in store procedure

  • If record exists in table 1

    then

    send email to tom@yahoo.com with table1 data

    end if

  • What version of SQL Server is this for? How do you want the contents of the table sent, as a CSV attachment, inline to the body of the message, as a HTML message? More information is needed to provide a real solution to your question. In SQL 2005/2008 use database mail, in SQL 2000, it is a bit more involved.

    Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
    My Blog | Twitter | MVP Profile
    Training | Consulting | Become a SQLskills Insider
    Troubleshooting SQL Server: A Guide for Accidental DBAs[/url]

  • From within the database, you can use Database Mail (SQL2005) or SQL Mail (SQL2000). For SQL2005, there's a good article here:

    http://www.sql-server-performance.com/articles/dba/email_functionality_p4.aspx

    From an external application such as ASP.net, you might use something like CDO NTS (or a third party component)

  • 2005 sql server version

  • what kind of data you want to send, i have a full document for this point ( send email by database mail ) via procedure, what i have now to save data to table and send record by record via mail

  • I want to send data from the view. Thank you so much

  • check the following example i used it and it works fine but dont forget to enable ( database mail )

    ------------

    DECLARE @tableHTML NVARCHAR(MAX) ;

    SET @tableHTML =

    N' ' +

    N' ' +

    N' ' +

    N' ' +

    N' '+

    CAST ( ( SELECT td = Customer_Code, '',

    td = Customer_Name, '',

    td = City, ''

    FROM [customer].[dbo].customer

    WHERE customer_code = 121494

    FOR XML PATH('tr'), TYPE

    ) AS NVARCHAR(MAX) ) +

    N' ' ;

    EXEC MSDB.dbo.sp_send_dbmail

    @profile_name = 'HelpDesk',

    @recipients = 'ikrami2000@hotmail.com' ,

    @subject = 'Subject text',

    @body = @tableHTML,

    @body_format = 'HTML' ;

    ------------

  • DECLARE @tableHTML NVARCHAR(MAX) ;

    SET @tableHTML =

    N' ' +

    N' ' +

    N' ' +

    N' ' +

    N' '+

    CAST ( ( SELECT td = Customer_Code, '',

    td = Customer_Name, '',

    td = City, ''

    FROM [customer].[dbo].customer

    WHERE customer_code = 121494

    FOR XML PATH('tr'), TYPE

    ) AS NVARCHAR(MAX) ) +

    N' ' ;

    EXEC MSDB.dbo.sp_send_dbmail

    @profile_name = 'HelpDesk',

    @recipients = 'ikrami2000@hotmail.com' ,

    @subject = 'Subject text',

    @body = @tableHTML,

    @body_format = 'HTML' ;

  • Thank you so much

  • ikrami2000 (10/5/2008)


    DECLARE @tableHTML NVARCHAR(MAX) ;

    SET @tableHTML =

    N' ' +

    N' ' +

    N' ' +

    N' ' +

    N' '+

    CAST ( ( SELECT td = Customer_Code, '',

    td = Customer_Name, '',

    td = City, ''

    FROM [customer].[dbo].customer

    WHERE customer_code = 121494

    FOR XML PATH('tr'), TYPE

    ) AS NVARCHAR(MAX) ) +

    N' ' ;

    EXEC MSDB.dbo.sp_send_dbmail

    @profile_name = 'HelpDesk',

    @recipients = 'ikrami2000@hotmail.com' ,

    @subject = 'Subject text',

    @body = @tableHTML,

    @body_format = 'HTML' ;

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply