私はこのチュートリアルに従っています:http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset。私は今3回それを通過し、いくつかの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>
私はそれをテストすると実際の電子メールを得ることはありません(私は2つの別々のgmailアカウントとyahoo accオント)。どんな助言/助けもありがとう!
を/私は私の地元のdevのマシンから送信sendgridに問題があったが、私はそれをサーバーに展開した後それが期待どおりに機能するホスト。 (その価値のために) –
SendGrid側で何が利用できるのかを100%確かめることはできませんが、何らかの種類のダッシュボードがあるようです。そこにどんな徴候がありますか? – mxmissile
@GlennFerrie私は今見たいと思っていますが、私はそれをする前にもっと完成した製品を望んでいました。 私はAzureのウェブサイトからSendGridの情報を見てきましたが、本当に何も見つかりませんでした。私はもう少し見ていきます。提案していただきありがとうございます! –