Пример #1
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);
        }
Пример #2
0
        /// <summary>
        /// Send email
        /// </summary>
        /// <param name="emailAccount">Email account</param>
        /// <param name="sendOptions">Email send options</param>
        /// <returns>Return the email send result</returns>
        public static async Task <List <SendEmailResult> > SendAsync(EmailAccount emailAccount, params SendEmailOptions[] sendOptions)
        {
            if (emailAccount == null || sendOptions.IsNullOrEmpty())
            {
                return(new List <SendEmailResult>(0));
            }
            List <SendEmailOptions> syncInfos  = new List <SendEmailOptions>();
            List <SendEmailOptions> asyncInfos = new List <SendEmailOptions>();

            foreach (var sendOption in sendOptions)
            {
                //Set additional
                SetAdditional(sendOption);

                if (sendOption == null)
                {
                    continue;
                }
                if (sendOption.Asynchronously)
                {
                    asyncInfos.Add(sendOption);
                }
                else
                {
                    syncInfos.Add(sendOption);
                }
            }
            if (!asyncInfos.IsNullOrEmpty())
            {
                InternalQueueSendEmailItem emailInternalMessageCommand = new InternalQueueSendEmailItem()
                {
                    Datas        = asyncInfos,
                    EmailAccount = emailAccount
                };
                InternalQueueManager.GetQueue(EZNEWConstants.InternalQueueNames.Message).Enqueue(emailInternalMessageCommand);
            }
            if (syncInfos.IsNullOrEmpty())
            {
                return(new List <SendEmailResult>(1)
                {
                    SendEmailResult.SuccessResult(null)
                });
            }
            return(await ExecuteSendAsync(emailAccount, syncInfos).ConfigureAwait(false));
        }