[英]SMTP issue when trying to submit a basic form
I am sorry for so much code, but I am having an issue with this form. 对于这么多代码,我感到抱歉,但是此表单存在问题。 I have tried every which way to configure it but I simply cannot get it to send. 我尝试了每种方式来配置它,但是我根本无法发送它。 All it needs to do is send an email with the message body, but every time I hit submit it just says error (which comes from the catch block). 它所要做的就是发送一封带有消息正文的电子邮件,但是每次我单击“提交”时,它只会显示错误消息(来自catch块)。
This is in C# ASP.NET. 这是在C#ASP.NET中。
I am new to development and would appreciate any help. 我是开发的新手,将不胜感激。 Thanks in advance! 提前致谢!
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/>";
}
}
I am new to this site, so if I need to post more info I will. 我是这个网站的新手,所以如果我需要发布更多信息,我会。 Thank you! 谢谢!
More than likely, you're going to need to set some credentials for the SMTP object. 您很有可能需要为SMTP对象设置一些凭据。
smtp.Credentials = new System.Net.NetworkCredential("info@kellersflowers.com", "PasswordForEmail");
Hope that helps! 希望有帮助! Good Luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.