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
|
No responses found. Be the first to respond and make money from revenue sharing program.
|