ASP.NET MVC电子邮件联系表单

时间:2012-01-31 18:55:47

标签: asp.net-mvc asp.net-mvc-2 asp.net-mail

我正在创建一个联系表单,供客户在表单中输入信息并向我们提交电子邮件。此外,我的托管包在Go Daddy下。我使用以下代码作为测试并且它有效。

MailMessage oMail = new System.Web.Mail.MailMessage();

      oMail.Subject = "Subject",
      oMail.Body 
      oMail.From = "myFromAddress@domain.com",
      oMail.To = "myToAddress@someotherdomain.com",

SmtpMail.SmtpServer = "relay-hosting.secureserver.net";
SmtpMail.Send(oMail);

然后我更改为代码以从表单输入信息以通过电子邮件发送,而不是它给我一个错误。

public ActionResult ContactForm(ContactModel emailModel)
    {
        MailMessage oMail = new System.Web.Mail.MailMessage();

            oMail.From = "Website Contact Form";
            oMail.To = "myemail@hmydomain.com";
            oMail.Subject = emailModel.Subject;
            string body = "Name: " + emailModel.Name + "\n"
                        + "Email: " + emailModel.Email + "\n"
                        + "Website: " + emailModel.Website + "\n"
                        + "Phone: " + emailModel.Phone + "\n\n"
                        + emailModel.Message;

            oMail.Body = body;

    SmtpMail.SmtpServer = "relay-hosting.secureserver.net";
    SmtpMail.Send(oMail);
        return View();
    }

它给我这个错误

COMException(0x8004020d):至少需要一个From或Sender字段,但都没有找到

2 个答案:

答案 0 :(得分:1)

看起来您没有提供来自电子邮件的字段:

oMail.From = "someEmail@someDomain.com"; //instead of -> "Website Contact Form"

答案 1 :(得分:0)

您指定的“发件人”字段不是有效的电子邮件地址。往返需要采用电子邮件格式,即使它们不是可以发送或接收邮件的实时地址。