private static EmailQueueItem[] Load(int id, string to, int minTries, int maxTries, DateTime minNextTryDate) { List<EmailQueueItem> lEmailQueueItems = new List<EmailQueueItem>(); //using (var conn = Config.DB.Open()) { using (var reader = SqlHelper.GetDB().ExecuteReader("LoadEmailQueue", id, to, minTries, maxTries, minNextTryDate == DateTime.MinValue ? null : (object) minNextTryDate)) { while (reader.Read()) { EmailQueueItem eqItem = new EmailQueueItem(); eqItem.id = (int) reader["Id"]; eqItem.fromEmail = (string) reader["From"]; eqItem.toEmail = (string) reader["To"]; eqItem.fromName = reader["FromName"] != DBNull.Value ? (string) reader["FromName"] : null; eqItem.toName = reader["ToName"] != DBNull.Value ? (string) reader["ToName"] : null; if (reader["CC"] is string) { eqItem.cc = (string) reader["CC"]; } if (reader["BCC"] is string) { eqItem.bcc = (string) reader["BCC"]; } eqItem.subject = (string) reader["Subject"]; eqItem.body = (string) reader["Body"]; eqItem.tries = (int) reader["Tries"]; eqItem.lastTry = reader["LastTry"] is DateTime ? (DateTime) reader["LastTry"] : DateTime.MinValue; if (reader["LastError"] is string) { eqItem.lastError = (string) reader["LastError"]; } eqItem.nextTry = reader["NextTry"] is DateTime ? (DateTime) reader["NextTry"] : DateTime.MinValue; lEmailQueueItems.Add(eqItem); } reader.Close(); } } return lEmailQueueItems.ToArray(); }
public static void ProcessEmailQueueItem(EmailQueueItem eqItem, bool checkMailerLock) { if (checkMailerLock && mailerLock) { return; } if (eqItem.ToEmail.Contains("@ezFixUp.com")) return; try { MailMessage message = new MailMessage(); if (eqItem.FromName == null) message.From = new MailAddress(eqItem.FromEmail); else { message.From = new MailAddress(eqItem.FromEmail, eqItem.FromName, Encoding.UTF8); } if (eqItem.ToName == null) message.To.Add(new MailAddress(eqItem.ToEmail)); else { message.To.Add(new MailAddress(eqItem.ToEmail, eqItem.ToName, Encoding.UTF8)); } if (eqItem.Cc != null) { message.CC.Add(eqItem.Cc); } if (eqItem.Bcc != null) { message.Bcc.Add(eqItem.Bcc); } message.Subject = eqItem.Subject; #region Set Subject Encoding if (Properties.Settings.Default.EmailSubjectEncoding == "" || Properties.Settings.Default.EmailSubjectEncoding == null) { message.SubjectEncoding = Encoding.UTF8; } else { try { message.SubjectEncoding = Encoding.GetEncoding(Properties.Settings.Default.EmailSubjectEncoding); } catch (ArgumentException) { message.SubjectEncoding = Encoding.UTF8; } } #endregion message.IsBodyHtml = true; message.Body = eqItem.Body; #region Set Body Encoding if (Properties.Settings.Default.EmailBodyEncoding == "" || Properties.Settings.Default.EmailBodyEncoding == null) { message.BodyEncoding = Encoding.UTF8; } else { try { message.BodyEncoding = Encoding.GetEncoding(Properties.Settings.Default.EmailBodyEncoding); } catch (ArgumentException) { message.BodyEncoding = Encoding.UTF8; } } #endregion SmtpClient smtpClient = new SmtpClient(); //smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Send(message); EmailQueueItem.Delete(eqItem.Id); } catch (Exception err) { eqItem.LastError = err.Message; while (err.InnerException != null) { eqItem.LastError = eqItem.LastError + err.InnerException.ToString(); err = err.InnerException; } eqItem.LastTry = DateTime.Now; eqItem.NextTry = DateTime.Now.AddHours(Config.Mailing.RetryInterval); eqItem.Tries++; eqItem.Save(); } }
public static EmailQueueItem Create(string fromEmail, string fromName, string toEmail, string toName, string subject, string body) { if (toEmail.ToLower().Contains("@ezFixUp.com")) return null; EmailQueueItem qItem = new EmailQueueItem(); qItem.id = -1; qItem.fromEmail = fromEmail; qItem.toEmail = toEmail; qItem.fromName = fromName; qItem.toName = toName; qItem.subject = subject; qItem.body = body; qItem.tries = 0; qItem.lastTry = DateTime.MinValue; qItem.nextTry = DateTime.MinValue; return qItem; }