示例#1
0
        public static Return Send(string fromEmailAddress, IEnumerable <MailAddress> emailAddresses, string subject, string body, EmailMode emailMode = EmailMode.Both, bool bcc = true)
        {
            Return returnObj = new Return();
            var    emailLog  = new EmailLog();

            if (emailMode == EmailMode.Both || emailMode == EmailMode.Smtp)
            {
                try
                {
                    MailMessage message = new MailMessage();

                    foreach (MailAddress address in emailAddresses)
                    {
                        if (bcc)
                        {
                            message.Bcc.Add(address);
                        }
                        else
                        {
                            message.To.Add(address);
                        }
                    }

                    message.Sender = new MailAddress(fromEmailAddress);

                    message.IsBodyHtml = true;
                    message.Subject    = subject;
                    message.Body       = body;

                    emailLog = GetEmailLogFromMailMessage(message);

                    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

                    client.Send(message);

                    emailLog.ServerMessage = "Successfully sent email";

                    EmailsMapper.Insert(emailLog);

                    return(returnObj);
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogException(ex);

                    returnObj.Error = ErrorHelper.CreateError(ex);

                    emailLog.ServerMessage = returnObj.Error.Message;

                    EmailsMapper.Insert(emailLog);

                    if (emailMode == EmailMode.Both)
                    {
                        var directSentReturn = SendDirectMessage(fromEmailAddress, emailAddresses, subject, body, bcc);

                        if (directSentReturn.IsError)
                        {
                            return(directSentReturn);
                        }
                        else
                        {
                            returnObj = BaseMapper.GenerateReturn();
                        }
                    }

                    return(returnObj);
                }
            }
            else
            {
                return(SendDirectMessage(fromEmailAddress, emailAddresses, subject, body));
            }
        }
示例#2
0
        private static Return SendDirectMessage(string fromEmailAddress, IEnumerable <MailAddress> emailAddresses, string subject, string body, bool bcc = true)
        {
            var returnObj = new Return();
            var emailLog  = new EmailLog();

            Message message = new Message();

            message.Subject = subject;
            message.From    = new Address(fromEmailAddress);

            foreach (var mailAddress in emailAddresses)
            {
                if (bcc)
                {
                    message.Bcc.Add(mailAddress.Address);
                }
                else
                {
                    message.To.Add(mailAddress.Address);
                }
            }


            message.IsHtml        = true;
            message.BodyHtml.Text = body;
            message.BodyText.Text = body;

            emailLog.FromEmailAddress   = message.From.Email;
            emailLog.SenderName         = message.Sender.Name;
            emailLog.SenderEmailAddress = message.Sender.Email;
            emailLog.Subject            = message.Subject;
            emailLog.ToEmailAddresses   = "";
            emailLog.VisitorIP          = "";
            emailLog.RequestUrl         = "";
            emailLog.ServerMessage      = "";
            emailLog.Message            = body;

            try
            {
                var mailMessage = new MailMessage();
                mailMessage.CopyFrom(message);

                var returnStr = ActiveUp.Net.Mail.SmtpClient.DirectSend(message);

                if (!string.IsNullOrEmpty(returnStr))
                {
                    emailLog.ServerMessage = returnObj?.Error?.Message;

                    if (emailLog.ServerMessage == null)
                    {
                        emailLog.ServerMessage = "";
                    }

                    EmailsMapper.Insert(emailLog);
                }

                return(returnObj);
            }
            catch (Exception ex)
            {
                ErrorHelper.LogException(ex);

                returnObj.Error = ErrorHelper.CreateError(ex);

                emailLog.ServerMessage = returnObj?.Error?.Message;

                if (emailLog.ServerMessage == null)
                {
                    emailLog.ServerMessage = "";
                }

                EmailsMapper.Insert(emailLog);

                return(returnObj);
            }
        }