如何使用smtp通过本地主机发送电子邮件? asp.net

时间:2014-04-17 19:34:45

标签: c# asp.net email smtp localhost

我已经为网络应用实施了联系表单,但是当我尝试发送电子邮件时,我收到了JavaScript运行时错误,line 37//calls the SendMail() and resets the textboxes protected void sendBtn_Click(object sender, EventArgs e) { try { //here on button click what will done SendMail(); confirmationLbl.Text = "Your email has been sent to customer support."; confirmationLbl.Visible = true; subjectTbx.Text = ""; emailTbx.Text = ""; nameTbx.Text = ""; questionTbx.Text = ""; } catch (Exception ex) { confirmationLbl.Text = "Your email has failed to send,please check your connection."; Console.WriteLine("IOException source: {0}", ex.Message ); } //line 37 is here, which is blank } //method to compose email from textboxes protected void SendMail() { // Gmail Address from where you send the mail var fromAddress = emailTbx.Text.ToString(); // any address where the email will be sending var toAddress = "brianDoe@gmail.com"; //Password of your gmail address const string fromPassword = "Password"; // Passing the values and make a email formate to display string subject = subjectTbx.Text.ToString(); string body = "From: " + nameTbx.Text + "\n"; body += "Email: " + emailTbx.Text + "\n"; body += "Subject: " + subjectTbx.Text + "\n"; body += "Question: \n" + questionTbx.Text + "\n"; // smtp settings var smtp = new System.Net.Mail.SmtpClient(); { smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); smtp.Timeout = 20000; } // Passing values to smtp object smtp.Send(fromAddress, toAddress, subject, body); } 这是一个空行。

有没有人知道我的实施有什么问题,还是我必须更改一些设置才能在本地主机上测试它?

{{1}}

1 个答案:

答案 0 :(得分:1)

您应该检查解析的HTML是否存在JavaScript错误,而不是C#代码。此外,请确保在设置下在Gmail中启用POP / IMAP访问。

enter image description here