Tuesday, 17 April 2012

How to send Mail?


 I am explaining that how to send email using gmail in asp.net or send email using gmail smtp server in asp.net using c#.

Describe about smtp server and setting forwording POP/IMAP


If you dont have smtp server in your system so that you can create gmail smtp server in your application and send mail through asp.net application. Please first you can configure gmail account settingforwording and pop/imap.


If you must have add reference to your application  System.Web.Mail namespace than use system.net.mail.
The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery in asp.net application for that we need to add System.web.dll reference to our application.




     i. On the Project menu, click Add Reference.
     ii. On the .NET tab, locate System.Web.dll, and then click Select.

     iii. Click OK in the Add References.






using System.Net;

 using System.Net.Mail;
 protected void Button2_Click(object sender, EventArgs e)
{

if (TextBox2.Text == "")
{

Page.ClientScript.RegisterClientScriptBlock(
typeof(Page), "Script", "alert('Please enter correct email_id')", true);
}

else if (TextBox3.Text == "")
{

Page.ClientScript.RegisterClientScriptBlock(
typeof(Page), "Script", "alert('Please enter content')", true);
}

else
{

String s = TextBox2.Text;
String[] toId = s.Split(';');

MailMessage mail = new MailMessage();
for (int i = 0; i <= toId.Length - 1; i++)
{

mail.To.Add(toId[i].ToString());

}

mail.To.Add(
new MailAddress(TextBox2.Text));
mail.From = new MailAddress("give your user id");

mail.IsBodyHtml = true;
mail.Subject = TextBox3.Text;

mail.Body = TextBox4.Text;

SmtpClient smtp1 = new SmtpClient();
smtp1.Host = "smtp.gmail.com";
smtp1.Port = 587;

smtp1.UseDefaultCredentials =
false;
smtp1.Credentials = new NetworkCredential("give your user id", "password");
smtp1.EnableSsl = true;
byte[] data = new byte[1024];
System.IO.MemoryStream stream = new System.IO.MemoryStream(data);
Attachment attach = new Attachment(stream, "Attachment file name");

mail.Attachments.Add(attach);

smtp1.Send(mail);



t1.Text =
"";
t2sender.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
lblMessage.Visible = true;

 

lblMessage.Text =
"Message sent.";
}

}

2 comments:

  1. all cool..but which port u r using...
    n
    how i can find my own appropriate port to send a mail

    ReplyDelete

What is ASP.NET? Components of ASP.NET

ASP.Net Definition -It is used for creating web-based applications.ASP.Net is a web development platform provided by Microsoft. ASP.NET i...