public static void SaveMailSendLog(EmailSendLog emailSendLog) { Repository <EmailSendLog> emailSendLogRepository = new Repository <EmailSendLog>(); emailSendLogRepository.Add(emailSendLog); emailSendLogRepository.Save(); }
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); }
/// <summary> /// Parameterless Constructor /// </summary> public EmailHelper() { EmailMessage = new MailMessage(); errorLog = new EmailErrorLog(); sendLog = new EmailSendLog(); email = new Email(); }
public EmailHelper(EmailConfig _emailConfig) { EmailMessage = new MailMessage(); errorLog = new EmailErrorLog(); sendLog = new EmailSendLog(); email = new Email(); emailConfig = _emailConfig; }
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 void AddLog(bool flag) { EmailSendLog model = new EmailSendLog(); model.CreateDate = DateTime.Now.ToString(); model.EmailFromAddress = txtFromUser.Text.Trim(); model.EmailSubject = txtSubject.Text.Trim(); model.EmailToAddress = lstToMail[0]; model.IsSuccess = flag == true ? "发送成功" : "发送失败"; dalLog.Add(model); }
// Sending Mail to User with pdf Attachment public static bool Sendmail(int orderid) { User user = UserHelper.GetUserByOrderId(orderid); Candidate candidate = UserHelper.GetCandidateByOrderId(orderid); string toAddress = user.Email; bool isSent = false; try { StringBuilder emailBody = new StringBuilder(ConstructMail.GetMailBody(Constant.STATUS_COMPLETE)); emailBody = emailBody.Replace(Constant.CONST_FIRSTNAME, user.FirstName); emailBody = emailBody.Replace(Constant.CONST_LASTNAME, user.LastName); string subject = Constant.CONST_ORDERCOMPLETE_SUBJECT; subject = subject.Replace(Constant.CONST_ORDERID, orderid.ToString()); MailMessage mail = new MailMessage(ConfigurationManager.AppSettings["Email"].ToString(), toAddress, subject, emailBody.ToString()); mail.IsBodyHtml = true; string PDF_File_Path = GetFilePath() + "\\" + orderid.ToString() + ".pdf"; FileStream stream = File.OpenRead(PDF_File_Path); int length = Convert.ToInt32(stream.Length); byte[] data = new byte[length]; stream.Read(data, 0, length); stream.Close(); using (MemoryStream input = new MemoryStream(data)) { using (MemoryStream output = new MemoryStream()) { string password = candidate.Email; PdfReader reader = new PdfReader(input); PdfEncryptor.Encrypt(reader, output, true, password, password, PdfWriter.ALLOW_SCREENREADERS); data = output.ToArray(); } } mail.Attachments.Add(new Attachment(new MemoryStream(data), orderid + ".pdf")); SmtpClient smtp = new SmtpClient(); smtp.Send(mail); EmailSendLog emailSendLog = new EmailSendLog(); emailSendLog.OrderId = orderid; emailSendLog.UserMailId = toAddress; emailSendLog.InsertTime = DateTime.Now; OrderHelper.SaveMailSendLog(emailSendLog); isSent = true; } catch (Exception ex) { } return(isSent); }
public override void DeleteSiteContent(int siteId) { SiteSettings siteSettings = new SiteSettings(siteId); CommerceReport.DeleteBySite(siteSettings.SiteGuid); FileAttachment.DeleteBySite(siteSettings.SiteGuid); EmailSendLog.DeleteBySite(siteSettings.SiteGuid); EmailTemplate.DeleteBySite(siteSettings.SiteGuid); ContentHistory.DeleteBySite(siteSettings.SiteGuid); ContentWorkflow.DeleteBySite(siteSettings.SiteGuid); ContentMetaRespository metaRepository = new ContentMetaRespository(); metaRepository.DeleteBySite(siteSettings.SiteGuid); }
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); }
public static bool Sendmail(int orderid, bool isStatusEnquiry) { User user = UserHelper.GetUserByOrderId(orderid); string toAddress = user.Email; bool isSent = false; try { StringBuilder emailBody = new StringBuilder(File.ReadAllText(ConfigurationManager.AppSettings["EmailFullPath"].ToString())); emailBody = emailBody.Replace(Constant.CONST_FIRSTNAME, user.FirstName); emailBody = emailBody.Replace(Constant.CONST_LASTNAME, user.LastName); string subject = Constant.CONST_ORDERCOMPLETE_SUBJECT; subject = subject.Replace(Constant.CONST_ORDERID, orderid.ToString()); MailMessage mail = new MailMessage(ConfigurationManager.AppSettings["Email"].ToString(), toAddress, subject, emailBody.ToString()); mail.IsBodyHtml = true; string PDF_File_Path = GetFilePath() + "\\" + orderid.ToString() + ".pdf"; FileStream stream = File.OpenRead(PDF_File_Path); mail.Attachments.Add(new Attachment(stream, orderid + ".pdf")); SmtpClient smtp = new SmtpClient(); smtp.Send(mail); EmailSendLog emailSendLog = new EmailSendLog(); emailSendLog.OrderId = orderid; emailSendLog.UserMailId = toAddress; emailSendLog.InsertTime = DateTime.Now; OrderHelper.SaveMailSendLog(emailSendLog); isSent = true; } catch (Exception ex) { } return(isSent); }