0

我想发送一封电子邮件,但它显示以下错误:

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;
}

我的 web.config 文件如下所示:

<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>
4

1 回答 1

0

据我所知,您正在使用 localhost(您的 IIS)作为 SMTP 服务器来覆盖您的 web.config 设置。如果您没有将 IIS 配置为 SMTP 服务器,这自然不会起作用。但是,如果您想使用 localhost 而不是 google 服务器,只需使用以下 3 行:

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

如果您的所有其他设置都正确,您应该localhost从中删除字符串并且它应该可以工作。SmtpClient mysmptclient = new SmtpClient("localhost");

更多信息可以在这里找到。

于 2013-10-01T06:07:30.147 回答