【发布时间】:2016-02-16 23:42:31
【问题描述】:
我们有一个在 Windows Server 2008 机器上运行的应用程序。它使用 office365 smtp 中继帐户发送电子邮件。但是,并非所有电子邮件都发送成功。我们在 smtp.Send 调用发送的电子邮件中随机得到这两个异常:
- System.Net.Mail.SmtpFailedRecipientsException:无法发送给所有收件人。 ---> System.Net.Mail.SmtpFailedRecipientException:邮箱不可用。服务器响应为:5.7.64 TenantAttribution;中继访问被拒绝
- System.Net.Mail.SmtpFailedRecipientException:系统存储空间不足。服务器响应是:4.5.3 收件人太多
到目前为止,我们还无法弄清楚为什么会发生这种情况。任何想法都表示赞赏。
电子邮件代码使用 System.Net.Mail 命名空间 - .Net framework 4.0。 我们传入 NetworkCredential 的用户名和密码。
public void Send(string from, string[] to, string[] cc, string[] bcc, string subject, string body, string[] attachmentArr, Boolean isBodyHtml, string smtpServerName, int port = 25, bool enableSsl = true, string userName = null, string password = null, string domain = null, int timeoutMilliSec = 100000)
{
MailMessage objEmail = new MailMessage();
try
{
foreach (string toItem in to)
{
objEmail.To.Add(toItem);
}
if (cc != null)
{
foreach (string toItem in cc)
{
objEmail.CC.Add(toItem);
}
}
if (bcc != null)
{
foreach (string toItem in bcc)
{
objEmail.Bcc.Add(toItem);
}
}
objEmail.From = new MailAddress(from);
objEmail.Subject = subject;
objEmail.Body = body;
objEmail.IsBodyHtml = isBodyHtml;
objEmail.Priority = MailPriority.High;
if (attachmentArr != null)
{
foreach (String s1 in attachmentArr)
{
objEmail.Attachments.Add(new Attachment(s1));
}
}
using (SmtpClient smtp = new SmtpClient(smtpServerName))
{
if (string.IsNullOrEmpty(userName) == false && string.IsNullOrEmpty(password) == false)
{
NetworkCredential credential = (string.IsNullOrEmpty(domain)) ? new NetworkCredential(userName, password) : new NetworkCredential(userName, password, domain);
smtp.Credentials = credential;
}
smtp.Timeout = timeoutMilliSec;
smtp.Port = port;
smtp.EnableSsl = enableSsl;
smtp.Send(objEmail);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (attachmentArr != null && objEmail.Attachments != null)
{
foreach (Attachment a1 in objEmail.Attachments)
{
a1.Dispose();
}
}
}
}
【问题讨论】:
标签: smtp office365 system.net.mail