如何在asp.net mvc4中发送电子邮件

时间:2014-09-07 11:29:23

标签: c# asp.net asp.net-mvc asp.net-mvc-4 email

我想使用asp.net mvc 4应用程序发送电子邮件我正在使用此方法

     public void send_mail(ProcRec.Models.AccuseReception _objModelMail)
    {

        MailMessage mail = new MailMessage();
        mail.To.Add(_objModelMail.To);
        mail.From = new MailAddress(_objModelMail.From);
        mail.Subject = _objModelMail.Subject;
        string Body = _objModelMail.Body;
        mail.Body = Body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential
        ("username", "password");// Enter seders User name and password
        smtp.EnableSsl = true;
        smtp.Send(mail);


    }

我的模特:

            public class AccuseReception
{


    public string From { get; set; }
    public string To { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }


}

控制器:

        if (ModelState.IsValid)
        {

           AccuseReception confermation_mail = new AccuseReception {

            From="xxxxxxxxxxx@gmail.com",
            To ="ssssssss@gmail.com",
            Subject ="mail_de confermation",
            Body = "votre demande est enregistrer ",

            };

        send_mail(my_mail);
         }

我没有收到任何错误但邮件没有发送且ModelState无效

2 个答案:

答案 0 :(得分:3)

ModelState属于传递给相应视图/控制器的模型。

我在你的控制器中看到你创建了一个新的AccuseReception所以我假设你没有将AccuseReception作为这个控制器动作中的模型。

这就是您ModelState.IsValid返回false并且没有发送邮件的原因。

尝试类似:

 public void SendMail(ProcRec.Models.AccuseReception _objModelMail)
 {
       if(ModelState.IsValid)
       {

           MailMessage mail = new MailMessage();
           mail.To.Add(_objModelMail.To);
           mail.From = new MailAddress(_objModelMail.From);
           mail.Subject = _objModelMail.Subject;
           string Body = _objModelMail.Body;
           mail.Body = Body;
           mail.IsBodyHtml = true;
           SmtpClient smtp = new SmtpClient();
           smtp.Host = "smtp.gmail.com";
           smtp.Port = 587;
           smtp.UseDefaultCredentials = false;
           smtp.Credentials = new System.Net.NetworkCredential
           ("username", "password");// Enter seders User name and password
           smtp.EnableSsl = true;
           smtp.Send(mail);
        }    
   }

答案 1 :(得分:0)

这是一个应该适合您的vb.net示例。我在示例中看不到您的模型。但这应该足够了:

Dim smtpClient As New SmtpClient(_ServerHost, _ServerPort)
smtpClient.Credentials = New Net.NetworkCredential(_EmailAddress, _EmailPass, String.Empty)
smtpClient.EnableSsl = True
smtpClient.Timeout = 60000

Dim mailmsg as New MailMessage
mailmsg.Subject = i_Subject
mailmsg.From = New MailAddress(emailAddress, displayName)
mailmsg.To.Clear()
mailmsg.IsBodyHtml = True
mailmsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

' Add email addresses
mailmsg.To.Add("someemail@gmail.com")

mailmsg.Body = SomeBodyHTML

If Not String.IsNullOrEmpty(mailmsg.Body.Trim) Then
    ' Send message
    smtpClient.Send(mailmsg)
End If    

顺便说一句,如果你想轻松发送更复杂的HTML作为电子邮件,你可以使用Postal。在邮政中,您可以构建以电子邮件形式发送的视图:

aboutcode.net/postal