简体   繁体   中英

How to use SMTP or Office 365 account in ASP.Net IIdentityMessageService?

I used to use web.config to configure my SMTP account in order to be able to use send email function but ASP.Net Identity is asking for Email Services. How can I use SMTP or Office 365 email in the following area:

Register.aspx.cs

IdentityResult result = manager.Create(user, Password.Text );

            if (result.Succeeded)
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                string code = manager.GenerateEmailConfirmationToken(user.Id);
                string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
                manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");

                signInManager.SignIn( user, isPersistent: false, rememberBrowser: false);
                IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else 
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }

IdentityConfig.cs

public Task SendAsync(IdentityMessage message)
    {


        // Credentials:
        var credentialUserName = "test@test.com";
        var sentFrom = "test@test.com";
        var pwd = "testpassword";

        // Configure the client:
        System.Net.Mail.SmtpClient client =
            new System.Net.Mail.SmtpClient("smtp-mail.outlook.com");

        client.Port = 587;
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;

        // Create the credentials:
        System.Net.NetworkCredential credentials =
            new System.Net.NetworkCredential(credentialUserName, pwd);

        client.EnableSsl = true;
        client.Credentials = credentials;

        // Create the message:
        var mail =
            new System.Net.Mail.MailMessage(sentFrom, message.Destination);

        mail.Subject = message.Subject;
        mail.Body = message.Body;

        // Send:
        return client.SendMailAsync(mail);


        // Plug in your email service here to send an email.
        //return Task.FromResult(0);
    }

Web.Config

 <system.net>
    <mailSettings>
      <smtp>
        <network defaultCredentials="true" enableSsl="true" host="smtp.office365.com" port="587" password="password" userName="username" />
      </smtp>
    </mailSettings>
  </system.net>

I appreciate your efforts in reaching a solution for my problem.

Inside EmailService.SendTaskAsync there's a comment that tells you where to plugin your email code. That's where you'd put your SMTP code, or your call to Office 365 API's, or a web service like SendGrid . Here's an example of using SMTP.

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        //create a client, it should pick up the settings from web.config
        var client = new SmtpClient();

        //now construct a MailMessage object from the message
        var email = new MailMessage("donotreply@mysite.com", message.Destination, message.Subject, message.Body);

        //send the email asynchronously
        await client.SendMailAsync(email);

        return Task.FromResult(0);
    }
}

Obviously, you might want to move your actual email code to another class so that you can reuse it for other email related tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM