how to send emails when records inserted?

Posted on 2002-03-06
Last Modified: 2012-05-04
I like to send the email when the record in a table is inserted. the table will have the email address already.

should I use trigger in the table, never used it before, anyone provide some simple code to get me started.

yeah, i know, i will look into the sql help too, but i like to have people experience if i have problem understaning the sql help.

how to setup the email on the sql server as well.

Question by:chrislee8
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

meos earned 50 total points
ID: 6844922
you may use SQL built in SQL Mail with stored procedures: xp_sendmail; but also external utility (ie: sendmail.exe)

use triggers for table as:
ON titles
   EXEC master..xp_sendmail 'MaryM',
      'Don''t forget to print a report for the distributors.'

or xp_cmdshell
ON titles
   EXEC master..xp_cmdshell 'c:\sendmail.exe "'Don''t forget to print a report for the distributors."'

hope it helps
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 6846869
As meos indicates, using the xp_cmdshell inside the trigger is the "simple" solution.
However, this has some disadvantages:
* the insert statment will be delayed if any problems appear during the mail send
* the mail send cannot be tracked
* the mail send cannot easily be configured

Thus, i do not send the email directly, but insert a short record in a working table (like "TODO_SENDEMAIL_REMINDER"), with has only the ID fields to the related table, a timestamp field and a flag saying if the email has been send...
To send the emails, you can still use xp_cmdshell, by running a job for example, looping on that table. Of course, you can also use some other external process and mailing tools...


Expert Comment

ID: 9280792
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question