private async Task <SendEmailResult> TrySendSmtpEmail <TModel>(string emailAddress, string templateId, TModel model, bool test) { try { var result = await SmtpEmailProvider.SendEmailAsync(emailAddress, templateId, model, test); await EmailSendLog.WriteAsync( new EmailSendLogModel { Message = "Email successfully sent via SMTP", Subject = result.EmailSubject, Recipients = result.EmailAddress, Server = result.Server }); return(result); } catch (Exception ex) { Logger.LogError( ex, "{FuncName}: Cannot send email to {Email} using SMTP. TemplateId: {TemplateId}", nameof(TrySendSmtpEmail), emailAddress, templateId); } return(null); }
public virtual async Task <List <SendEmailResult> > SendDistributionEmailAsync <TModel>(string emailAddresses, string templateId, TModel model, bool test) { var emailList = emailAddresses.SplitI(";").ToList(); emailList = emailList.RemoveI("sender", "recipient"); if (emailList.Count == 0) { throw new ArgumentNullException(nameof(emailList)); } if (emailList.ContainsAllEmails() == false) { throw new ArgumentException($"{emailList} contains an invalid email address", nameof(emailList)); } var successCount = 0; var results = new List <SendEmailResult>(); foreach (var emailAddress in emailList) { try { var result = await SendEmailAsync(emailAddress, templateId, model, test); await EmailSendLog.WriteAsync( new EmailSendLogModel { Message = "Email successfully sent via SMTP", Subject = result.EmailSubject, Recipients = result.EmailAddress, Server = result.Server, Username = result.ServerUsername, Details = result.EmailMessagePlainText }); successCount++; results.Add(result); } catch (Exception ex) { Logger.LogError( ex, "{FuncName}: Could not send email directly to {Email}. TemplateId: '{Template}'", nameof(SendDistributionEmailAsync), emailAddress, templateId); } } return(results); }
private async Task <SendEmailResult> TrySendGovNotifyEmailAsync <TModel>(string emailAddress, string templateId, TModel model, bool test) { try { var result = await GovNotifyEmailProvider.SendEmailAsync(emailAddress, templateId, model, test); if (result.Status.EqualsI("created", "sending", "delivered") == false) { throw new Exception($"Unexpected status '{result.Status}' returned"); } await EmailSendLog.WriteAsync( new EmailSendLogModel { Message = "Email successfully sent via GovNotify", Subject = result.EmailSubject, Recipients = result.EmailAddress, Server = result.Server }); return(result); } catch (Exception ex) { Logger.LogError( ex, "{FuncName}: Could not send email to Gov Notify using the email address: {Email}:", nameof(TrySendGovNotifyEmailAsync), emailAddress); // send failure email to GEO using smtp email provider await SmtpEmailProvider.SendEmailTemplateAsync( new SendEmailTemplate { RecipientEmailAddress = EmailOptions.GEODistributionList, Subject = "GPG - GOV NOTIFY ERROR", MessageBody = $"Could not send email to Gov Notify using {emailAddress} due to following error:\n\n{ex.GetDetailsText()}.\n\nWill attempting to resend email using SMTP." }); } return(null); }