/// <summary> /// Sends the emails to all subscribers /// </summary> private void StartSend() { bool lastItem = false; int numberOfMailServerErrors = 0; SmtpClient smtp = new SmtpClient(); if (!String.IsNullOrEmpty(EmailServer)) { smtp.Host = EmailServer; } else if (String.IsNullOrEmpty(smtp.Host)) { smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; } try { // send the emails for (; ;) { // make local copies of the message body so we don't get a race condition on the instance variables string htmlBodyCopy = HtmlBody; string plainTextBodyCopy = PlainTextBody; Lock.AcquireWriterLock(Timeout.Infinite); if (Subscribers.Count == 0 || MailServerDown) { Lock.ReleaseWriterLock(); break; } SubscriberInfo subscriber = Subscribers.Dequeue(); if (Subscribers.Count == 0) { lastItem = true; } Lock.ReleaseWriterLock(); MailMessage mail = new MailMessage(); mail.From = new MailAddress(FromEmail); mail.To.Add(subscriber.Email); mail.Subject = Subject; if (Attachments != null) { foreach (Attachment att in Attachments) { mail.Attachments.Add(att); } } if (subscriber.Replacements != null && subscriber.Replacements.Count > 0) { foreach (string key in subscriber.Replacements.Keys) { if (!string.IsNullOrEmpty(key) && subscriber.Replacements[key] != null) { htmlBodyCopy = htmlBodyCopy.Replace(key, subscriber.Replacements[key].ToString()); plainTextBodyCopy = plainTextBodyCopy.Replace(key, subscriber.Replacements[key].ToString()); } } } switch (subscriber.SubscriptionType) { case SubscriptionType.Html: mail.IsBodyHtml = true; mail.Body = htmlBodyCopy; break; case SubscriptionType.PlainText: mail.Body = plainTextBodyCopy; break; case SubscriptionType.MultiPart: AlternateView textView = AlternateView.CreateAlternateViewFromString(plainTextBodyCopy, null, "text/plain"); textView.TransferEncoding = TransferEncoding.SevenBit; AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBodyCopy, null, "text/html"); htmlView.TransferEncoding = TransferEncoding.SevenBit; mail.AlternateViews.Add(textView); mail.AlternateViews.Add(htmlView); break; default: mail.Body = htmlBodyCopy; break; } try { smtp.Send(mail); Lock.AcquireWriterLock(Timeout.Infinite); DataRow dr = newsletterActions.NewRow(); dr["MailoutID"] = MailoutID; dr["Timestamp"] = DateTime.UtcNow; dr["IPAddress"] = IPAddress; dr["Email"] = subscriber.Email; dr["SubscriberID"] = subscriber.SubscriberID; dr["NewsletterActionTypeID"] = newsletterSendTypeID; newsletterActions.Rows.Add(dr); Lock.ReleaseWriterLock(); if (lastItem) { allThreadsFinished = true; } } catch (SmtpException e) { mailServerError = true; numberOfMailServerErrors++; if (numberOfMailServerErrors > 2) //If the thread fails at the mail server 3 times in a row (thats 2 minutes of failure) assume the server is down and stop execution { MailServerDown = true; } Lock.AcquireWriterLock(Timeout.Infinite); NotSentMails++; Lock.ReleaseWriterLock(); Helpers.LogException(e); if (lastItem) { allThreadsFinished = true; } Thread.Sleep(30000); } } } catch (Exception e) { Helpers.LogException(e); } }