Since most of our applications send mail to notify users at some point sending mail started to become a must part of ourÂ
applications. So I decided to make a method to send mail with every parameter possible. This is an example of how to build
a static class that sends mail using SMTP server.
 public static class SMTPMail
    {
        public static void SendMail(string smtpAddress, string from, string to, string cc, string bcc, string body, string subject, bool isHtml, string attachmentFileNames, bool useCredentials, string userName, string password)
        {
            SendMail(smtpAddress, 25, from, to, cc, bcc, body, subject, isHtml, attachmentFileNames, useCredentials, userName, password);
        }Â
        public static void SendMail(string smtpAddress, string from, string to, string cc, string bcc, string body, string subject, bool isHtml, string attachmentFileNames)
        {
            SendMail(smtpAddress, 25, from, to, cc, bcc, body, subject, isHtml, attachmentFileNames, false, String.Empty, String.Empty);
        }Â
        public static void SendMail(string smtpAddress, string from, string to, string cc, string bcc, string body, string subject, bool isHtml)
        {
            SendMail(smtpAddress, 25, from, to, cc, bcc, body, subject, isHtml, String.Empty, false, String.Empty, String.Empty);
        }Â
        public static void SendMail(string smtpAddress, int portNumber, string from, string to, string cc, string bcc, string body, string subject, bool isHtml, string attachmentFileNames, bool useCredentials, string userName, string password)
        {
            SmtpClient insSmtpClient = new SmtpClient(smtpAddress, portNumber);
            MailMessage insMailMessage = new MailMessage();
            insMailMessage.From = new MailAddress(from);
            foreach (string strBcc in bcc.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                insMailMessage.Bcc.Add(strBcc);
            }
            foreach (string strTo in to.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                insMailMessage.To.Add(strTo);
            }
            foreach (string strCc in cc.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                insMailMessage.CC.Add(strCc);
            }
            insMailMessage.Body = body;
            insMailMessage.Subject = subject;
            insMailMessage.IsBodyHtml = isHtml;Â
            foreach (string strAtt in attachmentFileNames.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                insMailMessage.Attachments.Add(new Attachment(strAtt));
            }Â
            if (useCredentials)
            {
                NetworkCredential insNetworkCredential = new NetworkCredential(userName, password);
                insSmtpClient.Credentials = insNetworkCredential;
            }
            insSmtpClient.Send(insMailMessage);
        }
    }