Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,
Here i want to send using stored procedure in sql server 2008 .

I got code from net it is like that

CREATE PROCEDURE [dbo].[sp_send_mail]
		@from varchar(500) ,
		@to varchar(500) ,
		@subject varchar(500),
		@body varchar(4000) ,
		@bodytype varchar(10),
		@output_mesg varchar(10) output,
		@output_desc varchar(1000) output
AS
DECLARE @imsg int
DECLARE @hr int
DECLARE @source varchar(255)
DECLARE @description varchar(500)

EXEC @hr = sp_oacreate 'cdo.message', @imsg out

--SendUsing Specifies Whether to send using port (2) or using pickup directory (1)
EXEC @hr = sp_oasetproperty @imsg,
'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").value','2'

--SMTP Server
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").value', 
  'smtp.gmail.com' 

--UserName
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").value', 
  'shinde.sshinde.sujata@gmail.com' 

--Password
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").value', 
  'Balkrishna' 

--UseSSL
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl").value', 
  'True' 

--PORT 
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").value', 
  '465' 

--Requires Aunthentication None(0) / Basic(1)
EXEC @hr = sp_oasetproperty @imsg, 
  'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").value', 
  '1' 

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

SET @output_mesg = 'Success'

-- sample error handling.
IF @hr <>0 
	SELECT @hr
	BEGIN
		EXEC @hr = sp_oageterrorinfo null, @source out, @description out
		IF @hr = 0
		BEGIN
			--set @output_desc = ' source: ' + @source
			set @output_desc =  @description
		END
	ELSE
	BEGIN
		SET @output_desc = ' sp_oageterrorinfo failed'
	END
	IF not @output_desc is NULL
			SET @output_mesg = 'Error'
END
EXEC @hr = sp_oadestroy @imsg


What I have tried:

Now i want execute that code.
i have tried like this
DECLARE @out_desc varchar(1000),
		@out_mesg varchar(10)

EXEC sp_send_mail 'sender@gmail.com',
	'receiver@gmail.com',
	'Hello', 
	'This is s Test Mail',
	'htmlbody', @output_mesg = @out_mesg output, @output_desc = @out_desc output

PRINT @out_mesg
PRINT @out_desc



it gives me error like this

-
-2147220973

and error message like
(1 row(s) affected)
Error
The transport failed to connect to the server.


Please give me any idea
Posted
Updated 11-Aug-20 18:29pm

Check if your google settings is allowing to send email by using any other smtp protocol
Read https://support.google.com/accounts/answer/6010255?hl=en
and go to https://www.google.com/settings/security/lesssecureapps and change settings to "Activated"
 
Share this answer
 
v2
It works when I changed smtpserverport to 25
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900