Send email to register form

1

I need a help from you guys, I have a form on my system, I need it when I register it, send an email to a user.

When I click save, I enter this post to save the data.

[HttpPost]
public void EventosAdversos(EventosAdversos obj)
{
    var hospitalId = int.Parse(Cookies.GetCookie("hid"));
    var usuarioId = int.Parse(Cookies.GetCookie("uid"));

    obj.HospitalId = hospitalId;
    obj.StatPree = 1;
    obj.UsuarioId = usuarioId;

    if (obj.EaId == 0)
    {
        var eaId = _eventosAdversosService.NovoEaId(obj.PatientId, hospitalId);
        obj.EaId = eaId;
    }

    _eventosAdversosService.Add(obj);
}

Could anyone help me?

    
asked by anonymous 17.11.2017 / 12:43

1 answer

1

Create a class to send email and call in your method there.

Follow the example:

using System.Net;
using System.Net.Mail;
using System.Net.Mime;

...
try
{

   SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");

    // set smtp-client with basicAuthentication
    mySmtpClient.UseDefaultCredentials = false;
   System.Net.NetworkCredential basicAuthenticationInfo = new
      System.Net.NetworkCredential("username", "password");
   mySmtpClient.Credentials = basicAuthenticationInfo;

   // add from,to mailaddresses
   MailAddress from = new MailAddress("[email protected]", "TestFromName");
   MailAddress to = new MailAddress("[email protected]", "TestToName");
   MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

   // add ReplyTo
   MailAddress replyto = new MailAddress("[email protected]");
   myMail.ReplyToList.Add(replyTo);

   // set subject and encoding
   myMail.Subject = "Test message";
   myMail.SubjectEncoding = System.Text.Encoding.UTF8;

   // set body-message and encoding
   myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
   myMail.BodyEncoding = System.Text.Encoding.UTF8;
   // text or html
   myMail.IsBodyHtml = true;

   mySmtpClient.Send(myMail);
}

catch (SmtpException ex)
{
  throw new ApplicationException
    ("SmtpException has occured: " + ex.Message);
}
catch (Exception ex)
{
   throw ex;
}

Note: you will need to use the System.Net.Mail.MailMessage lib

    
17.11.2017 / 14:28