sql-server - 在 gmail 服务器上使用 sp_send_dbmail 的 SQL Server 邮件发送错误
问题描述
我正在设置一个基于触发器的邮件通知,在使用它时,我也遇到了错误。
Profile设置如下:
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).
我已经启用了谷歌帐户的设置以使用不太安全的应用程序访问。
我不确定我错过了什么,任何帮助都会得到高度评价。
解决方案
Gmail 设置看起来正确。带有“尝试不同的端口”的警告(在上面原始问题的评论中也提到过)
<!–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 工具是一种快速测试从您尝试离开的机器到互联网土地的路径的方法。
推荐阅读
- wordpress - 使用 Parent_id 和 GroupBy 获取自定义帖子类型
- oracle - ExecuteSqlRaw() 抱怨无效的 SQL 语句,但事实并非如此
- java - 为什么我的预编译二进制文件在 yocto 中发生了变化?
- docker - 下载前获取 docker 镜像的摘要哈希
- haskell - 在haskell中实现logbase2函数
- javascript - 仅在页面处于活动状态时测量获取请求所花费的时间
- flutter - 我不明白如何解决此错误:在构建 Wrapper(dirty) 时抛出了以下 ProviderNotFoundException
- pine-script - 如何绘制更高时间范围内的数据变化?
- python - 使用盈透证券 TWS API - Python 作为对象/变量返回价格
- reactjs - 如何在玩笑中模拟未定义的事件