我是 asp.net mvc Identity 和 SendGrid 的新手,但我真的很想使用它们的功能。
我想让用户使用身份注册表格进行注册,然后使用 SendGrid v3 发送一个模板(内置在我的 SendGrid 帐户中)作为帐户注册确认电子邮件。我创建了一个 Transactional 模板并拥有一个 Api Key。
我已在身份中启用电子邮件确认:
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
然后,我在 web.config 的应用程序设置中设置了我的 sendGrid apiKey 和帐户凭据,以便我可以在我的代码中使用它们。
<appSettings>
<add key="SendGridUsername" value="xxxxxxx" />
<add key="SendGridPassword" value="xxxxxxx" />
<add key="SendGridApiKey" value="xxxxxxxxxxxxxxxxxxxxxxxx" />
</appSettings>
我已将其添加到 IdentityConfig.cs 中的 EmailService 中,但我不知道从哪里开始:
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var apiKey = WebConfigurationManager.AppSettings["SendGridApiKey"];
var client = new SendGridClient(apiKey);
var from = new EmailAddress("me@us.com", "Me");
var subject = message.Subject;
var to = new EmailAddress(message.Destination);
var email = MailHelper.CreateSingleEmail(from, to, subject, "", message.Body);
await client.SendEmailAsync(email);
}
}
我还阅读了以下内容,但不明白在哪里实现它:
https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/smtpapi.html
{
"filters": {
"templates": {
"settings": {
"enable": 1,
"template_id": "5997fcf6-2b9f-484d-acd5-7e9a99f0dc1f"
}
}
}
}
对此的任何帮助都会很棒,因为我只是不确定从这里去哪里。
谢谢