标题pretty多说了。有没有添加电子邮件确认我的应用程序,而无需使用发送网格的一种方式?我的Azure帐户不会让我用它表示,它在我的心不是区可用,我不能似乎找到了另一种解决方案。
解决方案我没有sendgrid无论是在我的网站的电子邮件确认服务使用。我看不出有一点它小网站低流量。
我用gmail SMTP服务,而不是和它完美的作品:
使用系统;使用System.Collections.Generic;使用System.Configuration;使用System.Linq的;使用System.Net.Mail;使用的System.Web;命名空间MyProject.MyClasses{公共类GmailEmailService:SmtpClient{ // Gmail的用户名 公共字符串用户名{获得;组; } 公共GmailEmailService(): 基地(ConfigurationManager.AppSettings [GmailHost],Int32.Parse(ConfigurationManager.AppSettings [GmailPort])) { //从web.config文件中的值: this.UserName = ConfigurationManager.AppSettings [GmailUserName]; this.EnableSsl = Boolean.Parse(ConfigurationManager.AppSettings [GmailSsl]); this.UseDefaultCredentials = FALSE; this.Credentials =新System.Net.NetworkCredential(this.UserName,ConfigurationManager.AppSettings [GmailPassword]); }}}
在web.config文件中添加以下内容:
< configSections> <&的appSettings GT; <! - SMPTP服务器(确认电子邮件) - GT; <添加键=GmailUserName值=[用户名] @ gmail/> <添加键=GmailPassword值=[您的密码]/> <添加键=GmailHostVALUE =smtp.gmail/> <添加键=GmailPortVALUE =587/> <添加键=GmailSslVALUE =真/> < /的appSettings> < / configSections>在App_Start \\ IdentityConfig.cs文件改变SendAsync方法来此code:
公共类EmailService:IIdentityMessageService{公共异步任务SendAsync(IdentityMessage消息){ 电子邮件MAILMESSAGE新= MAILMESSAGE(新MailAddress(noreply@myproject,(不回复)) 新的MailAddress(message.Destination)); email.Subject = message.Subject; email.Body = message.Body; email.IsBodyHtml = TRUE; 使用(VAR的MailClient =新MyProject.MyClasses.GmailEmailService()) { //为了使用从电子邮件地址,取消此行原文: //email.From =新的MailAddress(mailClient.UserName,(不回复)); 等待mailClient.SendMailAsync(电子邮件); }}}请注意:你也有这样的使用添加到IdentityConfig.cs文件:
使用System.Net.Mail;的最后一件事是更新您的AccountController文件中的以下内容类似的东西:
//// POST:/帐号/注册[HttpPost][使用AllowAnonymous][ValidateAntiForgeryToken]公共异步任务<&的ActionResult GT;寄存器(RegisterViewModel模型){ 如果(ModelState.IsValid) { VAR用户=新ApplicationUser {用户名= model.UserName,电子邮件= model.Email}; VAR的结果=等待UserManager.CreateAsync(用户,model.Password); 如果(result.Succeeded) { //注释以下行以prevent直到用户确认登录: //等待SignInManager.SignInAsync(用户,isPersistent:假的,rememberBrowser:假); 字符串callbackUrl =等待SendEmailConfirmationTokenAsync(user.Id,账户确认书); //取消注释在本地调试 // TempData的[ViewBagLink] = callbackUrl; ViewBag.errorMessage =请确认电子邮件发送给您。 返回视图(ShowMsg); } AddErrors(结果); } //如果我们走到这一步,事情失败了,重新显示形式 返回查看(模型);}//// GET:/帐号/ ConfirmEmail[使用AllowAnonymous]公共异步任务<&的ActionResult GT; ConfirmEmail(字符串userid,字符串code){ 如果(用户ID == NULL || code == NULL) { 返回视图(错误); } VAR的结果=等待UserManager.ConfirmEmailAsync(用户ID,code); 返回查看(result.SucceededConfirmEmail:错误);}私人异步任务<串GT; SendEmailConfirmationTokenAsync(字符串userid,字符串主题){ //有关如何启用帐户确认和重置密码,请访问go.microsoft/fwlink/?LinkID=320771更多信息 //发送一封电子邮件,此链接: 字符串code =等待UserManager.GenerateEmailConfirmationTokenAsync(用户ID); VAR callbackUrl = Url.Action(ConfirmEmail,帐户,新的{用户id =用户ID,code = code},协议:Request.Url.Scheme); 等待UserManager.SendEmailAsync(用户ID,主题,请以&lt确认您的账户; A HREF = \\+ callbackUrl +\\>点击此处< / A>中); 返回callbackUrl;}和一个可能的观点ShowMsg.cs文件可以是:
@ { ViewBag.Title =消息;}< H1类=TEXT-危险> @ ViewBag.Title< / H1>@ { 如果(String.IsNullOrEmpty(ViewBag.errorMessage)) { < H3类=TEXT-危险>处理您的请求时发生错误< / H3 GT&; } 其他 { < H3类=TEXT-危险> @ ViewBag.errorMessage< / H3 GT&; }}这就是它!它为我工作。
P.S:你必须让你的Gmail帐户的安全性较低,应用选项。按照Gmail的链接 的
我用下面的文章来写的解决方案:
创建..
ASP-NET-MVC-确认-Registration-电子邮件
stackoverflow-email-using-gmail-smtp-in-asp-net-mvc-application
加入的双因素认证..
如何-于─发送安在ASP.Net使用Gmail匿名电子邮件
的如何对发送的电子邮件,使用Gmail邮箱,SMTP -...
The title pretty much says it. Is there a way of adding Email Confirmation to my application without using the send grid ? my Azure account won't let me use it, says it isnt available in my zone and i cant seem to find another solution .
解决方案I didn't use sendgrid neither in my website for the Email confirmation service. I See no point having it in small websites with low traffic.
I use gmail SMTP service instead and it's works perfectly:
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"]); } }}
in the Web.Config file add the following:
<configSections> <appSettings> <!--Smptp Server (confirmations emails)--> <add key="GmailUserName" value="[your user name]@gmail"/> <add key="GmailPassword" value="[your password]"/> <add key="GmailHost" value="smtp.gmail"/> <add key="GmailPort" value="587"/> <add key="GmailSsl" value="true"/> </appSettings> </configSections>In App_Start\IdentityConfig.cs file change the SendAsync method to this code:
public class EmailService : IIdentityMessageService { public async Task SendAsync(IdentityMessage message) { MailMessage email = new MailMessage(new MailAddress("noreply@myproject", "(do not reply)"), new MailAddress(message.Destination)); email.Subject = message.Subject; email.Body = message.Body; email.IsBodyHtml = true; using( var mailClient = new MyProject.MyClasses.GmailEmailService() ) { //In order to use the original from email address, uncomment this line: //email.From = new MailAddress(mailClient.UserName, "(do not reply)"); await mailClient.SendMailAsync(email); } } }note: you will also have to add this using to the IdentityConfig.cs file:
using System.Net.Mail;The last thing is to update the following in your AccountController file to something like that:
// // 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 go.microsoft/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; }and a possible view ShowMsg.cs file can be:
@{ 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> } }That's it!, It works for me.
p.s: you will have to allow the "less-secure-apps" option in your gmail account. follow that gmail link
I used the following articles to write that solution:
Create a secure ASP.NET MVC 5 web app with..
ASP-NET-MVC-Confirm-Registration-Email
stackoverflow-email-using-gmail-smtp-in-asp-net-mvc-application
adding-two-factor-authentication..
how-to-send-an Anonymous Email using Gmail in ASP.Net
how-to-send-email-using-gmail-smtp-...
更多推荐
电子邮件确认在ASP.NET mvc5没有sendgrid
发布评论