-4

I am using this tutorial to register a user with email confirmation, but I can't find the emailservice class.

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset

I am using MVC 5 with Identity 2.2.

Puck
  • 2,080
  • 4
  • 19
  • 30

3 Answers3

1

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();

0

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);
pquest
  • 3,151
  • 3
  • 27
  • 40
  • As an addition: Implement the [IIdentityMessageService](https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.iidentitymessageservice(v=vs.108).aspx) interface and put the code from @pquest into the **SendAsync** method. Your class can then be registered for sending emails. – Horizon_Net Jan 28 '15 at 18:10
0

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.

Ben
  • 1,032
  • 8
  • 18