saidmohamed11
saidmohamed11

Reputation: 275

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="[email protected]",
            To ="[email protected]",
            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

Upvotes: 2

Views: 8804

Answers (2)

Alex Art.
Alex Art.

Reputation: 8781

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

Upvotes: 3

ilans
ilans

Reputation: 2697

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("[email protected]")

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

Upvotes: 1

Related Questions