SMTP SendEmail 挂起,我们的回复没有任何错误

SMTP SendEmail hung without any error our response

我正在使用 SmtpClient 发送带附件的电子邮件。发送邮件方法如下

  public static void SendEmail(string subject, string messageBody, string toEmailId, List<string> attachments=null, List<string> cc = null, bool IsBodyHtml = false)
    {
        try
        {

            var fromAddress = new MailAddress("email@email.com");
            var toAddress = new MailAddress(toEmailId);
            const string fromPassword = "password";


            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
                Timeout= 1000 * 60
            };


            using (var message = new MailMessage(fromAddress, toAddress)
            {

                Subject = subject,
                Body = messageBody,
                IsBodyHtml = IsBodyHtml


            })
            {
                if (cc != null)
                {
                    foreach (string s in cc)
                        message.CC.Add(s);
                }

                if (attachments != null)
                {
                    foreach (string attachment in attachments)
                    {
                        message.Attachments.Add(new Attachment(attachment));

                    }
                }



                Console.WriteLine("email sending");
                smtp.Send(message);

                //Clean up attachments
                foreach (Attachment attachment in message.Attachments)
                {
                    attachment.Dispose();
                }

            }

        }

        catch (Exception ex)
        {
            throw ex;
        }

    }

我可以使用上述 SMTP 配置发送不带附件的电子邮件。但是当我使用附件时,程序在 smtp.Send(message) 之后挂起而没有给出任何 response/error.

相同的代码 运行 在我的本地机器上没问题,但是当我将它上传到服务器时 运行 它没有响应。我还在服务器上尝试了以下步骤。

  1. 已在服务器上打开端口 587。
  2. 授予对附件文件夹的所有权限。

服务器上安装了防火墙,它阻止了附件大小超过 1 MB 的电子邮件。它还阻止了一些 SMTP 连接。