Beispiel #1
0
        public static bool SendEmail
            (string fromAddress, string toAddress, string subject, string body)
        {
            var mm = new MailMessage(fromAddress, toAddress);

            mm.Subject    = subject;
            mm.Body       = UseTemplates ? EmailTemplate.Replace("{CONTENT}", body) : body;
            mm.IsBodyHtml = true;

            var smtp = new SmtpClient();

            smtp.Send(mm);

            if (_logEmails)
            {
                var l = new EmailLog
                {
                    SentDateTime = DateTime.Now,
                    SentFrom     = fromAddress,
                    SentTo       = toAddress,
                    Subject      = subject,
                    Body         = UseTemplates ? EmailTemplate.Replace("{CONTENT}", body) : body
                };
                l.Insert();
            }
            return(true);
        }
        public bool SendEmail(string fromAddress,
                              string toAddress,
                              string subject,
                              string body)
        {
            this.ErrorException = null;
            if (this.TestEmailDuringSetup)
            {
                UseTemplates = false;
                _logEmails   = false;
            }
            try {
                string mailBody;
                using (var mm = new MailMessage(fromAddress, toAddress)) {
                    if (UseTemplates)
                    {
                        mailBody = EmailTemplate.FormatWith(new {
                            Subject = subject,
                            Content = body
                        });
                    }
                    else
                    {
                        mailBody = body;
                    }

                    mm.Subject    = subject;
                    mm.Body       = mailBody;
                    mm.IsBodyHtml = true;

                    NetworkCredential credentials = null;
                    if (!string.IsNullOrEmpty(this.Login) &&
                        !string.IsNullOrEmpty(this.Password))
                    {
                        //smtp.Credentials
                        credentials = new NetworkCredential(this.Login, this.Password);
                    }


                    if (string.IsNullOrEmpty(this.Server))
                    {
                        using (var smtp = new SmtpClient()) {
                            if (credentials != null)
                            {
                                smtp.Credentials = credentials;
                            }
                            smtp.Send(mm);
                        }
                    }
                    else
                    {
                        using (var smtp = new SmtpClient(this.Server, this.Port)) {
                            if (credentials != null)
                            {
                                smtp.Credentials = credentials;
                            }
                            smtp.Send(mm);
                        }
                    }
                }
                if (_logEmails)
                {
                    var l = new EmailLog {
                        SentDateTime = DateTime.Now,
                        SentFrom     = fromAddress,
                        SentTo       = toAddress,
                        Subject      = subject,
                        Body         = mailBody
                    };
                    l.Insert();
                }
            } catch (Exception ex) {
                this.Log().Error("Unable to send email: {0}", ex.Message);
                this.ErrorException = ex;
                return(false);
            }
            return(true);
        }