简体   繁体   English

如何在 ASP.Net IIdentityMessageService 中使用 SMTP 或 Office 365 帐户?

[英]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.我曾经使用 web.config 配置我的 SMTP 帐户,以便能够使用发送电子邮件功能,但 ASP.Net Identity 要求提供电子邮件服务。 How can I use SMTP or Office 365 email in the following area:如何在以下区域使用 SMTP 或 Office 365 电子邮件:

Register.aspx.cs注册.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.EmailService.SendTaskAsync有一条注释告诉您在何处插入电子邮件代码。 That's where you'd put your SMTP code, or your call to Office 365 API's, or a web service like SendGrid .这是您放置 SMTP 代码、调用 Office 365 API 或 Web 服务(如SendGrid )的地方 Here's an example of using SMTP.这是使用 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.显然,您可能希望将实际的电子邮件代码移动到另一个类,以便您可以将其重用于其他与电子邮件相关的任务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.Net Identity 2.0 - 如何使用SmtpClient实现IIdentityMessageService来执行异步SMTP? - Asp.Net Identity 2.0 - How to Implement IIdentityMessageService to do Async SMTP using SmtpClient? 如何在 asp.net C# 中使用启用双重身份验证的 office365 email 帐户发送 email? - How to send email using two factor authentication enabled office365 email account in asp.net C#? Asp.net MVC5 和 SMTP Office365 版本 4.5.2 出现异常,代码在版本 4.8 上运行良好 - Asp.net MVC5 with SMTP Office365 Exception at version 4.5.2, the code works well at version 4.8 ASP.NET Identity UserManager IIdentityMessageService将额外参数传递给SendAsync - ASP.NET Identity UserManager IIdentityMessageService pass extra parameters to SendAsync ASP.NET Identity 3.0上IIdentityMessageService的等价物是什么? - What is the equivalent for IIdentityMessageService on ASP.NET Identity 3.0? 在asp.net中读取/写入Office 365文档 - Read / Write Office 365 document in asp.net 使用Office365应用的Asp.Net身份验证 - Asp.Net Identity authentication with Office365 app Office 365与Asp.net Web应用程序(MVC应用程序)集成吗? - Office 365 integration with Asp.net web application (MVC Application)? 从Office 365单一登录到Asp.Net网站 - Single sign on from office 365 to Asp.Net website 使用Office 365作为ASP.net中的中继时,如何向电子邮件添加发件人(代表用户) - How to add a sender (on behalf of user) to the email when using Office 365 as relay in ASP.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM