【发布时间】:2014-06-02 05:49:14
【问题描述】:
我已经为 Web 应用程序实现了一个联系表单,但是当我尝试发送电子邮件时,我收到 JavaScript 运行时错误,http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp 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);
}
【问题讨论】:
-
您的电子邮件地址在其中,即所有人都可以在此处看到。
-
您的 C# 代码中出现 JavaScript 错误?非常奇怪(提示:尝试查看 JavaScript 中的第 37 行...也尝试发布错误;这对调试过程非常重要)。
-
我不认为他的姓氏是 Doe
-
你是对的,它不是......因为他改变了它 - 与继续讨论无关。
标签: c# asp.net email smtp localhost