How to Send email to gmail using asp.net
Sending email to gmail any address you need authentication i tell you the whole procedure step by step:
1st step:
Create the web-application
2nd step:
include in the top: using System.Net.Mail;
3rd step:
add those variable:
string first_name;
string last_name;
string email;
string question;
string message;
string to;
string subject;
System.Web.Mail.MailFormat pFormat;
4th step:
a) make the function: set_attribute which take parameter as all the 4 text box.
b) i have first name textbox, last name textbox, email textbox, question textbox.
c) here is the function: [which basically set the global attibutes]
d) i use send_mail() function here because it sets variable and send it .. send_mail() function are decribed under the set_attributes function
public void set_attributes(TextBox fn,TextBox ln,TextBox em, TextBox quest)
{
first_name = fn.Text;
last_name = ln.Text;
email = em.Text;
question = quest.Text;
send_main();
}
5th step:
a) Remember in code italics are required to change like username field,password field
b) make the send_main() function which basically send to your gmail address by using asp.net
c) i use lblerror this is a only a label .. for showing error.
d) in response.redirect you will redirect to any page like success , its basically a redirect and send the acknowlegment to the user that message has been send.
private void send_main()
{
to = "fastontologydeveloper@gmail.com";
subject = "Contact: " + email;
message += "FirstName = " + first_name + ",";
message += "LastName = " + last_name + ",";
message += "Email Address = " + email + ",";
message += "------------Question------------" + ",";
message += "Question = " + question + "";
try
{
MailMessage msg = new MailMessage(email, to, subject, message);
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("username", "password"); // don't use @ sign in the username
SmtpClient contact_smtpclient = new SmtpClient("smtp.gmail.com",587);
contact_smtpclient.EnableSsl = true;
contact_smtpclient.UseDefaultCredentials = false;
contact_smtpclient.Credentials = cred;
contact_smtpclient.Send(msg);
Response.Redirect("~/success.aspx");
}
catch (Exception err)
{
lblerror.Enabled = true;
lblerror.Text = err.Message.ToString();
}
Please say thanks.. and if you have any comments or query then feel free to ask.
Thanks..
Powered by AVARICESOFT.com
Tuesday, March 24, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment