Send Email and attachments in C#

Discussion in 'C#' started by naimish, Jul 18, 2009.

?

Was this useful ?

  1. Yes

    88.9%
  2. No

    11.1%
  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    Introduction



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

    Background



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

    The code



    Code:
    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)
        {
            try
            {
                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;
    
                    mailMessage.CC.Add(emailCC);
                }
                if (!String.IsNullOrEmpty(emailBCC))
                {
                    bool ValidateBCCEmailAddress = ValidateEmailAddress(emailBCC);
                    if (ValidateBCCEmailAddress == false)
                        return "Invalid BCC Email Address: " + emailBCC;
                    mailMessage.Bcc.Add(emailBCC);
                }
    
                foreach (string attach in attachments)
                {
                    Attachment attached = new Attachment(attach, MediaTypeNames.Application.Octet);
                    mailMessage.Attachments.Add(attached);
                }
                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)
                {
                    smtpClient.Send(mailMessage);
                }
                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)
        {
            try
            {
                string TextToValidate = emailAddress;
                Regex expression = new Regex(@"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}");
    
                if (expression.IsMatch(TextToValidate))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
     
  2. msn90

    msn90 New Member

    Joined:
    Jul 17, 2009
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    gr8 post. This post is very useful for me.Thanks for solving my problem.
     
  3. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Thanks buddy :)
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Does anyone know any free email provider server ?
     
  6. sk.mor100

    sk.mor100 New Member

    Joined:
    Jan 7, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    dear sir,
    please give me C# book in the form of pdf of html format
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice