System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(Textfrom.Text, Textto.Text);
mail.To.Add(Textto.Text);
mail.From = new MailAddress(Textfrom.Text, "One Ghost", System.Text.Encoding.UTF8);
mail.Subject = txtsubject.Text;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = bodytxt.Text;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
if (FileUpload1.HasFile)
{
// string PhysicalPath = "C:\\Documents and Settings\\Administrator\\Desktop\\";
string fileName = FileUpload1.PostedFile.FileName;
FileUpload1.PostedFile.SaveAs(fileName);
Attachment data = new Attachment( fileName);
mail.Attachments.Add(data);
}
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(Textfrom.Text, "9966794757");
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layers
restxt.Text = "Send Mail";
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
HttpContext.Current.Response.Write(errorMessage);
}
But i got this error
CrishPosted May 21, 2010, 9:35 AM
You need to pass username and password for gmail smtp.
try below links
http://www.projectpier.org/node/817
http://www.codeproject.com/KB/aspnet/smtpmailByNavaneeth.K.N.aspx