简体   繁体   中英

how to send an email in asp.net mvc4

i want to send an email using a asp.net mvc 4 application i am using this method

     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);


    }

My model :

            public class AccuseReception
{


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


}

controller :

        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);
         }

i get no error but the mail is not sending and the ModelState is not valid

ModelState belongs to a model that is passed to corresponding view/controller.

I see that in your controller you create a new AccuseReception so i assume that you are not getting AccuseReception as a model in this controller action.

This is the reason your ModelState.IsValid returns false and no mails are sent.

Try something like:

 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);
        }    
   }

Here's a vb.net example that should work for you. I can't see your model in the example. But that should be enough:

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    

BTW if you want to easily send more complex html as emails, you can use Postal. In Postal you build views that are sent as emails:

aboutcode.net/postal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM