Click here to Skip to main content
16,009,391 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hey fellas!

I have the following code for a contact form that is sent to my email address

C#
[HttpPost]
        public ActionResult Contact(ContactsModel model)
        {
            if (ModelState.IsValid)
            {
                InsertContact(model.Name, model.Phone, model.Email, model.Comments);

                MailMessage message = new MailMessage();
                message.From = new MailAddress("sender1@foo.bar.com");
                message.To.Add(new MailAddress("lotusms@outlook.com"));
                message.Subject = "This is my subject";
                message.Body = "This is the content";
                SmtpClient client = new SmtpClient();
                client.Send(message);

                TempData["notice"] = "Someone will reply as soon as possible.";
                return RedirectToAction("Index", "Home");
            }

            return View();
            
        }


The good news is that it works. The bad news are:

1. It goes to the junk folder. Is there a way to get around this?
2. It sends an email from sender1@foo.bar.com to me with a "This is my content" instead of the actual content in the textarea.

How can I make it so that the message.From actually sends the email address entered in the email textbox, and the message.Body to send the content of the TextArea.

Here is my form as well.

C#
<div id="contact-form">
            @using(Html.BeginForm()) 
            {
                @Html.LabelFor(model => model.Name)
                @Html.TextBoxFor(model => model.Name)<br class="clear" /> 
                @Html.ValidationMessageFor(model => model.Name)<br class="clear" /> <br />
    
                @Html.LabelFor(model => model.Phone)
                @Html.TextBoxFor(model => model.Phone)<br class="clear" />    
                @Html.ValidationMessageFor(model => model.Phone)<br class="clear" /> <br />
    
                @Html.LabelFor(model => model.Email)
                @Html.TextBoxFor(model => model.Email)<br class="clear" /> 
                @Html.ValidationMessageFor(model => model.Email)<br class="clear" /> <br />
    
                @Html.LabelFor(model => model.Comments)
                @Html.TextAreaFor(model => model.Comments, 10, 40, null)<br class="clear" /> 
                @Html.ValidationMessageFor(model => model.Comments)<br class="clear" /> <br />
    
        
                <button type="submit">SEND</button> 
                <button type="reset">CLEAR</button> 
            }    
        </div>
Thanks
Posted

Do this to specify the correct destination address:
C#
message.To.Add(new MailAddress(model.Email));

/ravi
 
Share this answer
 
Email is going to junk folder as your not providing STMP host name and genuine email address registered with SMTP server. Host name is required to authenticate email from where they are coming.
 
Share this answer
 
v2
Hello,

You are hard-coding the values. Try changing the code as shown below. I think you must have missed it, because while inserting a record in database you are using correct values.
C#
[HttpPost]
public ActionResult Contact(ContactsModel model)
{
    if (ModelState.IsValid)
    {
        InsertContact(model.Name, model.Phone, model.Email, model.Comments);
 
        MailMessage message = new MailMessage();
        message.From = new MailAddress(model.Email);
        message.To.Add(new MailAddress("lotusms@outlook.com"));
        message.Subject = "This is my subject";
        message.Body = model.Comments;
        SmtpClient client = new SmtpClient();
        client.Send(message);
 
        TempData["notice"] = "Someone will reply as soon as possible.";
        return RedirectToAction("Index", "Home");
    }
    return View();
}

Regards,
 
Share this answer
 
Comments
[no name] 22-May-13 10:48am    
Thanks, I will try this. One more question. How do I concatenate model.Name, model.Phone, model.Email, and model.Comments within the message.Body so that I can retrieve the name and phone number submitted by the sender? I tried commas and semicolons but that was wrong
Prasad Khandekar 22-May-13 10:56am    
Use String.concat method.

string args = {"Name : ", model.Name, "\nPhone : ", model.Phone, "\nComments : ", model.Comments};
string strBody = String.Concat(args);

Regards,
[no name] 22-May-13 11:00am    
Prasad,

thank you. however I received the following error:

'string' does not contain a definition for 'concat '
Prasad Khandekar 22-May-13 11:03am    
Sorry my mistake. Change concat to Concat.
[no name] 22-May-13 11:09am    
Now I have a different error
Member 'string.Concat(string, string)' cannot be accessed with an instance reference; qualify it with a type name instead
1. It goes to the junk folder likely because you hardcoded the sender to be "sender1@foo.bar.com"
2. See #1. You just need to pass through the right variables.
 
Share this answer
 
Comments
[no name] 22-May-13 10:37am    
No kidding! I know that. That's what I am asking. How do I make it so that it sends the right info. I am new at ASP. I am not familiar with ASP syntax. In my head I want to pass model => model.Email in place of sender1@foo.bar.com but something tells me that is wrong.
ZurdoDev 22-May-13 10:39am    
Sorry. If you knew that then why did you ask it that way? Anyway, you have code that says InsertContact(model.Name, model.Phone, model.Email, model.Comments); Does that work? If so, there is your code. Did you even try model.Email?
[no name] 22-May-13 10:41am    
I am following tutorials. There is a lot I don't understand clearly. When I took ASP in college, MVC wasn't out yet. And I never coded in Windows after that. I stuck to Linux
ZurdoDev 22-May-13 10:46am    
No problem. However, does model.Email have what you need?
[no name] 22-May-13 10:52am    
I'm going to publish it all to the server in a few. Unfortunately, GoDaddy has a long process to publish changes. But I think it will work as soon as I figure out how to concatenate the name, phone, and email in the comment body

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900