未在MVC中验证或发送电子邮件

时间:2013-12-09 08:32:55

标签: validation email asp.net-mvc-4 gmail

我试图从我的MVC 4网站发送电子邮件并验证但没有发生任何事情这是控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;


namespace DonsSolution.Controllers
{
public class SendMailerController : Controller
{
    //
    // GET: /SendMailer/

public ActionResult Index()
{
        return View();
 }
public static bool SendGmail(string subject, string content, string[] recipients,string         ........from)
    {
        bool success = recipients != null && recipients.Length > 0;

        if (success)
        {
            SmtpClient gmailClient = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("youremail@gmail.com","password")

            };


            using (MailMessage gMessage = new MailMessage(from, recipients[0], subject,     content))
            {
                for (int i = 1; i < recipients.Length; i++)
                    gMessage.To.Add(recipients[i]);

                try
                {
                    gmailClient.Send(gMessage);
                    success = true;
                }
                catch (Exception) { success = false; }
            }
        }
        return success;
    }
}

}

这是模型

public class MailModel
    {
      [Required(ErrorMessage = "This is required")]
      public string From { get; set; }

    [Required(ErrorMessage = "This is required")]
    public string Subject { get; set; }
    [Required(ErrorMessage = "This is required")]
    public string Body { get; set; }

   }

它正在联系页面下的部分视图中传递

@model DonsSolution.Models.MailModel

<h2>Ask the Coach a question</h2>
<fieldset>
<legend>
Ask me a question?
</legend>
@using (Html.BeginForm(FormMethod.Post))
{
    @Html.ValidationSummary()

    <p>@Html.Label("From")
        @Html.TextBoxFor(m => m.From)</p>

    <p>@Html.Label("Subject") </p>
    <p>@Html.TextBoxFor(m => m.Subject)</p>
    <p>@Html.Label("Body")</p>
    <p>@Html.TextAreaFor(m => m.Body)</p>
    <input type="submit" value="Send" />
}

我对编码很新。我真的需要你的帮助。我尝试了很多不同的选择,没有任何作用。请帮忙。

0 个答案:

没有答案