Tuesday, March 24, 2009

How to Send email to gmail using asp.net

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

How to Upload in asp.net

How to Upload in asp.net:

here is the code of upload.aspx.cs:

private string Directory;

protected void Page_Load(object sender, EventArgs e)
{
Directory = Path.Combine(Request.PhysicalApplicationPath, "uploads");

}

protected void Button1_Click(object sender, EventArgs e)
{

}
protected void Button1_Click1(object sender, EventArgs e)
{
if (FileUploader.PostedFile.FileName == "")
{
lblInfo.Text = "No file spefcified";
//lblInfo.Text =

}

else
{
string extension = Path.GetExtension(FileUploader.PostedFile.FileName);
switch (extension.ToLower())
{
case ".doc":
case ".docx":
case ".txt":
break;
default:
lblInfo.Text = "This file type is not supported";
return;
}

string serverFileName = Path.GetFileName(FileUploader.PostedFile.FileName);
string fullUpladPath = Path.Combine(Directory, serverFileName);

//string fullupladpath = Path.Combine(Directory, serverFileName);

try
{
FileUploader.PostedFile.SaveAs(fullUpladPath);
lblInfo.Text = "File" + serverFileName + " uplaod successfully to " + fullUpladPath;
lblInfo.Text = "Server Filename: " + serverFileName;
serverfpath.Text = "Server Path: " + fullUpladPath;

HyperLink11.Text = serverFileName;
HyperLink11.NavigateUrl = "http://www.fob05.co.cc/uploads/" + serverFileName;


}
catch (Exception err)
{
lblInfo.Text = err.Message.ToString();
}
}

}


Powerd by AVARICESOFT.com