How to send email in asp.net mvc using Gmail
.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" });
Share This with your friend by choosing any social account
You may also read following recent Post
![]() |
How old are you and what is your net worth? Also, what is your educational background and what sort of a job do you do? Do you enjoy your work?
47 By Nauman Shafi |
![]() |
create appointment booking page with available time slot using database ms sql server
49 By Junaid A |
![]() |
what is Asp.Net Core
64 By Junaid A |
![]() |
publish has encountered an error Object reference not set to an instance of an object A diagnostic log has been written to following location
96 By Junaid A |
![]() |
The provided URI scheme 'http' is invalid; expected 'https'." & vbCrLf & "Parameter name: via
105 By Junaid A |
![]() |
C# Language Basics
1667 By |
![]() |
Crud in Asp.NET using tabs
635 By Haider |
![]() |
Parser Error
699 By Usman Jafar |
![]() |
What are 3 C
192 By |
![]() |
what is .net
418 By |
![]() |
How to post uploaded file and form data in MVC using jquery?ts Common scenario where you want to pos
224 By |
![]() |
MVC url routing
263 By |
![]() |
Learn about Session
287 By |