问题描述
我们有一个 azure 应用服务,突然间我们看到了这个错误:
System.Net.Mail.SmtpException: Insufficient system storage. The server response was: 4.3.1 Out of memory at System.Net.Mail.SendMailAsyncResult.End(IAsyncResult result) at System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result) --- End of stack trace from prevIoUs location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at
我们已经向发送电子邮件的提供商发出了一张票,但他们说他们没有发现问题。现在我们认为我们在上一个版本中推送了一些可能导致问题的内容。当然,我们目前还没有发现任何东西。
只是好奇其他人是否有这些问题以及最终结果是什么?
我们也在 Azure 中的其他应用服务上使用了此代码,但这些服务由不同的客户(不同的实例)控制。现在有问题的是一个新客户,我们在一周前的星期一推送了我们的代码。不过上周似乎一切都很好。
这是我们的 SMTP 代码:
/// <summary>
/// Send an email via code. All email's with attachments are sent through code.
/// </summary>
/// <param name="email">Email Data</param>
/// <param name="smtpServer">The SMTP server.</param>
/// <returns>
/// Task
/// </returns>
private async Task SendSMTPEmail(EmailDTO email,SMTPServerDTO smtpServer)
{
SmtpClient smtp = null;
if (email == null)
{
throw new ArgumentNullException(nameof(email));
}
if (smtpServer == null)
{
throw new ArgumentNullException(nameof(smtpServer));
}
if (!smtpServer.Port.HasValue)
{
smtp = new SmtpClient(smtpServer.HostName);
}
else
{
smtp = new SmtpClient(smtpServer.HostName,smtpServer.Port.Value);
}
// Sets the NetworkCredential if a user name and password is required
if (!string.IsNullOrWhiteSpace(smtpServer.Username) && !string.IsNullOrWhiteSpace(smtpServer.Password))
{
smtp.Credentials = new System.Net.NetworkCredential(smtpServer.Username,smtpServer.Password);
}
MailMessage mailMessage = new MailMessage
{
From = new MailAddress(email.FromAddress)
};
foreach (string toAddress in email.ToAddresses)
{
mailMessage.To.Add(new MailAddress(toAddress));
}
foreach (string ccAddress in email.CCAddresses)
{
mailMessage.CC.Add(ccAddress);
}
foreach (string bccAddress in email.BCCAddresses)
{
mailMessage.Bcc.Add(bccAddress);
}
if (!string.IsNullOrWhiteSpace(email.ReplyToAddress))
{
mailMessage.ReplyToList.Add(email.ReplyToAddress);
}
mailMessage.Subject = email.Subject;
mailMessage.Body = email.Body;
if (!string.IsNullOrWhiteSpace(email.Importance))
{
email.Importance = email.Importance.ToLower();
}
switch (email.Importance)
{
case "low":
mailMessage.Priority = MailPriority.Low;
break;
case "normal":
mailMessage.Priority = MailPriority.normal;
break;
case "high":
mailMessage.Priority = MailPriority.High;
break;
default:
mailMessage.Priority = MailPriority.normal;
break;
}
if (!string.IsNullOrWhiteSpace(email.Sensitivity))
{
mailMessage.Headers.Add("Sensitivity",email.Sensitivity);
}
else
{
mailMessage.Headers.Add("Sensitivity",EmailSensitivityConstants.CompanyConfidential);
}
foreach (EmailAttachmentDTO emailAttachment in email.Attachments)
{
byte[] attachmentData = Convert.FromBase64String(emailAttachment.FileData);
MemoryStream memoryStream = new MemoryStream(attachmentData);
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(memoryStream,emailAttachment.FileName);
mailMessage.Attachments.Add(attachment);
}
await smtp.SendMailAsync(mailMessage).ConfigureAwait(false);
}
解决方法
问题出在我们 Azure 实例中电子邮件服务的第 3 方提供商。这导致我们的应用服务等出现内存不足问题。
从未听到他们为解决问题所做的工作。
电子邮件现在可以正常发送。