Using ASP.NET Core Identity, I'm getting an error trying to use SendGrid for email. I saw another thread about "User" validation requiring an email for the SMTP user. I changed the user to apikey (per SendGrid Requirements) in appsetting.Development.json but now I get
"The specified string is not in the form required for an e-mail address." Is sendgrid api mail not supported at all?
Solution:
- Install the SendGrid Nuget Package (current latest is SendGrid 9.28.1)
- Get a Sendgrid API Key (Sendgrid docs here [Manage SendGrid API Keys | Twilio]
- Edit appsettings.json or appsettings.Development.json replacing [Smpt:password] with your API key. Note: username doesn't matter for Sendgrid they only care about the API Key so you can refactor your appsettings to match.
4.Open AccountController.cs - Replace SendEmailAsync(string to, string subjThis text will be hiddenect, string body) with
private async Task SendEmailAsync(string to, striThis text will be hiddenng subject, string body)
{
var apiKey = configuration.GetValue<string>("Smtp:Password");'
var client = new SendGridClient(apiKey);
var from = new EmailAddress("email@somewhere.com", "Sender Name");
var plainTextContent = body;
var htmlContent = body;
var msg = MailHelper.CreateSingleEmail(from, new EmailAddress(to), subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
}
I hope this helps someone.