Example #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 <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));
            }
        }
Example #2
0
        /// <summary>
        /// Send email
        /// </summary>
        /// <param name="emailAccount">Email account</param>
        /// <param name="sendInfos">Email send infos</param>
        /// <returns>Return the email send result</returns>
        public static async Task <List <EmailSendResult> > SendAsync(EmailAccount emailAccount, params EmailSendInfo[] sendInfos)
        {
            if (emailAccount == null || sendInfos.IsNullOrEmpty())
            {
                return(new List <EmailSendResult>(0));
            }
            List <EmailSendInfo> SyncInfos  = new List <EmailSendInfo>();
            List <EmailSendInfo> AsyncInfos = new List <EmailSendInfo>();

            foreach (var sendInfo in sendInfos)
            {
                if (sendInfo == null)
                {
                    continue;
                }
                if (sendInfo.Asynchronously)
                {
                    AsyncInfos.Add(sendInfo);
                }
                else
                {
                    SyncInfos.Add(sendInfo);
                }
            }
            if (!AsyncInfos.IsNullOrEmpty())
            {
                SendEmailInternalMessageCommand emailInternalMessageCommand = new SendEmailInternalMessageCommand()
                {
                    SendInfos    = AsyncInfos,
                    EmailAccount = emailAccount
                };
                InternalMessageQueue.Enqueue(emailInternalMessageCommand);
            }
            if (SyncInfos.IsNullOrEmpty())
            {
                return(new List <EmailSendResult>(1)
                {
                    EmailSendResult.SuccessResult(null)
                });
            }
            return(await ExecuteSendAsync(emailAccount, SyncInfos).ConfigureAwait(false));
        }