UserManager SendEmailAsync No Email Sent

前端 未结 4 699
旧巷少年郎
旧巷少年郎 2020-12-01 09:41

I am using the following code to try to send an email asynchronously, but no email is sent and I am not sure what is being done incorrectly. I have also added the 2nd segme

4条回答
  •  一生所求
    2020-12-01 09:56

    I think you are using Macrosoft ASP.NET Identity and SMTP email client server. Then your full configuration would be like below :

    Web.config

    
    
      
        
      
    
    
    

    Create a class SmtpEmailService.cs

    public class SmtpEmailService : IIdentityMessageService
    {
        readonly ConcurrentQueue _clients = new ConcurrentQueue();
    
        public async Task SendAsync(IdentityMessage message)
        {
            var client = GetOrCreateSmtpClient();
            try
            {
                MailMessage mailMessage = new MailMessage();
    
                mailMessage.To.Add(new MailAddress(message.Destination));
                mailMessage.Subject = message.Subject;
                mailMessage.Body = message.Body;
    
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.IsBodyHtml = true;
    
                // there can only ever be one-1 concurrent call to SendMailAsync
                await client.SendMailAsync(mailMessage);
            }
            finally
            {
                _clients.Enqueue(client);
            }
        }
    
    
        private SmtpClient GetOrCreateSmtpClient()
        {
            SmtpClient client = null;
            if (_clients.TryDequeue(out client))
            {
                return client;
            }
    
            client = new SmtpClient();
            return client;
        }
    }
    

    IdentityConfig.cs

    // Configure the application user manager used in this application. 
    //UserManager is defined in ASP.NET Identity and is used by the application.
    public class ApplicationUserManager : UserManager
    {
        public ApplicationUserManager(IUserStore store, IIdentityMessageService emailService)
            : base(store)
        {
            this.EmailService = emailService;
        }
    
        public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore(context.Get()), new SmtpEmailService());
            .
            .
            .
            .
            return manager;
        }
    }
    

    If you are using dependency injection(DI) then configure it. I am using UnityContainer (UnityConfig.cs) so my configuration is :

    container.RegisterType();
    

    Finally use it from your controller :

    public async Task TestSmtpMail()
    {
        var subject = "Your subject";
        var body = "Your email body it can be html also";
        var user = await UserManager.FindByEmailAsync("xxx@gmail.com");
        await UserManager.SendEmailAsync(user.Id, subject, body);
        return Ok();
    }
    

    You can get an error something like :

    The SMTP server requires a secure connection or the client was not authenticated.

    Then Allow less security apps & Allow gmail account making it possible for other apps to gain access

    For more details and SendGrid client visit here

提交回复
热议问题