在gmail服务器上使用sp_send_dbmail时SQL Server邮件发送错误



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

配置文件设置如下:

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设置看起来是正确的。带有"尝试不同的端口"的警告(也在上面原始问题的评论中提到(

发件人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服务器机器上安装这个小实用程序,它会有很大的帮助。

如果你在linux上运行sql服务器,那就另当别论了。。。您可以"apt-installcurl"(或类似的(并通过这种方式ping端口。

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

如果上面的portqueryui工具报告"已阻止"或"已筛选"。。。您可能需要在计算机上打开一个端口。

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

您的路由器/网络也可能阻塞端口。同样,portqueryui工具是一种快速测试从你试图访问的机器到互联网的路径的方法。

相关内容

最新更新