I am using this tutorial to register a user with email confirmation, but I can't find the emailservice
class.
I am using MVC 5 with Identity 2.2.
I am using this tutorial to register a user with email confirmation, but I can't find the emailservice
class.
I am using MVC 5 with Identity 2.2.
Thanks but i already find the solution
I have created the class emailservice and them
Selected the email from usermanager, like this
userManager.EmailService = new EmailService();
As far as I know, there is no such .net class as EmailService
Typically you use the SmtpClient
class to send emails:
SmtpClient client = new SmtpClient("server.address.com");
MailAddress from = new MailAddress(fromAddress, fromName);
MailMessage msg = new MailMessage();
msg.From = from;
foreach(string addr in to)
msg.To.Add(addr);
msg.Body = content;
msg.Subject = subject;
client.Send(msg);
I had the same question, and used the following solution. First, create a new folder in your project called services, and create a new class with the following code from the tutorial:
namespace YourProject.Services
{
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
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("no-reply@your-domain.com", "Customer Service");
myMessage.Subject = message.Subject;
myMessage.Text = message.Body;
myMessage.Html = message.Body;
var credentials = new NetworkCredential(
ConfigurationManager.AppSettings["SendGridUsername"],
ConfigurationManager.AppSettings["SendGridPassword"]
);
// 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);
}
}
}
}
Then, what I did was set the EmailService property of the UserManager object in my controller to a new instance of the EmailService class (scroll all the way to the right to see it, sorry for that):
namespace YourProject.Controllers
{
public class UserManagementController : Controller
{
private UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())){EmailService = new Services.EmailService()};
// GET: UserManagement
Then, when you call
UserManager.SendEmailAsync(user.Id, subject, body);
It will send the email using your SendGrid accound as configured in the EmailService class above.