Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 5 无sendgrid的ASP.NET mvc5中的电子邮件确认_Asp.net Mvc 5 - Fatal编程技术网

Asp.net mvc 5 无sendgrid的ASP.NET mvc5中的电子邮件确认

Asp.net mvc 5 无sendgrid的ASP.NET mvc5中的电子邮件确认,asp.net-mvc-5,Asp.net Mvc 5,标题差不多就是这么说的。有没有一种方法可以在不使用发送网格的情况下将电子邮件确认添加到我的应用程序中?我的Azure帐户不允许我使用它,说它在我的区域中不可用,我似乎找不到其他解决方案。我的网站上也没有使用sendgrid来提供电子邮件确认服务。我认为在流量低的小型网站上使用它毫无意义 我改用gmail SMTP服务,它可以完美地工作: using System; using System.Collections.Generic; using System.Configuration; usin

标题差不多就是这么说的。有没有一种方法可以在不使用发送网格的情况下将电子邮件确认添加到我的应用程序中?我的Azure帐户不允许我使用它,说它在我的区域中不可用,我似乎找不到其他解决方案。

我的网站上也没有使用sendgrid来提供电子邮件确认服务。我认为在流量低的小型网站上使用它毫无意义

我改用gmail SMTP服务,它可以完美地工作:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Web;

namespace MyProject.MyClasses
{
public class GmailEmailService : SmtpClient
{
    // Gmail user-name
    public string UserName { get; set; }

    public GmailEmailService():
        base( ConfigurationManager.AppSettings["GmailHost"], Int32.Parse(ConfigurationManager.AppSettings["GmailPort"]) )
    {
        //Get values from web.config file:
        this.UserName = ConfigurationManager.AppSettings["GmailUserName"];
        this.EnableSsl = Boolean.Parse( ConfigurationManager.AppSettings["GmailSsl"] );
        this.UseDefaultCredentials = false;
        this.Credentials = new System.Net.NetworkCredential(this.UserName, ConfigurationManager.AppSettings["GmailPassword"]);
    }


}
}

在Web.Config文件中添加以下内容:

  <configSections>

      <appSettings>

        <!--Smptp Server (confirmations emails)-->
        <add key="GmailUserName" value="[your user name]@gmail.com"/>
        <add key="GmailPassword" value="[your password]"/>
        <add key="GmailHost" value="smtp.gmail.com"/>
        <add key="GmailPort" value="587"/>
        <add key="GmailSsl" value="true"/>

      </appSettings>

  </configSections>
注意:您还必须使用以下内容将其添加到IdentityConfig.cs文件:

using System.Net.Mail;
最后一件事是将AccountController文件中的以下内容更新为类似的内容:

//
// POST: /Account/Register
[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)
        {
            //Comment the following line to prevent log in until the user is confirmed:
            //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

            string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Account confirmation");

            // Uncomment to debug locally 
            // TempData["ViewBagLink"] = callbackUrl;

            ViewBag.errorMessage = "Please confirm the email was sent to you.";
            return View("ShowMsg");
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}


//
// GET: /Account/ConfirmEmail
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string userId, string code)
{
    if (userId == null || code == null)
    {
        return View("Error");
    }
    var result = await UserManager.ConfirmEmailAsync(userId, code);
    return View(result.Succeeded ? "ConfirmEmail" : "Error");
}


private async Task<string> SendEmailConfirmationTokenAsync(string userID, string subject)
{
    // 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(userID);
    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userID, code = code }, protocol: Request.Url.Scheme);
    await UserManager.SendEmailAsync(userID, subject, "Please confirm your account by <a href=\"" + callbackUrl + "\">clicking here</a>");


    return callbackUrl;
}        
//
//职位:/Account/Register
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务);
返回callbackUrl;
}        
ShowMsg.cs文件的可能视图可以是:

@{
    ViewBag.Title = "Message";
}

<h1 class="text-danger">@ViewBag.Title</h1>
@{
    if (String.IsNullOrEmpty(ViewBag.errorMessage))
    {
        <h3 class="text-danger">An error occurred while processing your request.</h3>
    }
    else
    {
        <h3 class="text-danger">@ViewBag.errorMessage</h3>
    }
}
@{
ViewBag.Title=“消息”;
}
@视图包。标题
@{
if(String.IsNullOrEmpty(ViewBag.errorMessage))
{
处理您的请求时出错。
}
其他的
{
@ViewBag.errorMessage
}
}
就这样!对我来说很有效

附言: 你必须在你的gmail帐户中允许“不太安全的应用”选项。

我使用以下文章来编写该解决方案:


使用自托管SMTP或其他提供程序怎么样?对于标识,您只需实现IIdentityMessageService并配置标识即可使用。下面给出了一个示例。我已经尝试过了,但这个错误总是会突然出现:根据错误的验证过程提示,远程证书无效?您认为这是因为你没有有效的证书。看看你能做什么。记住,你不应该在生产中这样做(如线程中所述)。我已经看过了,但这不是一个解决方案。而且,它不再起作用了。我怎样才能得到有效的证书?非常感谢。工作得很有魅力!
@{
    ViewBag.Title = "Message";
}

<h1 class="text-danger">@ViewBag.Title</h1>
@{
    if (String.IsNullOrEmpty(ViewBag.errorMessage))
    {
        <h3 class="text-danger">An error occurred while processing your request.</h3>
    }
    else
    {
        <h3 class="text-danger">@ViewBag.errorMessage</h3>
    }
}