Click here to Skip to main content
15,888,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey
How do I send comfirm mail together html page ?

I use Identity smtp mail sender . But I can't send to mail with my html template page.

Thanks for help

This code :

What I have tried:

C#
public async Task<ActionResult> Register(RegisterViewModel model)
      {
          if (ModelState.IsValid)
          {
              var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
              var result = await UserManager.CreateAsync(user, model.Password);
              if (result.Succeeded)
              {
                  await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                  await UserManager.AddToRoleAsync(user.Id, "Uye");
                  // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                  // Send an email with this link
                  string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                  var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                  await UserManager.SendEmailAsync(user.Id, "Üyelik Doğrulama", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
       //
                  return RedirectToAction("Index", "Home");
              }
              AddErrors(result);
          }

          // If we got this far, something failed, redisplay form
          return View(model);
      }




C#
public Task SendAsync(IdentityMessage message)
       {
           SmtpClient client = new SmtpClient();
           client.Port = 587;
           client.Host = "in-v3.mailjet.com";
           client.EnableSsl = true;
           client.Timeout = 10000;
           client.DeliveryMethod = SmtpDeliveryMethod.Network;
           client.UseDefaultCredentials = false;
           client.Credentials = new NetworkCredential("user", "key");

           return client.SendMailAsync("mail", message.Destination, message.Subject,message.Body);
       }
Posted
Updated 24-Aug-19 9:44am
Comments
Afzaal Ahmad Zeeshan 24-Aug-19 14:11pm    
What is the problem with this code?
Yunus Bulduk 24-Aug-19 14:59pm    
I cannot send the HTML I prepared as a template.
Afzaal Ahmad Zeeshan 24-Aug-19 15:44pm    
Please see my answer in Solution 1.
Yunus Bulduk 24-Aug-19 14:58pm    
thanks answer . problem I can't send html code flour View

1 solution

First of all, you are using IdentityMessage, instead of a MailMessage.

IdentityMessage Class (Microsoft.AspNet.Identity) | Microsoft Docs[^]
MailMessage Class (System.Net.Mail) | Microsoft Docs[^]

There was option with MailMessage to send HTML content, and have it processed and delivered with special MIME. If you want to send the email with HTML content in it, you need to set the IsBodyHtml flag to be true, so anywhere before SendMailAsync(), do this,
C#
client.IsBodyHtml = true;
Then send the email, and this time it will be parsed as HTML document and rendered properly. Also one thing that you can always send a link to your web page so your viewers can link on the click to preview it as an HTML document if their mail software is broken or older.

See here, MailMessage.IsBodyHtml Property (System.Net.Mail) | Microsoft Docs[^]

One more thing, .NET Core recommends to use MailKit for emails in .NET Core, GitHub - jstedfast/MailKit: A cross-platform .NET library for IMAP, POP3, and SMTP.[^]. You can check out how that SDK enables you to send HTML bodied documents.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900