2

I want to send email using an MVC 3 application. I have a user table named Reviewer in a database with email addresses. What I want is when I select 2-3 users from the table on the view using checkboxes, the email address for each user should be inserted automatically into the "Textbox" on the feedback view page, but it's not working. It's taking the value of Webmail.Send() from the controller instead of the feedback form page. Any ideas?

 [HttpPost]
    public ActionResult Feedback(string email, string subject, string body)
    {
        try
        {
            WebMail.SmtpServer = "smtp.gmail.com";
            WebMail.EnableSsl = true;
            WebMail.SmtpPort = 25;
            WebMail.UserName = "[email protected]";
            WebMail.Password = "*******123";
            WebMail.From = "[email protected]";
            WebMail.Send(
                    "[email protected]",
                    subject,
                    body,
                    email 
                );

            return RedirectToAction("FeedbackSent");
        }
        catch (Exception ex)
        {
            ViewData.ModelState.AddModelError("_FORM", ex.ToString());
        }

        return View();
    }

This is the View page of Feedback:

@using(Html.BeginForm()) {
    <table>
    <tr>
        <td>Your e-mail:</td>
        <td>@Html.TextBox("email")</td>
    </tr>
    <tr>
        <td>Subject:</td>
        <td>@Html.TextBox("subject")</td>
    </tr>
    <tr>
        <td>Body:</td>
        <td>@Html.TextArea("body")</td>
    </tr>
    </table>
    <input type="submit" value="Send" />
}
3
  • What do you mean when you say that it's taking the value from the controller? That the "from" email is [email protected] instead of what was entered in the form? Commented Apr 1, 2012 at 13:52
  • i mean when i send email it goes from "[email protected]" to "[email protected]". But i want to send email from "[email protected]" to the person(s) that i select from the check box list on the feedback page. Commented Apr 1, 2012 at 14:06
  • I can't find where are you selecting the destination emails... your controller just have one email as parameter and the others are harcoded. Commented Apr 1, 2012 at 18:20

1 Answer 1

2

You should use this:

WebMail.Send(
    email,
    subject,
    body
);

Or you could eliminate the line:

WebMail.From = "[email protected]";

And use:

WebMail.Send(
    email,
    subject,
    body,
    from: "[email protected]"
);

More on the WebMail class and what the arguments to WebMail.Send are can be found here: WebMail Class on MSDN.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.