尝试提交基本表单时出现SMTP问题
问题描述:
对于如此多的代码,我很抱歉,但是我遇到了此表单的问题。我已经尝试过每种方式来配置它,但我根本无法发送它。它所需要做的就是发送一封包含邮件正文的电子邮件,但每次点击提交时都会提示错误(来自catch块)。尝试提交基本表单时出现SMTP问题
这是在C#ASP.NET中。
我是新来的发展,并希望任何帮助。提前致谢!
protected void submitButton_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string messageBody = "//numerous textboxes, dropdown lists, and checkboxes";
SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage();
message.IsBodyHtml = false;
try
{
// Prepare to and from e-mail addresses
MailAddress fromAddress = new MailAddress("[email protected]");
MailAddress toAddress = new MailAddress(emailTextBox.Text);
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = "Contact Form Email";
message.Body = messageBody;
// Server details
smtp.Host = "email.goidp.com";
// Credentials
smtp.UseDefaultCredentials = true;
// Send the email
smtp.Send(message);
// Inform the user
noticeLabel.Visible = true;
noticeLabel.Attributes.CssStyle.Add("display", "block");
//noticeLabel.Text = "E-mail sent";
noticeLabel.Text = "Thank you, we have received your information and will be in touch soon.";
// and clear the form
fullNameTextBox.Text = String.Empty;
companyTextBox.Text = String.Empty;
//many others, this is just to give the idea;
}
catch (Exception ex)
{
errorLabel.Text = "Error sending e-mail:<br/>\n" + ex.Message;
Response.Redirect("estimate-error.html?error=1");
errorLabel.Visible = true;
errorLabel.Attributes.CssStyle.Add("display", "block");
errorLabel.Text = "Error sending e-mail:<br/>\n" + ex.Message;
}
}
else
{
//errorLabel.Text = "Error sending e-mail. Check required fields<br/>";
//Response.Redirect("estimate-error.html?error=2");
errorLabel.Visible = true;
errorLabel.Attributes.CssStyle.Add("display", "block");
errorLabel.Text = "Error sending e-mail. Check required fields<br/>";
}
}
我是这个网站的新手,所以如果我需要发布更多的信息,我会的。谢谢!
答
更可能的是,你将需要设置一些凭据SMTP对象。
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "PasswordForEmail");
希望有所帮助!祝你好运!
你得到了什么错误? – alf 2012-03-08 19:48:34
这只是来自catch块的通用错误。它只是说:“发送电子邮件时出错:发送请求失败” – Peter 2012-03-08 19:54:53
您可以在catch块的第一行设置断点并检查异常。它应该包含详细信息,或者可能是一个内部异常 – alf 2012-03-08 19:57:32