我有一個新的MVC項目,並按照這篇文章發送郵件註冊sendgrid。 http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset沒有sendgrid的電子郵件,也沒有錯誤消息
在項目的根目錄中創建了一個新文件夾「manager」。在該文件夾中創建了一個新文件EmailService.cs
並帶有以下代碼。
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
await configSendGridasync(message);
}
// Use NuGet to install SendGrid (Basic C# client lib)
private async Task configSendGridasync(IdentityMessage message)
{
var myMessage = new SendGridMessage();
myMessage.AddTo(message.Destination);
myMessage.From = new System.Net.Mail.MailAddress(
"[email protected]", "Joe S.");
myMessage.Subject = message.Subject;
myMessage.Text = message.Body;
myMessage.Html = message.Body;
var credentials = new NetworkCredential(
ConfigurationManager.AppSettings["mailAccount"],
ConfigurationManager.AppSettings["mailPassword"]
);
// Create a Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email.
if (transportWeb != null)
{
await transportWeb.DeliverAsync(myMessage);
}
else
{
Trace.TraceError("Failed to create Web transport.");
await Task.FromResult(0);
}
}
}
將此添加到web.config
用正確的價值觀
<add key="mailAccount" value="xyz" />
<add key="mailPassword" value="password" />
而且修改了登記控制器與此
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>");
我已經做別的什麼。我可以註冊。獲取沒有錯誤,沒有電子郵件。爲什麼我需要在項目啓動時將EmailService
添加到項目中?
就這樣,你知道你並不孤單,我與SendGrid有同樣的問題:沒有收到郵件,SendGrid發送計數沒有在儀表板中遞增,也沒有拋出異常。如果/當我找到一個,我會發佈一個答案:) –