/// <summary> /// Get a success result /// </summary> /// <param name="sendInfo">Email send info</param> /// <returns>Return the email send result</returns> public static EmailSendResult SuccessResult(EmailSendInfo sendInfo) { return(new EmailSendResult() { Success = true, SendInfo = sendInfo }); }
/// <summary> /// Get email account /// </summary> /// <param name="sendInfo">Email send info</param> /// <returns>Return the email accounts</returns> static EmailAccount GetAccount(EmailSendInfo sendInfo) { if (sendInfo == null) { return(null); } var emailAccount = GetEmailAccount?.Invoke(sendInfo); if (emailAccount == null) { throw new EZNEWException("No mail sending account was specified"); } return(emailAccount); }
/// <summary> /// Execute send email /// </summary> /// <param name="sendInfo">Email send info</param> /// <param name="account">Email account</param> /// <returns>Return email send result</returns> async Task <EmailSendResult> ExecuteSendAsync(EmailSendInfo sendInfo, EmailAccount account) { try { var toEmailAddress = sendInfo.EmailAddress.Where(email => email.IsEmail());//clear out irregular email addresses if (toEmailAddress.IsNullOrEmpty()) { return(EmailSendResult.Empty); } MailAddress fromMailAddress = new MailAddress(account.SendEmailAddress, account.SendPersonName);//sender MailMessage nowMessage = new MailMessage { Sender = fromMailAddress, From = fromMailAddress }; foreach (string email in toEmailAddress) { nowMessage.To.Add(new MailAddress(email)); } nowMessage.Subject = sendInfo.Subject; nowMessage.SubjectEncoding = sendInfo.SubjectEncoding; nowMessage.Body = sendInfo.Content; nowMessage.BodyEncoding = sendInfo.BodyEncoding; nowMessage.IsBodyHtml = sendInfo.BodyIsHtml; SmtpClient smtpClient = new SmtpClient { DeliveryMethod = SmtpDeliveryMethod.Network, EnableSsl = account.EnableSsl, Host = account.SmtpAddress, Port = int.Parse(account.Port), UseDefaultCredentials = false, Credentials = new System.Net.NetworkCredential(account.UserName, account.Password) }; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; await smtpClient.SendMailAsync(nowMessage).ConfigureAwait(false); return(EmailSendResult.SuccessResult(sendInfo)); } catch (Exception ex) { return(EmailSendResult.FailResult(ex.Message, ex, sendInfo)); } }
/// <summary> /// Get a fail result /// </summary> /// <param name="message">Message</param> /// <param name="exception">Exception</param> /// <param name="sendInfo">Email send info</param> /// <returns>Return the email send result</returns> public static EmailSendResult FailResult(string message = "", Exception exception = null, EmailSendInfo sendInfo = null) { return(new EmailSendResult() { Success = false, Exception = exception, Message = message, SendInfo = sendInfo }); }