View Poll Results: Was this useful ?
Yes 8 88.89%
No 1 11.11%
Voters: 9. You may not vote on this poll

Send Email and attachments in C#

naimish's Avatar author of Send Email and attachments in C#
This is an article on Send Email and attachments in C# in C#.
Rated 5.00 By 1 users


This solution describes an approach how to send email messages with or without attachments.


The EmailProgram class is used to send email messages with or without attachments.

The code

Code: CSharp
public class EmailProgram
    /// <summary>
    /// call this Method to send an Email with attachments
    /// </summary>
    /// <param name="emailFrom">From EmailID</param>
    /// <param name="emailTo">To Email ID</param>
    /// <param name="emailCC">CC Email ID</param>
    /// <param name="emailBCC">BCC Email ID</param>
    /// <param name="subject">Subject</param>
    /// <param name="body">Body</param>
    /// <param name="IsBodyHtml">EmailBodyFormat(Default It is false, If It is true EmailBody should in HTML Format)</param>
    /// <param name="attachmentPath">Attachment File Path</param>
    /// <returns>If sends successfully returns True otherwise False</returns>
    public String SendMail(String emailFrom,
    String emailTo,
    String emailCC,
    String emailBCC,
    String subject,
    String body,
    ArrayList attachments,
    bool IsBodyHtml)
            bool ValidateFromEmailAddress = ValidateEmailAddress(emailFrom);
            if (ValidateFromEmailAddress == false)
                return "Invalid From Email Address: " + emailFrom;
            bool ValidateToEmailAddress = ValidateEmailAddress(emailTo);
            if (ValidateToEmailAddress == false)
                return "Invalid To Email Address: " + emailTo;
            MailAddress mailFrom = new MailAddress(emailFrom);
            MailAddress mailTo = new MailAddress(emailTo);
            MailMessage mailMessage = new MailMessage(mailFrom, mailTo);
            mailMessage.Subject = subject;
            mailMessage.Body = body;
            mailMessage.IsBodyHtml = IsBodyHtml;
            if (!String.IsNullOrEmpty(emailCC))
                bool ValidateCCEmailAddress = ValidateEmailAddress(emailCC);
                if (ValidateCCEmailAddress == false)
                    return "Invalid CC Email Address: " + emailCC;

            if (!String.IsNullOrEmpty(emailBCC))
                bool ValidateBCCEmailAddress = ValidateEmailAddress(emailBCC);
                if (ValidateBCCEmailAddress == false)
                    return "Invalid BCC Email Address: " + emailBCC;

            foreach (string attach in attachments)
                Attachment attached = new Attachment(attach, MediaTypeNames.Application.Octet);
            String strSMTPServer = ConfigurationSettings.AppSettings.Get("SMTPServer");
            SmtpClient smtpClient = new SmtpClient(strSMTPServer);
            //smtpClient.Credentials = new NetworkCredential("UserName", "Password");//if credentials needed
            smtpClient.UseDefaultCredentials = true;
            if (mailMessage != null)
            return "Message sent to " + emailTo + " at " + DateTime.Now.ToString() + ".";

        catch (Exception ex)
            return ex.Message.ToString();
    /// <summary>
    /// check email address is valid or not
    /// </summary>
    /// <param name="emailAddress">email address to validate</param>
    /// <returns>True if email address is valid</returns>
    public bool ValidateEmailAddress(string emailAddress)
            string TextToValidate = emailAddress;
            Regex expression = new Regex(@"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}");

            if (expression.IsMatch(TextToValidate))
                return true;
                return false;
        catch (Exception)
msn90's Avatar, Join Date: Jul 2009
Go4Expert Member
gr8 post. This post is very useful for me.Thanks for solving my problem.
naimish's Avatar
Thanks buddy
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nominate this article for Article of the month - Jul 2009
naimish's Avatar
Does anyone know any free email provider server ?
sk.mor100's Avatar, Join Date: Jan 2011
Newbie Member
dear sir,
please give me C# book in the form of pdf of html format