public static void SendMail(string toEmailAddress, string emailTitle, string emailMsgBody, CustomLogger logger) { MailAddress fromAddress = new MailAddress(Constants.FromEmailAddress); MailAddress toAddress = new MailAddress(toEmailAddress); MailMessage email = new MailMessage(fromAddress, toAddress); email.Subject = emailTitle; email.Body = emailMsgBody; email.IsBodyHtml = true; SmtpClient smtp = new SmtpClient("smtp.mail.ru", 587); smtp.Credentials = new NetworkCredential(Constants.FromEmailAddress, Constants.FromEmailPassword); smtp.EnableSsl = true; try { smtp.Send(email); logger.UseLogger("INFO", "Email successfully delivered.", Thread.GetDomainID().ToString(), "SendMail"); } catch (SmtpFailedRecipientsException ex) { for (int i = 0; i < ex.InnerExceptions.Length; i++) { SmtpStatusCode status = ex.InnerExceptions[i].StatusCode; if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable) { logger.UseLogger("ERROR", "Delivery failed - retrying in 5 seconds.", Thread.GetDomainID().ToString(), "SendMail"); System.Threading.Thread.Sleep(5000); smtp.Send(email); } else { logger.UseLogger("ERROR", "Failed to deliver message to " + ex.InnerExceptions[i].FailedRecipient, Thread.GetDomainID().ToString(), "SendMail"); } } } catch (Exception ex) { logger.UseLogger("ERROR", "Exception caught in RetryIfBusy(): " + ex.ToString(), Thread.GetDomainID().ToString(), "SendMail"); } }
public void BtSendMsg_Click(object sender, EventArgs e) { if (fldMsgBox.Text.Length > 0 || fldMsgBox.Text != String.Empty) { fldDialogBox.SelectionFont = new Font(fldDialogBox.Font.FontFamily, this.Font.Size, FontStyle.Italic); fldDialogBox.AppendText("\n\n" + fldMsgBox.Text); fldDialogBox.SelectionFont = new Font(fldDialogBox.Font.FontFamily, this.Font.Size, FontStyle.Regular); switch (DialogStatus) { case Constants.DialogStatus01: SendSystemMsg(Constants.DialogMsg02); ShowMenu(); name = fldMsgBox.Text; if (name == Constants.AdminName) { DialogStatus = Constants.DialogStatusAdmin01; SendSystemMsg(Constants.DialogMsg04); logger.UseLogger("INFO", "Admin start administrate...", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } else { DialogStatus = Constants.DialogStatus02; SendSystemMsg(Constants.DialogMsg13); logger.UseLogger("INFO", "Be our guest! Put our service to the test!", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } break; case Constants.DialogStatusAdmin01: int variant; if (!int.TryParse(fldMsgBox.Text, out variant)) { SendSystemMsg(Constants.DialogMsg25); break; } if (Convert.ToInt32(fldMsgBox.Text) > 0 && Convert.ToInt32(fldMsgBox.Text) <= Constants.AmountAdminVariants) { switch (fldMsgBox.Text) { case "1": DialogStatus = Constants.DialogStatusAdmin02; SendSystemMsg(Constants.DialogMsg07); logger.UseLogger("INFO", "Start " + Constants.DialogStatusAdmin02, Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); break; case "2": DialogStatus = Constants.DialogStatusAdmin03; SendSystemMsg(Constants.DialogMsg11); logger.UseLogger("INFO", "Start " + Constants.DialogStatusAdmin03, Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); break; case "3": DialogStatus = Constants.DialogStatusAdmin04; SendSystemMsg(Constants.DialogMsg09); logger.UseLogger("INFO", "Start " + Constants.DialogStatusAdmin04, Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); break; case "4": ShowMenu(); SendSystemMsg(Constants.DialogMsg04); logger.UseLogger("INFO", "Update menu list.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); break; } } else { SendSystemMsg(Constants.DialogMsg05); SendSystemMsg(Constants.DialogMsg04); logger.UseLogger("DEBUG", Constants.DialogMsg06, Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } break; case Constants.DialogStatusAdmin02: if (InputCheckAdmin(fldMsgBox.Text) == true) { ClassLibrary.Menu.CreatePizza(MenuPizza, fldMsgBox.Text); SendSystemMsg(Constants.DialogMsg08); SendSystemMsg(Constants.DialogMsg04); DialogStatus = Constants.DialogStatusAdmin01; logger.UseLogger("INFO", Constants.DialogStatusAdmin02 + " ended.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } else { SendSystemMsg(Constants.DialogMsg25); } break; case Constants.DialogStatusAdmin03: if (InputCheckAdmin(fldMsgBox.Text) == true) { ClassLibrary.Menu.UpdateMenu(MenuPizza, fldMsgBox.Text); SendSystemMsg(Constants.DialogMsg12); SendSystemMsg(Constants.DialogMsg04); DialogStatus = Constants.DialogStatusAdmin01; logger.UseLogger("INFO", Constants.DialogStatusAdmin03 + " ended.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } else { SendSystemMsg(Constants.DialogMsg25); } break; case Constants.DialogStatusAdmin04: if (InputCheckAdmin(fldMsgBox.Text) == true) { ClassLibrary.Menu.DeletePizza(MenuPizza, Convert.ToByte(fldMsgBox.Text)); SendSystemMsg(Constants.DialogMsg10); SendSystemMsg(Constants.DialogMsg04); DialogStatus = Constants.DialogStatusAdmin01; logger.UseLogger("INFO", Constants.DialogStatusAdmin04 + " ended.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } else { SendSystemMsg(Constants.DialogMsg25); logger.UseLogger("WARN", "The answer was entered incorrectly.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } break; case Constants.DialogStatus02: if (InputCheck(fldMsgBox.Text) == true) { ClassLibrary.Bill.AddPizza(Order, MenuPizza, fldMsgBox.Text); SendSystemMsg(Constants.DialogMsg14); DialogStatus = Constants.DialogStatus03; logger.UseLogger("INFO", "Menu list updated.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } else { SendSystemMsg(Constants.DialogMsg25); logger.UseLogger("WARN", "The answer was entered incorrectly.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } break; case Constants.DialogStatus03: if (fldMsgBox.Text.ToLower() != "нет" || fldMsgBox.Text.ToLower() != "да") { if (fldMsgBox.Text.ToLower().Contains("нет")) { logger.UseLogger("INFO", "Stop ordering.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); id = Person.SearchPerson(People, name); if (id == 0) { DialogStatus = Constants.DialogStatus04; logger.UseLogger("INFO", Constants.DialogStatus04, Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); SendSystemMsg(Constants.DialogMsg15); //string name, string email, bool mailing } else { DialogStatus = Constants.DialogStatus06; fldMsgBox.Text = String.Empty; btSendMsg.Enabled = false; fldMsgBox.Enabled = false; SendSystemMsg(Constants.DialogMsg19); ShowOrder(); email = Person.SearchPersonEmail(People, id); logger.UseLogger("INFO", "Search email in the database.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); SendMailing(email); } } else { DialogStatus = Constants.DialogStatus02; SendSystemMsg(Constants.DialogMsg13); logger.UseLogger("INFO", "Start ordering.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } } else { SendSystemMsg(Constants.DialogMsg25); logger.UseLogger("WARN", "The answer was entered incorrectly.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } break; case Constants.DialogStatus04: bool correctEmail = IsValidEmail(fldMsgBox.Text); if (correctEmail == false) { SendSystemMsg(Constants.DialogMsg16); logger.UseLogger("WARN", "E-mail was entered incorrectly.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } else { email = fldMsgBox.Text; DialogStatus = Constants.DialogStatus05; SendSystemMsg(Constants.DialogMsg17); } break; case Constants.DialogStatus05: if (fldMsgBox.Text.ToLower() != "да" || fldMsgBox.Text.ToLower() != "нет") { switch (fldMsgBox.Text.ToLower()) { case "нет": mailing = false; logger.UseLogger("INFO", $"The user with the email {email} unsubscribed from the newsletter.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); break; case "да": mailing = true; logger.UseLogger("INFO", $"The user with the email {email} subscribed to the newsletter.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); break; default: break; } fldMsgBox.Text = String.Empty; fldMsgBox.Enabled = false; btSendMsg.Enabled = false; Person.CreateNewPerson(People, name, email, mailing); SendSystemMsg(Constants.DialogMsg18); logger.UseLogger("INFO", "User record created", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); SendSystemMsg(Constants.DialogMsg19); DialogStatus = Constants.DialogStatus06; ShowOrder(); SendMailing(email); } else { SendSystemMsg(Constants.DialogMsg25); logger.UseLogger("WARN", "E-mail was entered incorrectly.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } break; case Constants.DialogStatus06: SendSystemMsg(Constants.DialogMsg26); logger.UseLogger("INFO", "Order completed.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); break; default: break; } } else { SendSystemMsg(Constants.DialogMsg25); logger.UseLogger("WARN", "User entered an empty string.", Thread.GetDomainID().ToString(), GetCurrentMethod().ToString()); } fldMsgBox.Text = String.Empty; }