I have not used sendgrid on my website for the email confirmation service. I do not see the point on small sites with low traffic.
I use the gmail SMTP service and it works fine:
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
{
public string UserName { get; set; }
public GmailEmailService():
base( ConfigurationManager.AppSettings["GmailHost"], Int32.Parse(ConfigurationManager.AppSettings["GmailPort"]) )
{
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>
<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>
In the 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.com", "(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() )
{
await mailClient.SendMailAsync(email);
}
}
}
: IdentityConfig.cs:
using System.Net.Mail;
, 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)
{
string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Account confirmation");
ViewBag.errorMessage = "Please confirm the email was sent to you.";
return View("ShowMsg");
}
AddErrors(result);
}
return View(model);
}
[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)
{
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;
}
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>
}
}
!, .
p.s:
" " gmail.
gmail
:
- ASP.NET MVC 5 .
ASP-NET-MVC-Confirm-Registration-Email
fooobar.com/questions/544753/...
..
Gmail ASP.Net
-email--gmail-smtp -...