C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to send&Log HTML fromated mail message from SQL Server.


Posted Date: 17 Jul 2007    Resource Type: Code Snippets    Category: SQL

Posted By: Shaji Vasudevan       Member Level: Bronze
Rating:     Points: 10



You should have ASPQmail configuration utility and SMTPSvg.Mailer component on your machine.
Usage:

exec sp_SMTPMail
@SenderName='Damian',
@SenderAddress='damian@bdussy.com',
@RecipientName = 'Someone',
@RecipientAddress 'someone@someplace.com',
@Subject='SQL Test',
@body='Hello, this is a test email from SQL Server'

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO

Create Procedure dbo.sp_SMTPMail
@SenderName varchar(100),
@SenderAddress varchar(100),
@RecipientName varchar(100),
@RecipientAddress varchar(100),
@Subject varchar(200),
@Body varchar(8000),
@MailServer varchar(100) = 'RemoteMailServer.com'
AS
SET nocount on
--No surprises here. Set up the procedure and the parameters. I have set up the SMTP mailserver parameter to remote mail server machine.
declare @oMail int --Object reference
declare @resultcode int
EXEC @resultcode = sp_OACreate 'SMTPsvg.Mailer', @oMail OUT
--OUTsp_OACreate has an output parameter that returns a reference to the object instance, this is used to assign parameters. The result code is 0 for success.
if @resultcode = 0
BEGIN
EXEC @resultcode = sp_OASetProperty @oMail, 'RemoteHost', @mailserver
EXEC @resultcode = sp_OASetProperty @oMail, 'FromName', @SenderName
EXEC @resultcode = sp_OASetProperty @oMail, 'FromAddress', @SenderAddress
--sp_OASetProperty takes the reference to the object (@oMail), takes the property name and sets the value.
EXEC @resultcode = sp_OAMethod @oMail, 'AddRecipient', NULL, @RecipientName, @RecipientAddress
--sp_OAMethod calls a method of the object. We pass the object reference, then the method name "AddRecipient". The next parameter is for returning a value from a method. In this case I don't want one so I pass it a null. After that I pass the parameters of the method. The "AddRecipient" method has Name and Email Address parameters so I am passing them in.
EXEC @resultcode = sp_OASetProperty @oMail, 'Subject', @Subject
EXEC @resultcode = sp_OASetProperty @oMail, 'BodyText', @Body
EXEC @resultcode = sp_OASetProperty @oMail, 'ContentType', 'text/html'
EXEC @resultcode = sp_OASetProperty @oMail, 'QMessage' , true
EXEC @resultcode = sp_OAMethod @oMail, 'SendMail', NULL
--Similar code here, we set the subject and message body, then call the "SendMail" method which sends the email.
EXEC sp_OADestroy @oMail
END
SET nocount off

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Automate Reindexing In SQL Server 2000
Previous Resource: Find Week in Given Date in SQL Server
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use