Mzsmunna
1/23/2020 - 1:53 PM

Emailer - send mail

<system.net> </system.net>


using Cerebro.JWTAuthService.Helper;
using Cerebro.JWTAuthService.Models;
using GTW.Sync.Entity;
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Web;

namespace GTW.Sync.MOC.Helper
{
    public static class Emailer
    {
        private static EmailConfig _emailConfig;

        public static void CustomInitialize(JWTHelperUtility utility)
        {
            _emailConfig = utility.GetEmailConfig(ApplicationConstants.ManualProcess ? "gateway-email-config-dev" : "gateway-email-config-live");
        }

        public static void CustomInitializeForPostage(JWTHelperUtility utility)
        {
            _emailConfig = utility.GetEmailConfig(ApplicationConstants.ManualProcess ? "gateway-postageamount-notification-email-config-dev" : "gateway-postageamount-notification-email-config-live");
        }

        public static void SendEmail(string to, string cc, string bcc, string fromName, string subject, string body, Attachment attachment = null)
        {
            try
            {
                var smtpClient = new SmtpClient
                {
                    Host = _emailConfig.Host,
                    Port = _emailConfig.Port,
                    EnableSsl = _emailConfig.EnableSSL,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(_emailConfig.UserName, _emailConfig.Password),
                    DeliveryMethod = SmtpDeliveryMethod.Network
                };
                var message = new MailMessage
                {
                    From = new MailAddress(_emailConfig.UserName, fromName),
                    Subject = subject,
                    IsBodyHtml = true,
                    Body = body
                };
                if (!string.IsNullOrEmpty(to))
                {
                    string[] emails = to.Split(',');
                    foreach (string email in emails)
                    {
                        message.To.Add(email);
                    }
                }
                if (!string.IsNullOrEmpty(bcc))
                {
                    string[] emails = bcc.Split(',');
                    foreach (string email in emails)
                    {
                        message.Bcc.Add(email);
                    }
                }
                if (!string.IsNullOrEmpty(cc))
                {
                    string[] emails = cc.Split(',');
                    foreach (string email in emails)
                    {
                        message.CC.Add(email);
                    }
                }
                if (attachment != null)
                {
                    message.Attachments.Add(attachment);
                }
                smtpClient.Send(message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}