0

We are using Independentsoft's dll wrapper for webDAV to send email. If I send 20 identical emails some where between 1 and 5 emails will get as far as the drafts folder but not actually send. It'll send if I open the draft email in Outlook and then click send. Every-time I test it at least one email fails to send, sometimes more. Within the 20 emails it can vary which ones fail, 1st 2nd, 15th etc.

Here's the code:

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();
    }
}

Once send from the drafts folder both successful and the unsuccessful email headers are almost identical.

Tod
  • 2,070
  • 21
  • 27
  • I decided to do some more investigating. What we were doing was running the email processing with a Quartz 1 minute job. I moved this code to a page with a button and it ran fine. So I've moved the code into a delegate and run it in a separate thread inside the Quartz task, this works fine. I don’t understand how a separate thread would allow the sending of some draft emails. Bizarre. – Tod Oct 11 '13 at 13:12
  • Correction this only fixed it for one test, subsequent tests have confirmed the same problem during the Quarts task. The code executed on page still has no issues. Threaded or otherwise. – Tod Oct 11 '13 at 13:55

0 Answers0