How to send email in asp.net mvc using Gmail

  By    Posted on February-24-2018   219

.Net

How to send email in asp.net mvc using Gmail

 

1) First of all we need to import  following namespaces

using System.Net;
using System.Net.Mail;

2) Create a Gmail id, that we will use to send emails.

3) Write below function in your class/project

 

 public static bool SendEmailWithGmail(string body, string subject, string[] to)
        {

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            NetworkCredential basicCredential =new NetworkCredential("yourgmailid@gmail.com", "yourpassowrd");
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress("youremmail@gmail.com", "displayname");
            smtpClient.UseDefaultCredentials = false;
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = basicCredential;
            message.From = fromAddress;
            message.Subject = subject;
            //Set IsBodyHtml to true means you can send HTML email.
            message.IsBodyHtml = true;
            message.Body = body;

            foreach (var item in to)
            {
                message.To.Add(item);
            }


            try
            {
                smtpClient.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                //Error, could not send the message
                // Response.Write(ex.Message);
            }

 


        }
    }

 

this code will send email on your provided details

 

we can call this function to send email

 

 SMTPMailClient.SendEmail("your passwords", "subject", new string[] { "toemail@gmail.com" });

 

 

How to send email in asp.net mvc using Gmail

 

1) First of all we need to import  following namespaces

using System.Net;
using System.Net.Mail;

2) Create a Gmail id, that we will use to send emails.

3) Write below function in your class/project

 

 public static bool SendEmailWithGmail(string body, string subject, string[] to)
        {

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            NetworkCredential basicCredential =new NetworkCredential("yourgmailid@gmail.com", "yourpassowrd");
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress("youremmail@gmail.com", "displayname");
            smtpClient.UseDefaultCredentials = false;
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = basicCredential;
            message.From = fromAddress;
            message.Subject = subject;
            //Set IsBodyHtml to true means you can send HTML email.
            message.IsBodyHtml = true;
            message.Body = body;

            foreach (var item in to)
            {
                message.To.Add(item);
            }


            try
            {
                smtpClient.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                //Error, could not send the message
                // Response.Write(ex.Message);
            }

 


        }
    }

 

this code will send email on your provided details

 

we can call this function to send email

 

 SMTPMailClient.SendEmail("your passwords", "subject", new string[] { "toemail@gmail.com" });

 

 

By      24-Feb-2018 Views  219



You may also read following recent Post

Recent Column What are 3 C
192  By
Recent Column what is .net
418  By