尝试提交基本表单时出现SMTP问题

时间:2012-03-08 19:46:20

标签: c# asp.net smtp submit

我很抱歉这么多代码,但是这个表格有问题。我已经尝试了每种方式来配置它,但我根本无法让它发送。它所需要做的就是发送一封包含邮件正文的电子邮件,但每次我点击提交时都会说错误(来自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/>";
    }
}

我是这个网站的新手,所以如果我需要发布更多信息,我会。谢谢!

1 个答案:

答案 0 :(得分:0)

很可能,您需要为SMTP对象设置一些凭据。

smtp.Credentials = new System.Net.NetworkCredential("info@kellersflowers.com", "PasswordForEmail");

希望有所帮助!祝你好运!