SMTP sendmail挂起没有任何错误,我们的响应
本文关键字:我们 响应 错误 任何 sendmail 挂起 SMTP | 更新日期: 2023-09-27 18:17:06
我使用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)
之后挂起,没有给出任何响应/错误。
相同的代码在我的本地机器上运行良好,但当我将其上传到服务器并运行时,它不响应。我也在服务器上尝试了以下步骤。
- 在服务器上打开587端口。
- 给附件文件夹所有权限。
服务器上安装了防火墙,它阻止了附件大小超过1mb的电子邮件,也阻止了一些SMTP连接。