0

I want to send an e-mail, but it shows the following error:

try
{
     string filename = Server.MapPath("~/App_Data/FeedBack.txt");
     string mailbody = System.IO.File.ReadAllText(filename);
     mailbody = mailbody.Replace("##Name##", txtName.Text);
     mailbody = mailbody.Replace("##Email##",txtEmail.Text);
     mailbody = mailbody.Replace("##contact##",txtContact.Text);
     mailbody = mailbody.Replace("##Commnect##",txtSuggestion.Text);
     MailMessage myMessage = new MailMessage();
     myMessage.Subject = "Responce from website";
     myMessage.Body = mailbody;
     myMessage.From = new MailAddress("amitverma0511@gmail.com","Amit Kumar");
     myMessage.To.Add(new MailAddress("amitverma1150@gmail.com", "Amit Verma"));
     SmtpClient mysmptclient = new SmtpClient("localhost");
     mysmptclient.Host = "smtp.gmail.com";
     mysmptclient.Port = 587;
     mysmptclient.Credentials = new System.Net.NetworkCredential("amitverma0511@gmail.com", "Myemailpassword");

     mysmptclient.Send(myMessage);
     formatTable.Visible = false;
     lblMail.Text = "Your Feedback has been sent successfully";
     lblMail.ForeColor = Color.Green;
}
catch(Exception expt)
{
     lblMail.Text = "unable to sent the feedback .Check your smpt server"+expt.Message;
     lblMail.ForeColor = Color.Red;
}

My web.config file looks like this:

<system.net>
  <mailSettings>
    <smtp from="amitverma0511@gmail.com">
      <network host="smtp.gmail.com"  port='587' password="myemailpass" userName="amitverma0511@gmail.com"/>
    </smtp>
  </mailSettings>
</system.net>
Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
Amit Kumar
  • 5,888
  • 11
  • 47
  • 85

1 Answers1

0

As far as I know, you are overwriting your web.config settings by using localhost (your IIS) as a SMTP Server. If you have not configured your IIS to serve as a SMTP server this naturally will not work. But, if you want to use localhost and not the google servers just use this 3 lines:

SmtpClient MySmtpClient = new SmtpClient();
MySmtpClient.UseDefaultCredentials = true;
MySmtpClient.Send(mailMessage);

You should remove the string localhost from SmtpClient mysmptclient = new SmtpClient("localhost"); and it should work, if all your other settings are correct.

Further information can be found here.

Community
  • 1
  • 1
Marco
  • 22,856
  • 9
  • 75
  • 124