Send email to register form

Asked

Viewed 210 times

1

Personal I need a help from you, I have a form on my system, I need 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 someone help me?

  • Creates an Emailservice class and encapsulates the methods you want to use for sending. At the end of your routine you would call, if successful, "Emailservice.mailEmailCadastro();". Shipping details you can find (here)[https://answall.com/questions/630/comorusposso-enviar-um-e-mail-pelo-gmail] and (here)[https://answall.com/questions/84239/enviare-mail-usando-asp-net-mvc]

  • http://netcoders.com.br/aplicando-solid-com-c-srp/

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 lib System.Net.Mail.MailMessage

  • 1

    Very good, simple and direct your answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.