Home > database >  Asp.net mvc 5 SendGrid not confirming Email upon click
Asp.net mvc 5 SendGrid not confirming Email upon click

Time:07-30

I was following the "Create a secure ASP.NET MVC 5 web app with log in, email confirmation and password reset (C#)" from enter image description here

So this is how i configure SendGrid in the IdentityConfig.cs

 public class EmailService : IIdentityMessageService
    {
         public Task SendAsync(IdentityMessage message)
            {
                return Task.Factory.StartNew(()=> 
                {
                    sendMail(message);
                });
            }
        async void sendMail(IdentityMessage message)
        {
           // var apiKey = ConfigurationManager.AppSettings["SendGridKey"];
            var apiKey = "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";
            var client = new SendGridClient(apiKey);
            var myMessage = new SendGridMessage();
            myMessage.AddTo(message.Destination);
            myMessage.From = new EmailAddress("[email protected]","Angelito");
            myMessage.Subject = message.Subject;
            myMessage.PlainTextContent = message.Body;
            await client.SendEmailAsync(myMessage);
        }
    }

i also runned the application in debug mode, and add it a break point to the Register Method and ConfirmEmail Method. In the ConfirmEmail Method i got this Error. i am guessing it has to be something with the Token..."

enter image description here

If Anyone could help me fix this i would really apreciated... Also if you guys could recommend latest books to Become a pro at asp.net or core. i would much apreciated.

So i made the changes to the code... Am still receiving the the confirmation link on my Email but when i click on it get invalidToken if i add a break point to ConfirmEmail Method...

enter image description here

CodePudding user response:

The problem is in the code. You shouldn't be using async void or using Task.Factory.StartNew. async void is only meant for asynchronous event handlers and can't be awaited.

Since SendEmailAsync is asynchronous, there's no reason to run it into another task. Even if there was a reason to start another task, Task.Run should be used, not Task.Factory.StartNew.

The code should change to :

public Task SendAsync(IdentityMessage message)
{
    await sendMailAsync(message);
}

async Task sendMailAsync(IdentityMessage message)
{
    ...
    await client.SendEmailAsync(myMessage);
}

CodePudding user response:

Okay so Here is how the code looks like inside IdentityConfig.cs In order to send Email confirmation and to confirm it on your inbox. The Following code, worked for me. The rest is the same as the microsoft.docs

IdentityConfig.cs

     public async Task SendAsync(IdentityMessage message)
        {
        await configSendGridasync(message);
        }
    private async Task configSendGridasync(IdentityMessage message)
    {
        var apiKey = "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";
        var client = new SendGridClient(apiKey);
        var from = new EmailAddress("[email protected]","Angel" );
        var subject = message.Subject;
        var to = new EmailAddress(message.Destination);
        var plainTextContent = message.Body;
        var htmlContent = message.Body;
        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent,htmlContent);

        // Send the email.
        if (client != null)
        {
            await client.SendEmailAsync(msg);
        }
        else
        {
            Trace.TraceError("Failed to create Web transport.");
            await Task.FromResult(0);
        }
    }
  • Related