我正在使用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)
之后挂起而不会给出任何响应/错误。
相同的代码在我的本地计算机上运行正常,但是当我将其上传到服务器并运行它时,请不要回复。我还在服务器上尝试了以下步骤。
答案 0 :(得分:0)
服务器上安装了防火墙,它阻止了大小超过1 MB的附件的电子邮件。它也阻止了一些SMTP连接。