Wednesday, June 19, 2019

c# Method to send email with attachment



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net.Mime;

namespace SendEmail
{
    public class SMTPHelper
    {

        //SMTP for email - set these up in project --> Property --> Settings (app.config)
        public static string _SmtpServer;
        public static int _SmtpPort;
        public static Boolean _SmtpIsAuthenticated;
        public static string _SmtpUserName;
        public static string _SmtpPassword;
        public static string _SmtpDomain;

        public static string _SmtpFrom;
        public static string _SmtpDefaultTo;
        public static string _DebugTo;
        public static bool _isDebug;

        public SMTPHelper()
        {

            _SmtpServer = Properties.Settings.Default.smtpServer;
            _SmtpPort = Properties.Settings.Default.smtpPort;
            _SmtpIsAuthenticated = Properties.Settings.Default.smtpIsAuthenticated;
            _SmtpUserName = Properties.Settings.Default.smtpUserName;
            _SmtpPassword = Properties.Settings.Default.smtpPassword;
            _SmtpDomain = Properties.Settings.Default.smtpDomain;


            _SmtpFrom = Properties.Settings.Default.smtpFrom;
            _SmtpDefaultTo = Properties.Settings.Default.smtpDefaultTo;

            _DebugTo = Properties.Settings.Default.smtpDebugTo;
            _isDebug = Properties.Settings.Default.DebugMode;



        }

        public string SendEmail(string emailAddresses, string subject, string message, string submessage, bool isHighPriority, string fileToAttach)
        {
            string retObj = null;
            try
            {

                //emailAddresses = (emailAddresses == "" || emailAddresses == null) ? _SmtpDefaultTo : emailAddresses;
                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                msg.Subject = subject;
                string toAddress = (_isDebug) ? _DebugTo : emailAddresses;
                string[] toArray = toAddress.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in toArray)
                {
                    msg.To.Add(item);
                }


                msg.From = new System.Net.Mail.MailAddress(_SmtpFrom);
                msg.IsBodyHtml = true;
                msg.Body = message + "<p> " + submessage + "</p>";
                if (_isDebug)
                {
                    msg.Body += "<h3>Debug Mode is On. <br/> Intended Email Addresses: " + emailAddresses + "</h3>";
                }
                //msg.Body += (ex == null) ? "" : ex.ToString();
                //msg.AlternateViews.Add(view);

                msg.Priority = (isHighPriority) ? System.Net.Mail.MailPriority.High : System.Net.Mail.MailPriority.Normal;
                msg.IsBodyHtml = true;
                Attachment data = new Attachment(fileToAttach, MediaTypeNames.Application.Octet);
                msg.Attachments.Add(data);
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(_SmtpServer);
                smtp.Port = _SmtpPort;

                if (_SmtpIsAuthenticated)
                {
                    smtp.Credentials = new System.Net.NetworkCredential(_SmtpUserName, _SmtpPassword, _SmtpDomain);
                }

                smtp.Send(msg);

            }
            catch (Exception e)
            {
                // log the exception
                retObj = e.ToString();
                string errorStr = "General Exception Email: " + e.ToString();
                Utility.LogWrite(errorStr);
                Console.WriteLine("General Exception Email: {0}", e);
            }

            return retObj;
        }
    }
}

No comments:

Post a Comment