无法从 Azure 云服务连接到 smtp

Not able to connect to smtp from Azure Cloud Service

我们在 Azure 上托管了 2 个云服务。

这两种服务都依赖于我们的 smtp 服务器来发送邮件。

问题是 azure 云服务无法连接到我们的 smtp 服务器。

我们可以在内部机器上使用相同的代码,没有任何问题。我们还检查了 25 端口是否打开并且 IP 地址也不在黑名单中。

以下是从云服务连接时的错误:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 111.93.111.42:25

邮件发送逻辑

MailMessage message = new MailMessage(senderID, reminder.UserName, template.Subject, body);
message.From = new MailAddress(data.SenderEmail, data.SenderName);
message.IsBodyHtml = true;

            try
            {
                SmtpClient smtp = new SmtpClient
                {
                    Host = data.SMTPServer, // smtp server address here...                    
                    Port = data.PortNo,
                    EnableSsl = data.SSL,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
                    Timeout = 30000,
                };
                smtp.Send(message);
                //Thread th = new Thread(() => { smtp.Send(message); });
                //th.Start();

            }
            catch (Exception ex)
            {
                ErrorLogging.ErrorLog(ex, "Error Reminders send Mail - Employee Reminders Mail Error Message : " + ex.Message, "Employee Reminders Mail", "0", "EmployeeRemindersMail", schemaName, companyId);
            }

Microsoft 建议 Azure 客户使用经过身份验证的 SMTP 中继服务(通常通过 TCP 端口 587 或 443 连接,但通常也支持其他端口)从 Azure VM 或 Azure 应用服务发送电子邮件。这些服务专注于发件人信誉,以最大限度地减少第 3 方电子邮件提供商拒绝邮件的可能性。此类 SMTP 中继服务包括但不限于SendGrid。您也可以在本地使用安全的 SMTP 中继服务 运行。 无论订阅类型如何,这些电子邮件传递服务的使用在 Azure 中都不受限制。

参考:https://blogs.msdn.microsoft.com/mast/2017/11/15/enhanced-azure-security-for-sending-emails-november-2017-update/

您可能还想参考解决类似问题的 this 讨论帖,看看是否有帮助。

请注意 - 从上面链接的文章 - Enterprise Azure 客户端可以直接从 Azure 发送 SMTP 消息:

“对于企业协议 Azure 用户,在不使用经过身份验证的中继的情况下发送电子邮件的技术能力没有变化。新的和现有的企业协议用户都可以尝试从 Azure VM 直接向外部电子邮件提供商发送出站电子邮件,而无需任何来自 Azure 平台的限制。虽然不能保证电子邮件提供商会接受来自任何给定用户的传入电子邮件,但 Azure 平台不会阻止企业协议订阅中的 VM 发送尝试。您必须直接与电子邮件提供商合作修复涉及特定提供商的任何消息传递或垃圾邮件过滤问题。"