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.
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.";
}
}
all cool..but which port u r using...
ReplyDeleten
how i can find my own appropriate port to send a mail
Bydefault Gmail Port
ReplyDelete