我们正在使用Independentsoft的dll包装器为webDAV发送电子邮件。 如果我发送20封相同的电子邮件,其中1到5封电子邮件将会到达草稿文件夹但实际上并未发送。如果我在Outlook中打开草稿电子邮件然后单击“发送”,它将发送。每次我测试它至少有一封电子邮件无法发送,有时更多。在20封电子邮件中,它可以改变哪些失败,第1,第2,第15等。
以下是代码:
foreach (var email in emailsToSend)
{
try
{
var service = new Service("https://Ourserver/ews/Exchange.asmx", credentials);
ItemId messageId = null;
ItemInfoResponse sendResponse = null;
if (!string.IsNullOrWhiteSpace(email.messageID))
{
try
{
messageId = new ItemId(email.messageID);
sendResponse = service.Send(messageId);
if (sendResponse.ResponseClass == ResponseClass.Success)
{
email.Success = true;
email.SentTimeStamp = DateTime.Now;
email.messageID = messageId.Id;
return;
}
else
{
email.LastError = sendResponse.Message;
email.messageID = null;
}
}
catch(Exception ex)
{
email.LastError = ex.Message;
email.messageID = null;
email.ErrorCount++;
}
ctx.SubmitChanges();
}
var mm = new Message();
foreach (var to in email.Tos)
{
switch (to.Type)
{
case 0: mm.ToRecipients.Add(new Mailbox(to.Address, to.Name));
break;
case 1: mm.CcRecipients.Add(new Mailbox(to.Address, to.Name));
break;
case 2: mm.BccRecipients.Add(new Mailbox(to.Address, to.Name));
break;
}
}
var attachmentCollection = new List<Independentsoft.Exchange.Attachment>();
foreach (var att in email.Attachments)
{
var attachment = new FileAttachment(att.FileData.ToArray(), att.FileName);
attachmentCollection.Add(attachment);
}
var body = new Body(email.Body);
body.Type = email.IsHTML ? BodyType.Html : BodyType.Best;
mm.Body = body;
mm.Subject = email.Subject;
try
{
//check for use of secondary emailaddress
var response = service.ResolveNames(string.Format("smtp:{0}", email.FromAddress));
if (response.Resolutions.Count == 1 && response.Resolutions.Cast<Resolution>().First().Mailbox != null)
{
Mailbox userMailbox = response.Resolutions.Cast<Resolution>().First().Mailbox;
StandardFolderId userInboxFolder = new StandardFolderId(StandardFolder.Inbox, userMailbox);
Folder inbox = service.GetFolder(userInboxFolder);
if (inbox != null)
{
service.ExchangeImpersonation = new Identity(userMailbox.EmailAddress);
}
else
{
mm.ReplyTo.Add(new Mailbox(email.FromAddress, email.FromName));
}
}
else
{
mm.ReplyTo.Add(new Mailbox(email.FromAddress, email.FromName));
}
}
catch
{
mm.ReplyTo.Add(new Mailbox(email.FromAddress, email.FromName));
}
messageId = service.CreateItem(mm);
if (attachmentCollection.Count > 0)
{
IList<CreateAttachmentResponse> responses = service.CreateAttachment(attachmentCollection, messageId);
messageId.ChangeKey = responses.Last().Attachment.AttachmentId.RootItemChangeKey;
}
sendResponse = service.Send(messageId);
if (sendResponse.ResponseClass == ResponseClass.Success)
{
email.Success = true;
email.SentTimeStamp = DateTime.Now;
email.messageID = messageId.Id;
}
else
{
email.LastError = sendResponse.Message;
email.messageID = messageId.Id;
email.ErrorCount++;
}
ctx.SubmitChanges();
}
catch (Exception ex)
{
email.LastError = ex.Message;
email.ErrorCount++;
ctx.SubmitChanges();
}
}
从草稿文件夹发送后,成功和不成功的电子邮件标题几乎完全相同。