Example #1
0
        public bool SendEmail(ObjectId tenantId, string[] to, string subject, string body, string[] cc = null, string[] bcc = null, string reply = null)
        {
            try
            {
                MailerConfiguration mailConfig = Utilities.Instance.GetMailerConfiguration();

                MailMessage mailMessage = new MailMessage();
                SmtpClient  smtpServer  = new SmtpClient(mailConfig.ClientURL, mailConfig.ClientPort);
                mailMessage.From = new MailAddress(mailConfig.MailAddress);

                if (reply != null)
                {
                    mailMessage.ReplyToList.Add(reply);
                }
                if (cc != null)
                {
                    mailMessage.CC.Add(string.Join(",", cc));
                }
                if (bcc != null)
                {
                    mailMessage.Bcc.Add(string.Join(",", bcc));
                }
                mailMessage.To.Add(to != null && to.Length > 0 ? string.Join(",", to) : mailConfig.MailAddress);
                mailMessage.Subject      = subject;
                mailMessage.IsBodyHtml   = true;
                mailMessage.BodyEncoding = Encoding.GetEncoding("utf-8");
                mailMessage.Body         = body;

                var tenant = _tenant ?? DynamicHelper.GetTenant(_dbPool, _cache, tenantId);
                if (tenant != null)
                {
                    // If Body contains "cid:Logo_Email", add Theme_Logo_Email to attachments
                    if (body.Contains("cid:Logo_Email"))
                    {
                        var logoAttachment = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath("~" + tenant.Theme_Logo_Email))
                        {
                            ContentId = "Logo_Email"
                        };
                        mailMessage.Attachments.Add(logoAttachment);
                    }
                    // If Body contains "cid:Bullet", add Theme_Bullet_Image to attachments
                    if (body.Contains("cid:Bullet"))
                    {
                        var bulletAttachment = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath("~" + tenant.Theme_Bullet_Image))
                        {
                            ContentId = "Bullet"
                        };
                        mailMessage.Attachments.Add(bulletAttachment);
                    }
                }

                smtpServer.EnableSsl             = true;
                smtpServer.UseDefaultCredentials = false;
                smtpServer.Credentials           = new System.Net.NetworkCredential(mailConfig.Username, mailConfig.Password);

                smtpServer.Send(mailMessage);
            }
            catch (Exception ex)
            {
                if (_log != null)
                {
                    _log.LogError(ex, string.Join(",", to));
                }
            }

            return(true);
        }