sp_OAmethod send返回true但邮件没有收到

时间:2016-01-20 01:27:05

标签: sql-server email stored-procedures ole cdo.message

我想从SQL Server发送邮件,我有来自Godaddy的专用服务器 我尝试以下

启用OLE

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

创建SP

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', 'dedrelay.secureserver.net'

    --UserName
    EXEC @hr = sp_OASetProperty @imsg, 'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").value', 'sender email'

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

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

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

    --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

执行存储过程

EXEC sp_send_mail
        'sender email',
        'receiver email ',
        'Hi',
        'Test Message',
        'htmlbody',
        @output_mesg = @out_mesg OUTPUT,
        @output_desc = @out_desc OUTPUT

PRINT @out_mesg
PRINT @out_desc

它返回成功,但没有收到邮件,换句话说

 EXEC @hr = sp_oamethod @imsg, 'send', null

上面这一行

返回0并且根据msdn,0表示成功,那么为什么我看不到收件箱收件箱中的邮件?

0 个答案:

没有答案