SoFunction
Updated on 2025-03-07

C# Example code for sending html-formatted mail using MailAddress class


public class Email
    {
        /// <summary>
/// Sender
        /// </summary>
        public string mailFrom { get; set; }

        /// <summary>
/// recipient
        /// </summary>
        public string[] mailToArray { get; set; }

        /// <summary>
/// Cc
        /// </summary>
        public string[] mailCcArray { get; set; }

        /// <summary>
/// Title
        /// </summary>
        public string mailSubject { get; set; }

        /// <summary>
/// Text
        /// </summary>
        public string mailBody { get; set; }

        /// <summary>
/// Sender password
        /// </summary>
        public string mailPwd { get; set; }

        /// <summary>
/// SMTP mail server
        /// </summary>
        public string host { get; set; }

        /// <summary>
/// Is the text in html format
        /// </summary>
        public bool isbodyHtml { get; set; }

        /// <summary>
/// appendix
        /// </summary>
        public string[] attachmentsPath { get; set; }

        public bool Send()
        {
//Initialize the MailAddress instance with the specified email address
            MailAddress maddr = new MailAddress(mailFrom);
//Initialize the MailMessage instance
            MailMessage myMail = new MailMessage();


//Add an email address to the recipient address collection
            if (mailToArray != null)
            {
                for (int i = 0; i < ; i++)
                {
                    (mailToArray[i].ToString());
                }
            }

//Add an email address to the cc recipient address collection
            if (mailCcArray != null)
            {
                for (int i = 0; i < ; i++)
                {
                    (mailCcArray[i].ToString());
                }
            }
//Sender address
            = maddr;

//Title of the email
            = mailSubject;

//Coding used for the subject content of the email
            = Encoding.UTF8;

//Email text
            = mailBody;

//Coding of the email body
            = ;

            = ;

            = isbodyHtml;

//Add attachments if there are attachments
            try
            {
                if (attachmentsPath != null && > 0)
                {
                    Attachment attachFile = null;
                    foreach (string path in attachmentsPath)
                    {
                        attachFile = new Attachment(path);
                        (attachFile);
                    }
                }
            }
            catch (Exception err)
            {
throw new Exception("There is an error when adding attachment:" + err);
            }

            SmtpClient smtp = new SmtpClient();
//Specify the sender's email address and password to verify the sender's identity
            = new (mailFrom, mailPwd);


//Set up the SMTP mail server
            = host;

            try
            {
//Send email to SMTP mail server
                (myMail);
                return true;

            }
            catch ( ex)
            {
                return false;
            }

        }
    }