How to send email in asp.net mvc using Gmail
Microsoft Net Framework
  • .Net
  • 380
  • February-24-2018
  • by 

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" });

 

 

Share This with your friend by choosing any social account


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM