0

I'm trying to generate a confirmation link to be sent to the email. The email was sent and received but the link is just not there. heres the code

[HttpPost]
        public async Task<IActionResult> SendEmail(Otp request)
        {

            randomcode = CreateRandomToken();
            var confirmationLink = Url.Action("confirmotp", "Authentication", new {randomcode}, Request.Scheme);
            var email = new MimeMessage();
            email.From.Add(MailboxAddress.Parse(_config.GetSection("EmailUsername").Value));
            email.To.Add(MailboxAddress.Parse(request.EmailAddress));
            email.Subject = "Confirmation Link For" + (request.EmailAddress);
            email.Body = new TextPart(TextFormat.Html)
            {
                Text = "Hello " + request.EmailAddress + ", click on the link: " + confirmationLink
            }
            using var smtp = new SmtpClient();
            smtp.Connect(_config.GetSection("EmailHost").Value, 587, SecureSocketOptions.StartTls);
            smtp.Authenticate(_config.GetSection("EmailUsername").Value, _config.GetSection("EmailPassword").Value);
            smtp.Send(email);
            smtp.Disconnect(true);
            return Ok();
        }

and this is the email I received :

Hello xxx@gmail.com, click on the link:

the link is just not being read. appreciate any help i can get thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • What is the value of confirmationLink when you inspect it in the debugger? – mason Dec 24 '22 at 14:16
  • its not returning anything. just null. –  Dec 24 '22 at 14:28
  • so I think you can hard code a string as the url to be be value of variable `confirmationLink ` then test again. If it worked, then try to troubleshoot why `Url.Action` didn't return correct value. By the way, can [this] help you?(https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.urlhelper.action?view=aspnet-mvc-5.2#overloads) – Tiny Wang Dec 26 '22 at 02:58
  • @TinyWang yes i did the hard code one and it worked. still not sure why url.action didnt return any value and thank you for the link, I have looked into, gonna try with those methods. thanks –  Dec 26 '22 at 05:56
  • thanks for your reply, hard code string can work, then we only need to troubleshoot Url.Action ... – Tiny Wang Dec 26 '22 at 07:01

1 Answers1

-1

Please try this to create the URL:

Uri uri = new Uri(url);

More details can be found in How to build a Url?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109