C#

how to send email from c sharp net

 The following C# source code shows how to send an email

First Create Class for sending mail.

    public class EmailModel

    {

        public List<string> To { get; set; } = new List<string>();

        public List<string> CC { get; set; } = new List<string>();

        public List<string> BCC { get; set; } = new List<string>();

        public string Subject { get; set; }

        public string Body { get; set; }

        public bool EnableSSL { get; set; }

        public string FromMail { get; set; }

        public int Port { get; set; }

        public string FileName { get; set; }

        public string SmtpServer { get; set; }

        

    }


    public class MailAddresses

    {

        public List<string> To { get; set; } = new List<string>();

        public List<string> CC { get; set; } = new List<string>();

    }


After that set some keys in web.config file or you can use in code also but for best practice you have to set in web.config file.

<!--email configuration-->

<add key="EmailFrom" value="pawan.kumar@gmail.com" />

<add key="PWD" value="Password@123" />

<add key="host" value="smtp.gmail.com" />

<add key="port" value="587" />

<add key="EnableSSL" value="true" />

<add key="MaildisplayName" value="no-reply"/>

<!--end email configuration-->


use these key in mail sending method. I have created one common method for sending mail. you can use this method in any type application like Console application, web application etc.



//this is the method which we use to send mail from gmail smtp server.   

 public void SendMail(EmailModel emailModel)

    {

 

            try

            {

                using (MailMessage mail = new MailMessage())

                {

                    emailModel.FromMail = ConfigurationManager.AppSettings["EmailFrom"];

                    string pwd = ConfigurationManager.AppSettings["PWD"];

                    string _displayName= ConfigurationManager.AppSettings["MaildisplayName"];

                    emailModel.SmtpServer = ConfigurationManager.AppSettings["host"];

                    emailModel.Port =                                      Convert.ToInt32(ConfigurationManager.AppSettings["port"]);

                    emailModel.EnableSSL =  Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSSL"]);

 

                    if (emailModel.To != null && emailModel.To.Count > 0)

                    {

                        foreach (var item in emailModel.To)

                            mail.To.Add(item);

                    }

                   

                    if (emailModel.CC != null && emailModel.CC.Count > 0)

                    {

                        foreach (var item in emailModel.CC)

                            mail.CC.Add(item);

                    }

                    mail.From = new MailAddress(emailModel.FromMail, _displayName);

                    mail.Subject = emailModel.Subject;

                    mail.Body = emailModel.Body;

                    

                     //if you want to send attachment then use this if condition other you can                     //skip this condition

                    if(!string.IsNullOrWhiteSpace(emailModel.FilePath))

                    {

                        //Attaching File to Mail

                        Attachment attachment = new  System.Net.Mail.Attachment(emailModel.FilePath);

                        mail.Attachments.Add(attachment);

                    }

                    mail.IsBodyHtml = true;

                    

                    SmtpClient smtp = new SmtpClient();

                    smtp.Host = emailModel.SmtpServer;

                    

                    smtp.EnableSsl = emailModel.EnableSSL;

                    NetworkCredential NetworkCred = new  NetworkCredential(emailModel.FromMail, pwd);

                    smtp.UseDefaultCredentials = true;

                    smtp.Credentials = NetworkCred;

                    smtp.Port = emailModel.Port;

                    

                    smtp.Send(mail);

                }

            }

 

            catch (Exception ex)

            {

            }

            

            

        }


Below is code for calling method.

//Create model class object and assign mail address list which 

//you want to send in TO and CC and pass all other fields which you 

//want to send in mail

                EmailModel emailModel = new EmailModel();

                emailModel.To = mailAddress.To;

                emailModel.CC = mailAddress.CC;

 

               emailModel.Subject = "Email Testing";

            //call the SendMail method either in same class or from other place 

               SendMail(emailModel);

 

No comments:

Post a Comment