How to use a Text File as a Template to Send Email in dotnet core 2

Answer:
  1. One way to accomplish this is by creating a text file and defining your template wrapped in a pair of double hash symbols
  2. For Example:

    Name: ##Name##
    E-Mail Address: ##Email##
    Home Phone: ##Phone##
    Comments: ##Comments##
  3. After you create a text file in your project and have typed your template as shown in 2, then you would write your C# code like this;

    using System.IO // For reading the file
    using System.Net.Mail //Sending the Email

    //First read the file
    string myFile = Server.MapPath("~/App_Directory/YourTextFileName.txt");
    string mailBody = File.ReadAllText(myFile);

    mailBody = mailBody.Replace("##Name##","Jack");
    mailBody = mailBody.Replace("##E-Mail Address##","[email protected]");

    Go on and implement this until you see fit.

Edit this Article
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy