0

I have created a .Net Core WebAPI project with a React template. Now I want to send an email from the API with an email template. The template is in Html format so we need to read the Html file as a template from the root path but the 'wwwroot' path is not available in this project by default.

I have done this in my previous .Net Core MVC project as shown below but I don't know how to do this in the current project due to there is no 'WWWRoot' folder. Could anyone please help me that where to store this template file and how to retrieve it as a string in c#?

//Get the email template path
string templatePath = RuntimeConfig.SiteUrl + "/Template/EmailTemplate.html";

     //Adding the template to the email body
      using (var client = new WebClient())
      {
          string reader = client.DownloadString(templatePath);
          StringBuilder sb = new StringBuilder();
          sb.Append(reader);
          ....
          .... --logics---
      }

1 Answer 1

1

I want to send an email from the API with an email template.

In my view, email template file(s) should not serve directly to clients.

To achieve your requirement, you can try:

Create a folder to store your email template files

enter image description here

To access/read template file from that folder within controller action

public class EmailController : ControllerBase
{
    private IWebHostEnvironment _env;
    public EmailController(IWebHostEnvironment env)
    {
        _env = env;
    }

    [HttpPost]
    public IActionResult SendConfirmEmail()
    {
        var path= Path.Combine(_env.ContentRootPath, @"EmailTemplates\" + "ConfirmEmailTemplate.html");
        var template = System.IO.File.ReadAllText(path);

        //code logic here
        //...

    //other actions
    //...
Sign up to request clarification or add additional context in comments.

3 Comments

I have used your code it's working fine in my local but when I deploy this to my Azure app server then it throws 405 error that not able to find the resource file path. Its try to fetch from my local but not from the server path. Error: {"message":"Could not find a part of the path 'D:\\home\\site\\wwwroot\\Resources\\CustomFields\\UserControlFields.es.resx'."}
Could not find a part of the path 'D:\\home\\site\\wwwroot\\Resources\\CustomFields\\UserControlFields.es.resx'You can try to use Kudu console to check and manage folder/file of your site. If the file(s) indeed not exist under specific folder, you can try create and copy .resx file(s) to that folder.
Hi Fei, Can you help me with this issue? I have tried to fetch the file using the above code and it's giving the exact hosted server drive path that's like "c:/projectname/wwwroot/template" but I just want to open this document from hosted server URL like projectname.com/core/template. So I can give this API url to my UI team they will fetch video/image files by using the URL. Ex: see this "URL: github.com/konvajs/use-image". The GitHub is a web project but calling the url gives us an image. I want the same as the GitHub app to fetch a file from the server or source path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.