How to send email in asp.net mvc using Gmail
- .Net
-
393
-
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