Send Email using Gmail ASP.NET C# VB.NET
If you want to send email using your Gmail account or using Gmail's smtp server in ASP.NET application or if you don't have a working smtp server to send mails using your ASP.NET application or aspx page than sending e-mail using Gmail is best option.
First of all add below mentioned namespace in code behind of aspx page from which you want to send the mail.
Now write this code in click event of button
using System.Net.Mail;
C# code
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("jainamit.agra@gmail.com");
mail.To.Add("amit_jain_online@yahoo.com");
mail.From = new MailAddress("jainamit.agra@gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
smtp.EnableSsl = true;
smtp.Send(mail);
}VB.NET code
Imports System.Net.Mail
Protected Sub Button1_Click
(ByVal sender As Object, ByVal e As EventArgs)
Dim mail As MailMessage = New MailMessage()
mail.To.Add("jainamit.agra@gmail.com")
mail.To.Add("amit_jain_online@yahoo.com")
mail.From = New MailAddress("jainamit.agra@gmail.com")
mail.Subject = "Email using Gmail"
String Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET"
mail.Body = Body
mail.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.Credentials = New System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword")
smtp.EnableSsl = True
smtp.Send(mail)
End SubChange YourUserName@gmail.com to your gmail ID and YourGmailPassword to Your password for Gmail account and test the code.
Than you need to check your Gmail username and password.
Hope this helps
0 comments:
Post a Comment