4

I want to send E-mail from SQL Server 2008. What are the basic steps for that? Can you explain me if someone used this facility.

Any help will appreciated.

this link will helpful as commented by marc_s (also I have accepted an answer below) http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/

asked Jan 5, 2012 at 5:14
7
  • @idstam How to move the same Question from SO to other? Commented Jan 5, 2012 at 5:28
  • 4
    There are many articles out there google.co.uk/search?q=sql+server+dbmail Commented Jan 5, 2012 at 5:58
  • 2
    See: blog.sqlauthority.com/2008/08/23/… - use the new, current "SQLMail" facilities - not old, deprecated solutions like "DatabaseMail" or even "Outlook activation" Commented Jan 5, 2012 at 6:12
  • @marc_s Thanks Very usefull blog post.... Commented Jan 5, 2012 at 6:50
  • 3
    @marc_s: DB Mail is new, SQL Mail is legacy... Commented Jan 5, 2012 at 12:03

3 Answers 3

3

In management studio configure the database mail option under "Management" (both the account and the profile) and then use the stored procedure sp_send_dbmail in the msdb database.

http://msdn.microsoft.com/en-us/library/ms190307.aspx

answered Jan 5, 2012 at 16:55
0
7
  1. In SSMS object Explorer, click Management
  2. Right-click Database Mail
  3. Click Configure Database Mail

enter image description here

4.Select Set up Database Mail by performing the following tasks

enter image description here

5.Enter a profile name and click Add

enter image description here

6.Enter the settings for your email account. I could not set the account for my company's email account - it just didn't work. But I had no trouble with setting the Gmail account I use and these are the parameters that you should use for the Server name and Port number

enter image description here

It is not possible to send attachments from SSMS, you have to use T-SQL for that:

EXEC msdb.dbo.sp_send_dbmail
@profile_name='carol Gmail',
@recipients='[email protected]',
@subject='Test email',
@body='Attachment test',
@file_attachments='D:123円\pic.jpg'
answered May 17, 2013 at 17:05
0
-4

Try the following SP.

CREATE procedure [dbo].[usp_send_cdosysmail]
@from varchar(500) ,
@to varchar(500) ,
@subject varchar(500),
@body varchar(max) ,
@smtpserver varchar(25),
@bodytype varchar(10) 
as
declare @imsg int
declare @hr int
declare @source varchar(255)
declare @description varchar(500)
declare @output varchar(1000)
exec @hr = sp_oacreate 'cdo.message', @imsg out
exec @hr = sp_oasetproperty @imsg,
'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").value','2'
exec @hr = sp_oasetproperty @imsg, 'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").value', @smtpserver 
exec @hr = sp_oamethod @imsg, 'configuration.fields.update', null
exec @hr = sp_oasetproperty @imsg, 'to', @to
exec @hr = sp_oasetproperty @imsg, 'from', @from
exec @hr = sp_oasetproperty @imsg, 'subject', @subject
-- if you are using html e-mail, use 'htmlbody' instead of 'textbody'.
exec @hr = sp_oasetproperty @imsg, @bodytype, @body
exec @hr = sp_oamethod @imsg, 'send', null
-- sample error handling.
if @hr <>0 
select @hr
begin
exec @hr = sp_oageterrorinfo null, @source out, @description out
if @hr = 0
begin
select @output = ' source: ' + @source
print @output
select @output = ' description: ' + @description
print @output
end
else
begin
print ' sp_oageterrorinfo failed.'
return
end
end
exec @hr = sp_oadestroy @imsg
answered Jan 5, 2012 at 5:58
3
  • Thanks for response. Can I include File attachment option? If How? Commented Jan 5, 2012 at 6:03
  • 2
    Way too complicated, way too risky (using the "sp_OA" stuff) and totally not necessary to do it this complicated..... just use the built-in SQL Server "SQLMail" - see how here: blog.sqlauthority.com/2008/08/23/… Commented Jan 5, 2012 at 6:13
  • 3
    -1 this is so dated and obsolete. If you can use CDO you can use DatabaseMail because it is SMTP based Commented Jan 5, 2012 at 12:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.