0

I have searched for days trying to figure out how to use my C# Windows Form App to send html formatted email from a user inputted textbox to some email addresses. I have only found how to use ASP.net to do this, but i am not using ASP.net. Just need to get the email to be formatted with html and have parts of it be substituted with other info from the app. Here is something I found that should work, but I am guessing only works with ASP.net:

private string PopulateBody(string userName, string title, string url, string description)
{
    string body = string.Empty;
    using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
    {
        body = reader.ReadToEnd();
    }
    body = body.Replace("{UserName}", userName);
    body = body.Replace("{Title}", title);
    body = body.Replace("{Url}", url);
    body = body.Replace("{Description}", description);
    return body;
}

StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")) part says that "Server" cannot be used. Is there a different way to have a html template be read and have items replaced in it then sent to an email address? Any help is appreciated!!

2
  • This code is not sending an email ... it just read template replace and return html as string ... did you tried something obvious like replacing Server.MapPath("~/EmailTemplate.htm") with @"O:\real\full\path\to\the\template.htm"
    – Selvin
    Feb 13, 2020 at 16:30
  • This worked thanks! Feb 14, 2020 at 19:43

2 Answers 2

1

You'll want to get the local path of the executable application, which likely isn't running in an ASP.NET server context (so you can't use Server.MapPath()). What you're looking for would be something like this:

using (StreamReader reader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + "EmailTemplate.htm")))

This should attempt to open a stream using a file named "EmailTemplate.htm" relative to the location of the executable (next to it in the same folder).

Check out this SO question for more info on getting an executable folder path: Get current folder path

0
private string PopulateBody(string userName, string title, string url, string description)
{
string body = string.Empty;
Assembly assembly = Assembly.GetExecutingAssembly();
StreamReader reader =  new StreamReader(assembly.GetManifestResourceStream("YourProjectName.YourHTMLPageName.html"));

body = reader.ReadToEnd();
body = body.Replace("{UserName}", userName);
   body = body.Replace("{Title}", title);
   body = body.Replace("{Url}", url);
   body = body.Replace("{Description}", description);

return body;
}

Make sure that you entered the html file to your project (Copy ,paste on your project folder )refresh the project from refresh icon then write click on the html file then choose include and then choose properties, then from build action combobox choose from it Embedded Resource

img link for calrification ::: https://www.dropbox.com/s/ec2iy7qtv4kvc0n/stack.JPG?dl=0

1
  • Please add a description of what this code is doing. Also it appears an edit is active, but this code should use fences and should be formatted.
    – Nik P
    Jun 3, 2020 at 20:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.