[英]Sendgrid does not send confirmation email asp.net core 3.1
我正在使用本教程進行帳戶確認 email 但我沒有收到任何 email 或任何錯誤這里是代碼:
AuthMessageSenderOptions.cs
public class AuthMessageSenderOptions
{
public string SendGridUser { get; set; }
public string SendGridKey { get; set; }
}
appSettings.json
"AuthMessageSenderOptions": {
"SendGridUser": "MyUserName",
"SendGridKey": "SG.Xa................ MyKey"
},
電子郵件發件人.cs
public class EmailSender : IEmailSender
{
public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor)
{
Options = optionsAccessor.Value;
}
public AuthMessageSenderOptions Options { get; } //set only via Secret Manager
public Task SendEmailAsync(string email, string subject, string message)
{
return Execute(Options.SendGridKey, subject, message, email);
}
public Task Execute(string apiKey, string subject, string message, string email)
{
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress("info@reloadrs.com", Options.SendGridUser),
Subject = subject,
PlainTextContent = message,
HtmlContent = message
};
msg.AddTo(new EmailAddress(email));
// Disable click tracking.
// See https://sendgrid.com/docs/User_Guide/Settings/tracking.html
msg.SetClickTracking(false, false);
return client.SendEmailAsync(msg);
}
}
注冊.cs
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var confirmationLink = Url.Page(nameof(ConfirmEmail), "Account",
new { code, email = user.Email }, Request.Scheme);
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by" +
$" <a href='{HtmlEncoder.Default.Encode(confirmationLink)}'>clicking here</a>.");
}
在不僅在本地發布而且仍然相同之后,我也嘗試過嘗試
您可以獲得發送 email 的結果以及錯誤消息:
var response = await client.SendEmailAsync(msg);
if (response.StatusCode != HttpStatusCode.OK
&& response.StatusCode != HttpStatusCode.Accepted)
{
var errorMessage = response.Body.ReadAsStringAsync().Result;
throw new Exception($"Failed to send mail to {to}, status code {response.StatusCode}, {errorMessage}");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.