public override int SaveChanges() { try { return(base.SaveChanges()); } catch (DbEntityValidationException ex) { var config = new DotNetConfigurationManager(); if (!config.EmergencyMailAddresses.Any()) { throw; } var mailSender = new SmtpMailSender(config, new ElmahExceptionLogger( config.DefaultMailFromAddress, config.EmergencyMailAddresses)); var message = new MailMessage( config.DefaultMailFromAddress, config.EmergencyMailAddresses.First()) { Subject = "UCosmic caught DbEntityValidationException during SaveChanges.", }; if (config.EmergencyMailAddresses.Count() > 1) { foreach (var emergencyAddress in config.EmergencyMailAddresses.Skip(1)) { message.CC.Add(emergencyAddress); } } var body = new StringBuilder(); foreach (var entry in ex.EntityValidationErrors) { var entityType = entry.Entry.Entity.GetType(); foreach (var error in entry.ValidationErrors) { body.Append(entityType.Name); body.Append('.'); body.Append(error.PropertyName); body.Append(": \r\n"); body.Append(error.ErrorMessage); body.Append("\r\n\r\n"); } } message.Body = body.ToString(); mailSender.Send(message); Debug.WriteLine(message.Body); throw; } }
public void SmtpMailSenderDoesntThrowWException() { IEmailSender sender = new SmtpMailSender(new EmailOptions { FromEmail = "*****@*****.**", FromName = "Luka Lukic", FromPassword = "******", Host = "smtp.gmail.com", Port = 587 }) { Body = "test 123", Subject = "Test case", ToEmail = "*****@*****.**" }; Action action = () => sender.Send(); action.Should().NotThrow(); }
public override int SaveChanges() { try { return(base.SaveChanges()); } catch (DbEntityValidationException ex) { var config = new DotNetConfigurationManager(); if (!config.IsDeployedToCloud) { throw; } var mailSender = new SmtpMailSender(config, new ElmahExceptionLogger(config)); var message = new MailMessage(config.EmailDefaultFromAddress, config.EmailEmergencyAddresses) { Subject = "UCosmic caught DbEntityValidationException during SaveChanges.", }; var body = new StringBuilder(); foreach (var entry in ex.EntityValidationErrors) { var entityType = entry.Entry.Entity.GetType(); foreach (var error in entry.ValidationErrors) { body.Append(entityType.Name); body.Append('.'); body.Append(error.PropertyName); body.Append(": \r\n"); body.Append(error.ErrorMessage); body.Append("\r\n\r\n"); } } message.Body = body.ToString(); mailSender.Send(message); throw; } }