【问题标题】:SQL Server Mail senging error using sp_send_dbmail on gmail server在 gmail 服务器上使用 sp_send_dbmail 的 SQL Server 邮件发送错误
【发布时间】:2020-03-01 13:31:09
【问题描述】:

我正在设置基于触发器的邮件通知,在使用它时,我也遇到了错误。

配置文件设置如下:

EXECUTE msdb.dbo.sysmail_add_account_sp @account_name               = 'TestMailAccount',
                                        @description                = 'Test Mail Account for sending notifications',
                                        @email_address              = 'my_gmail_id@gmail.com',
                                        @display_name               = 'Test Mail Notification',
                                        @username                   = 'my_gmail_id@gmail.com',
                                        @password                   = 'my_gmail_password',
                                        @mailserver_name            = 'smtp.gmail.com',
                                        @port                       = 587,
                                        @enable_ssl                 = 1;

EXECUTE msdb.dbo.sysmail_add_profile_sp @profile_name = 'TestMailProfile',
                                        @description = 'Main test profile used to send notification email';

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp  @profile_name       = 'TestMailProfile',
                                                @account_name       = 'TestMailAccount',
                                                @sequence_number    = 2;

EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
                                                @profile_name   = 'TestMailProfile',
                                                @principal_name = 'public',
                                                @is_default     = 0;

现在要发送邮件,我执行了:

DECLARE @mail_body NVARCHAR(MAX);
SET @mail_body  = CONCAT(   N'<html>',
                                N'<body>',
                                    N'<h1>Test Mail</h1>',
                                N'</body>',
                            N'</html>');
EXECUTE msdb.dbo.sp_send_dbmail 
        @profile_name   = 'TestMailProfile', 
        @recipients     = 'dest@gmail.com', 
        @subject        = N'DB Test Mail', 
        @body           = @mail_body,
        @body_format    = 'HTML';

在这之后,我检查了日志:

select * from sysmail_event_log

描述显示:

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 42 (2020-03-01T18:41:09). Exception Message: Cannot send mails to mail server. (The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at).

我已经启用了谷歌帐户的设置以使用不太安全的应用访问。

我不确定我错过了什么,任何帮助都将得到高度评价。

【问题讨论】:

  • 你试过 465 端口吗?
  • @lptr: 没试过,使用端口 465,错误是“由于邮件服务器故障,无法将邮件发送给收件人。(使用帐户 43 发送邮件(2020- 03-01T19:25:06)。异常消息:无法向邮件服务器发送邮件。(发送邮件失败。)。”
  • 用端口 587 测试它并且它工作正常
  • @lptr 你做了什么不同的事情吗?如果是这样,请分享脚本/设置等?
  • 我已经使用了您的设置脚本(只是更改了帐户的凭据)。 (也开启了不太安全的应用访问)

标签: sql-server sql-server-2014 sql-server-2014-express sp-send-dbmail dbmail


【解决方案1】:

Gmail 设置看起来正确。带有“尝试不同的端口”的警告(在上面原始问题的 cmets 中也提到过)

来自https://granadacoder.wordpress.com/2006/02/08/smarter-emailsmtp-setup-with-dotnet-configuration-sections-1-1-and-2-0/

<!–Note, with the 2.0 Framework, my tests show that gmail likes port 587–>

    <smtpServer 
        smtpServerName="smtp.gmail.com" 
        defaultEmailFrom="donotreply@gmail.com" 
        portNumber="465" 
        authenicationMode="SSL" 
        smtpUserName="mygmailaddress@gmail.com" 
        smtpUserPassword="mygmailpassword"/>

更可能的是,您可能有端口阻塞。

https://www.microsoft.com/en-us/download/details.aspx?id=24009

这是我用来 ping 端口的。如果您可以在 sql-server 机器上安装这个小实用程序,它将有很长的路要走。

如果你在 linux 上运行 sql-server...你可以“apt install curl”(或类似的)并以这种方式 ping 端口。

但是从 sql-server 发送电子邮件一直很棘手。您需要确切的设置,并且需要端口(取消)块。大多数时候是端口问题。

如果上面的 portqueryui 工具报告“已阻止”或“已过滤”...您可能必须在您的计算机上打开一个端口。

https://support.microsoft.com/en-us/help/4028544/windows-10-turn-windows-defender-firewall-on-or-off

您的路由器/网络也可以阻止端口。同样,portqueryui 工具是一种快速测试从您尝试离开的机器到 Internet 的路径的方法。

【讨论】:

  • 我目前正在我的家庭网络上尝试这个,这里不应该阻止任何东西(我认为)。关于Linux,不,我使用的是Windows PC。关于安装任何附加组件,我认为这是不可能的,因为安装程序将安装软件并且无法更改 SQL 配置。
  • 您的个人计算机可以阻止该端口。我引用的工具是一种简单的检查方法。
  • 该工具的结果是:正在启动 portqry.exe -n smtp.gmail.com -e 587 -p TCP ... 查询目标系统称为:smtp.gmail.com 尝试将名称解析为IP 地址...名称解析为 108.177.125.109 查询... TCP 端口 587(未知服务):LISTENING portqry.exe -n smtp.gmail.com -e 587 -p TCP exits with return code 0x00000000.
猜你喜欢
  • 1970-01-01
  • 2016-09-07
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 2011-05-13
  • 2023-03-07
相关资源
最近更新 更多