Exemplo n.º 1
0
        private bool DoSendEmailSync(SmtpClientWrapper smtpClientWrapper,
                                     IEnumerable <MailAddress> to,
                                     string subject, string body,
                                     MailAddress from,
                                     IEnumerable <MailAddress> cced               = null,
                                     IEnumerable <MailAddress> bcced              = null,
                                     IEnumerable <Attachment> attachments         = null,
                                     IEnumerable <LinkedResource> linkedResources = null,
                                     bool useSsl = false)
        {
            bool flag;

            try
            {
                var mailMessage = new MailMessage
                {
                    Subject    = subject,
                    Body       = body,
                    IsBodyHtml = true,
                    From       = from,
                };

                if (linkedResources != null && linkedResources.Any())
                {
                    AlternateView alternateView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                    foreach (LinkedResource linkedResource in linkedResources)
                    {
                        alternateView.LinkedResources.Add(linkedResource);
                    }
                    mailMessage.AlternateViews.Add(alternateView);
                }

                foreach (var toAddress in to)
                {
                    mailMessage.To.Add(toAddress);
                }

                if (attachments != null)
                {
                    foreach (Attachment attachment in attachments)
                    {
                        mailMessage.Attachments.Add(attachment);
                    }
                }

                if (cced != null)
                {
                    foreach (MailAddress addr in cced)
                    {
                        mailMessage.CC.Add(addr);
                    }
                }

                if (bcced != null)
                {
                    foreach (MailAddress addr in bcced)
                    {
                        mailMessage.Bcc.Add(addr);
                    }
                }

                if (useSsl)
                {
                    smtpClientWrapper.EnableSsl = true;
                }

                smtpClientWrapper.Send(mailMessage);
                flag = true;
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                this.LastError = exception;
                logger.Error("Error, while sending email. " + exception.Message, exception);
                flag = false;
            }
            return(flag);
        }
Exemplo n.º 2
0
        private bool SendEmailSync(SmtpClientWrapper smtpClientWrapper, IEnumerable <string> toName, IEnumerable <string> toEmail, string subject, string body, string fromName, string fromEmail,
                                   IEnumerable <Attachment> attachments,
                                   IEnumerable <MailAddress> cced  = null,
                                   IEnumerable <MailAddress> bcced = null,
                                   IEnumerable <LinkedResource> linkedResources = null,
                                   bool useSsl = false)
        {
            bool flag;

            try
            {
                MailMessage mailMessage = new MailMessage()
                {
                    Subject    = subject,
                    Body       = body,
                    IsBodyHtml = true
                };
                MailMessage mailAddress = mailMessage;
                if (linkedResources != null && linkedResources.Any <LinkedResource>())
                {
                    AlternateView alternateView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                    foreach (LinkedResource linkedResource in linkedResources)
                    {
                        alternateView.LinkedResources.Add(linkedResource);
                    }
                    mailAddress.AlternateViews.Add(alternateView);
                }
                List <string> list = toEmail.ToList <string>();
                List <string> strs = toName.With <IEnumerable <string>, List <string> >((IEnumerable <string> x) => x.ToList <string>());
                for (int i = 0; i < list.Count <string>(); i++)
                {
                    int    num  = i;
                    string str  = strs.With <List <string>, string>((List <string> x) => x.ElementAtOrDefault <string>(num) ?? string.Empty);
                    string str1 = list.ElementAt <string>(num);
                    if (!string.IsNullOrWhiteSpace(str1))
                    {
                        mailAddress.To.Add(new MailAddress(str1, str));
                    }
                }
                if (!string.IsNullOrWhiteSpace(fromName) && !string.IsNullOrWhiteSpace(fromEmail))
                {
                    mailAddress.From = new MailAddress(fromEmail, fromName);
                }
                if (attachments != null)
                {
                    foreach (Attachment attachment in attachments)
                    {
                        mailAddress.Attachments.Add(attachment);
                    }
                }
                if (cced != null && cced.Any <MailAddress>())
                {
                    foreach (MailAddress mailAddress1 in cced)
                    {
                        mailAddress.CC.Add(mailAddress1);
                    }
                }
                if (bcced != null && bcced.Any <MailAddress>())
                {
                    foreach (MailAddress mailAddress2 in bcced)
                    {
                        mailAddress.Bcc.Add(mailAddress2);
                    }
                }
                if (useSsl)
                {
                    smtpClientWrapper.EnableSsl = true;
                }
                smtpClientWrapper.Send(mailAddress);
                flag = true;
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                this.LastError = exception;
                logger.Error("Error, while sending email. " + exception.Message, exception);
                flag = false;
            }
            return(flag);
        }