Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/articles/c-sharp-tutorials/)
-   -   Send Email and attachments in C# (http://www.go4expert.com/articles/send-email-attachments-c-sharp-t18599/)

naimish 18Jul2009 10:21

Send Email and attachments in C#


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 21Jul2009 14:27

Re: Send Email and attachments in C#
gr8 post. This post is very useful for me.Thanks for solving my problem.

naimish 21Jul2009 14:28

Re: Send Email and attachments in C#
Thanks buddy :)

shabbir 3Aug2009 14:33

Re: Send Email and attachments in C#
Nominate this article for Article of the month - Jul 2009

naimish 20Aug2009 15:53

Re: Send Email and attachments in C#
Does anyone know any free email provider server ?

sk.mor100 7Jan2011 13:07

Re: Send Email and attachments in C#
dear sir,
please give me C# book in the form of pdf of html format

All times are GMT +5.5. The time now is 03:29.