Send an email using the supplied string. - CSharp System.Net.Mail

CSharp examples for System.Net.Mail:SmtpClient

Description

Send an email using the supplied string.

Demo Code


using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Net;
using System.IO;/*from w ww .  j av  a  2  s .c o m*/
using System;

public class Main{
        /// <summary>
        /// Send an email using the supplied string.
        /// [Mabna Method]
        /// </summary>
        /// <param name="body">String that will be used i the body of the email.</param>
        /// <param name="subject">Subject of the email.</param>
        /// <param name="sender">The email address from which the message was sent.</param>
        /// <param name="toAddress"></param>
        /// <param name="isHtml"></param>
        /// <param name="attachment"></param>
        /// <returns>A boolean value indicating the success of the email send.</returns>
        public static bool Email(this string body, string subject, string sender, string toAddress, bool isHtml, string attachment)
        {
            try
            {
                // To
                var mailMsg = new MailMessage();
                string[] to = toAddress.Split(',');
                foreach (string s in to)
                {
                    if (s.IsValidEmailAddress())
                    {
                        mailMsg.To.Add(s);
                    }
                }

                // From
                var mailAddress = new MailAddress(sender);
                mailMsg.From = mailAddress;

                // Subject and Body
                mailMsg.Subject = subject;
                mailMsg.Body = body;
                mailMsg.IsBodyHtml = isHtml;

                //attachment 
                var a = new Attachment(attachment);
                mailMsg.Attachments.Add(a);

                // Init SmtpClient and send
                var smtpClient = new SmtpClient();

                smtpClient.Send(mailMsg);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }
        /// <summary>
        /// Send an email using the supplied string.
        /// [Mabna Method]
        /// </summary>
        /// <param name="body">String that will be used i the body of the email.</param>
        /// <param name="subject">Subject of the email.</param>
        /// <param name="sender">The email address from which the message was sent.</param>
        /// <param name="toAddress"></param>
        /// <param name="isHtml"></param>
        /// <returns>A boolean value indicating the success of the email send.</returns>
        public static bool Email(this string body, string subject, string sender, string toAddress, bool isHtml)
        {
            try
            {
                // To
                var mailMsg = new MailMessage();
                string[] to = toAddress.Split(',');
                foreach (string s in to)
                {
                    if (s.IsValidEmailAddress())
                    {
                        mailMsg.To.Add(s);
                    }
                }

                // From
                var mailAddress = new MailAddress(sender);
                mailMsg.From = mailAddress;

                // Subject and Body
                mailMsg.Subject = subject;
                mailMsg.Body = body;
                mailMsg.IsBodyHtml = isHtml;

                // Init SmtpClient and send
                var smtpClient = new SmtpClient();

                smtpClient.Send(mailMsg);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }
        /// <summary>
        /// check input string is valid email. 
        /// [Mabna Method]
        /// </summary>
        /// <param name="s">string to validate</param>
        /// <returns>bool</returns>
        public static bool IsValidEmailAddress(this string s)
        {
            var regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            return regex.IsMatch(s);
        }
}

Related Tutorials