EmailJob.Execute gets stuck on invalid recipient email addresses

  • Last Post 02 January 2018
Bendico posted this 06 December 2017

Recently, our forum site stopped sending confirmation emails to users registering themselves. I discovered that it was due to some invalid email addresses that had been registered in the database. Apparently, email addresses with a dot just before the alpha were validated, f.ex. When the server tried to send an email to this address via smtp,an exception was thrown (we are using an office365 smtp server). This led to the EmailJob.Execute getting stuck on the exception, and never got on to send emails to valid addresses.

I had to modify the source code to find and correct the problem. In EmailService.cs I did the following:

// Loop through email email create a mailmessage and send 
foreach (var message in emails) {    try {       var msg = new MailMessage       {          IsBodyHtml = true,          Body = message.Body,          From = new MailAddress(fromEmail),          Subject = message.Subject       };       msg.To.Add(message.EmailTo);       mySmtpClient.Send(msg);    }    catch (Exception ex)    {       _loggingService.Error(string.Format("EXCEPTION sending mail to {0}: {1}", message.EmailTo, ex.Message));    }    emailsToDelete.Add(message); }

What I did was to put an exception handler around the smtp.send within the loop, and if the send fails with an exception, the mail is still deleted from the queue so that it won't try sending it again, and the loop continues with subsequent mails.

Any chance of getting this fix into the official source code?

YodasMyDad posted this 02 January 2018

Thanks I'll get that fixed