asp.net mvc将表单从站点发送到电子邮件

时间:2013-05-20 11:11:50

标签: asp.net-mvc asp.net-mvc-4

我的mvc网站上有反馈表,看起来像是

enter image description here

我为我的表单创建了模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ComponentTrading.Web.Models
{
public class FeedbackForm
    {
    public string Name { get; set; } 
    public string Email { get; set; } 
    public string Phone { get; set; }
    public string Message { get; set; }
    }
}

我为表单创建了视图

 @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "contact-form" }))
                    {
                    <fieldset>
                    @Html.TextBoxFor(model => model.Name, new { @Value = "Name" })
                    @Html.TextBoxFor(model => model.Email, new { @Value = "E-mail" })
                    @Html.TextBoxFor(model => model.Phone, new { @Value = "Phone" })
                    @Html.TextAreaFor(model => model.Message, new { @class = "img-shadow"})
                    <input class="form-button" data-type="reset" value="Clear" />
                    <input class="form-button" data-type="submit" type="submit" value="Send" />


                        }

现在我尝试向电子邮件发送一封信,但是当我推送&#34;发送按钮&#34;它什么都没发生,我没有收到电子邮件

我的控制器

    [HttpGet]
    public ActionResult Contacts()
    {
        FeedbackForm temp = new FeedbackForm();
        temp.Message = "Message";
        return View(temp);
    }


    [HttpPost]
    public ActionResult Contacts(FeedbackForm Model)
    {
        string Text = "<html> <head> </head>" +
        " <body style= \" font-size:12px; font-family: Arial\">"+
        Model.Message+
        "</body></html>";

        SendEmail("mironny@inbox.ru", Text);
        FeedbackForm temp = new FeedbackForm();
        return View(temp);
    }


    public static bool SendEmail(string SentTo, string Text)
    {
        MailMessage msg = new MailMessage();

        msg.From = new MailAddress("Test@mail.ru");
        msg.To.Add(SentTo);
        msg.Subject = "Password";
        msg.Body = Text;
        msg.Priority = MailPriority.High;
        msg.IsBodyHtml = true;

        SmtpClient client = new SmtpClient("smtp.mail.ru", 25);



        client.UseDefaultCredentials = false;
        client.EnableSsl = false;
        client.Credentials = new NetworkCredential("TestLogin", "TestPassword");
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        //client.EnableSsl = true;

        try
        {
            client.Send(msg);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

出了什么问题?

1 个答案:

答案 0 :(得分:1)

使用

// if you dont pass any parameter
// BeginForm posted to the action that
// has name as view name.
// so no need to write any parameters
@using (Html.BeginForm())
{
    ...

    <input type="submit" value="Send">
}

OR

@using (Html.BeginForm("Contacts", "SomeController", FormMethod.Post, new { id = "contact-form" }))
{
    ...

    <input type="submit" value="Send">
}

你对mvc有一些疑问,其中更重要的是不正确的重载方法。我的建议,首先,你应该学习html-helpers重载方法。和MVC模型绑定策略......

相关问题