using System.Net.Mail;
using System.Net;
public partial class sendmail : System.Web.UI.Page
{
protected void
Page_Load(object
sender, EventArgs e)
{
}
protected void
Button1_Click(object
sender, EventArgs e)
{
SmtpClient
smtpc = new SmtpClient("smtp.gmail.com");
smtpc.Port
= 587;
smtpc.EnableSsl
= true;
smtpc.UseDefaultCredentials
= false;
smtpc.Credentials
= new NetworkCredential(TextBox1.Text,
TextBox2.Text);
MailMessage
email = new MailMessage(TextBox1.Text,
TextBox3.Text, TextBox4.Text, TextBox5.Text);
try
{
smtpc.Send(email);
Label1.Text
= "Message has been successfully send";
}
catch
{
Label1.Text=
"server authentication is faild,check your id and password";
}
}
}
|
Description:- In above example,First i have include two Namespace to use the mail service in ASP.NET Application.
- using System.Net.Mail;
- using System.Net;
After that i have created a Smtpclient class object
(smtpc) and passed the Smtp server name and port Number
to the Smtpclient. After that i have passed the
our credential information to the Network through Smtpclient object(smtpc).
After that i have created a MailMessage class object(email) and
passed sender mail address, destination mail address,subject,message body
to the MailMessage class. After that send the mail message through Smtpclient
object(smtpc) as shown in above codes.