尝试提交基本表单时出现SMTP问题
本文关键字:SMTP 问题 表单 提交 | 更新日期: 2023-09-27 18:27:07
我很抱歉收到这么多代码,但我的表单有问题。我已经尝试了各种方法来配置它,但根本无法发送。它所需要做的就是发送一封带有消息正文的电子邮件,但每次我点击提交时,它只会显示错误(来自catch块)。
这是在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("info@kellersflowers.com");
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("info@kellersflowers.com", "PasswordForEmail");
希望能有所帮助!祝你好运