0

I am creating a contact form for customers to type info in the form and submit an email to us. Also, my hosting package is under Go Daddy. I used the following code as a test and it worked.

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);

Then I changed to code to get the info inputted from the form to be emailed, and not it's giving me an error.

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();
    }

Its giving me this error

COMException (0x8004020d): At least one of the From or Sender fields is required, and neither was found

tereško
  • 58,060
  • 25
  • 98
  • 150
Lars Hovden
  • 317
  • 2
  • 25

2 Answers2

1

looks like you are not supplying the email-from field:

oMail.From = "someEmail@someDomain.com"; //instead of -> "Website Contact Form"
Bassam Mehanni
  • 14,796
  • 2
  • 33
  • 41
  • That's correct. Except I physically need to type in the address. oMail.From = someone@domail.com – Lars Hovden Jan 31 '12 at 19:16
  • @LarsHovden fixed.. I just assumed your model has it, by the way, I recommend that you store that email in the database or in your web.config instead of directly hard-coding it. – Bassam Mehanni Jan 31 '12 at 19:36
0

The "From" field you specified isn't a valid email address. To and from need to be in email format, even if they aren't live addresses that can send or receive mail.

Brian
  • 2,229
  • 17
  • 24