-3

I am trying to implement a functionality for sending mails when a user register my page, also to reset passwords. I have been researching but I could not get good resources. Everything I found is based in old examples , using sendgrid v2.0 (a very old version). Anyone who has developed this functionality in the last year and has some guide? I have researched guides that are like this: https://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112

2 Answers2

1

There's two parts to this, and you haven't been clear enough about where you're having issues. Essentially, to send an email you need:

  1. An SMTP server
  2. The code to create the email and utilize the SMTP server to send it

You mentioned sendgrid, which is just an SMTP server (or really a grid of SMTP servers, hence the name); it's just a way to actually send the email. There's other options of course. You could enable the builtin SMTP server in Windows Server, or you could utilize Exchange, if you're running in a corporate environment. You could even use something like Gmail's SMTP server, though if you use that for anything more than testing or local development, you're likely to get banned.

As far as the actual code to create the email, for that you have SmtpClient, though I wouldn't recommend using it directly in an MVC application. It's better to use something like Postal, which gives you a higher-level API and utilizes Razor views to craft your emails, amongst other things. Under the hood, it utilizes SmtpClient, as well.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
0

I am not sure if this will help you but I have recently implemented the following in a MVC site I developed used to send approval request emails.

From the controller action I call the following method:

 public static void SendMail(MailMessage Msg)
    {
        SmtpClient _SMTP = new SmtpClient(SMTP, SMTP_Port);
        _SMTP.UseDefaultCredentials = false;
        _SMTP.Credentials = new System.Net.NetworkCredential(User, Password);
        _SMTP.Send(Msg);
    }

And the Mailmessage is created using the following method:

 public static MailMessage BuildEnqNotificationMessage(string _To, string _Body)
    {
        MailMessage Msg = new MailMessage();
        Msg.From = new MailAddress(FromAddress, FromFriendly);
        Msg.Subject = "New Sales Enquiry";
        Msg.To.Add(_To);
        Msg.Body = _Body;
        Msg.IsBodyHtml = true;
        return Msg;
    }

This implements in exactly the same way you would implement it in webforms.

ThatChris
  • 752
  • 1
  • 4
  • 18