我一直在關注本教程:http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset。我已經通過了三次,並檢查了幾個Stackoverflow的帖子,但我仍然不知道我缺少什麼。通過調試,Visual Studio顯示myMessage具有它所需的一切(要發送給的電子郵件地址,郵件主題,郵件正文,來自哪裏的人等),但實際上我沒有收到確認郵件。這是我目前擁有的代碼:SendGrid不發送電子郵件
IdentityConfig.cs
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
// line below was commented out and replaced upon tutorial request
//return Task.FromResult(0);
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]", "Robert");
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);
}
}
}
的AccountController:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// commented below code and RedirectToAction out so it didn't auto log you in.
//await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
//For more information on how to enable account confirmation and password reset please visit http://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>");
ViewBag.Message = "Check your email and confirm your account, you must be confirmed before you can log in.";
return View("Info");
//return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
Web.config文件:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="mailAccount" value="[email protected]" /> <!--the mailAccount that SendGrid gave me through Azure marketplace to use-->
<add key="mailPassword" value="xxxxxx" /> <!--password taken out, but I used the one from SendGrid-->
</appSettings>
的代碼生成,並沒有錯誤運行,但我在測試時從未收到實際的電子郵件(我使用了兩個單獨的Gmail帳戶和一個雅虎會員'mount)。任何意見/幫助將不勝感激!
我曾與sendgrid從我的本地開發機發送的問題,但一旦我把它部署到服務器/主機按預期工作。 (爲什麼它的價值) –
不是100%確定SendGrid方面可用的東西,但它看起來像是某種儀表板。那裏有任何跡象? – mxmissile
@GlennFerrie我想我現在可以嘗試部署,但是我在做之前想要更多的成品。 我查看了從Azure網站的SendGrid的信息,並沒有真正發現任何東西。我會看起來更多一點。感謝您的建議! –