Esempio n. 1
0
 /// <summary>
 /// Get a success result
 /// </summary>
 /// <param name="sendInfo">Email send info</param>
 /// <returns>Return the email send result</returns>
 public static SendEmailResult SuccessResult(SendEmailOptions sendInfo)
 {
     return(new SendEmailResult()
     {
         Success = true,
         SendInfo = sendInfo
     });
 }
Esempio n. 2
0
 static void SetAdditional(SendEmailOptions emailOptions)
 {
     if (emailOptions != null)
     {
         //work id
         emailOptions.AddWorkId();
     }
 }
Esempio n. 3
0
        /// <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 <SendEmailResult> ExecuteSendAsync(SendEmailOptions sendInfo, EmailAccount account)
        {
            SendEmailResult result = null;

            try
            {
                var toEmailAddress = sendInfo.Emails.Where(email => email.IsEmail());//clear out irregular email addresses
                if (toEmailAddress.IsNullOrEmpty())
                {
                    return(SendEmailResult.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);

                result = SendEmailResult.SuccessResult(sendInfo);
            }
            catch (Exception ex)
            {
                LogManager.LogError <NetEmailProvider>(ex.Message, ex);
                result = SendEmailResult.FailResult(ex.Message, ex, sendInfo);
            }
            if (result != null)
            {
                result.EmailAccount = account;
                result.SendInfo     = sendInfo;
            }
            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// Get email account
        /// </summary>
        /// <param name="sendInfo">Email send info</param>
        /// <returns>Return the email accounts</returns>
        static EmailAccount GetAccount(SendEmailOptions 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);
        }
Esempio n. 5
0
 /// <summary>
 /// Send email
 /// </summary>
 /// <param name="sendEmailOptions">Send email options</param>
 /// <returns>Return send result</returns>
 public static SendEmailResult Send(SendEmailOptions sendEmailOptions)
 {
     return(SendAsync(sendEmailOptions).Result);
 }
Esempio n. 6
0
 /// <summary>
 /// Send email
 /// </summary>
 /// <param name="sendEmailOptions">Send email options</param>
 /// <returns>Return send result</returns>
 public static async Task <SendEmailResult> SendAsync(SendEmailOptions sendEmailOptions)
 {
     return((await SendAsync(new SendEmailOptions[1] {
         sendEmailOptions
     }).ConfigureAwait(false))?.FirstOrDefault() ?? SendEmailResult.Empty);
 }
Esempio n. 7
0
 /// <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 SendEmailResult FailResult(string message = "", Exception exception = null, SendEmailOptions sendInfo = null)
 {
     return(new SendEmailResult()
     {
         Success = false,
         Exception = exception,
         Message = message,
         SendInfo = sendInfo
     });
 }