2

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

3
  • Your example is not showing a use of your model... this is quite basic. 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 Commented Sep 7, 2014 at 11:59
  • this how i use my model AccuseReception my_mail = new AccuseReception { From="[email protected]", To =candidat.adresse_mail, Subject ="mail_de confermation", Body = "votre demande est enregistrer , votre code d'identification pour modifier vos informations est ..Merci", }; Commented Sep 7, 2014 at 12:17
  • Sauce: c-sharpcorner.com/uploadfile/sourabh_mishra1/… Commented Dec 9, 2016 at 9:49

2 Answers 2

3

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);
        }    
   }
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.