0

I am developing a mail landing page. I want to upload html file which I will use as mail body to send email. Currently I am setting mail body like this:

MailMessage mail = new MailMessage();
mail.From = new MailAddress("robin35-908@diu.edu.bd");
mail.To.Add(new MailAddress(MailTo));
mail.Subject = "test";
mail.IsBodyHtml = true;
mail.Body=@"html text with (") replaced with ("") ";

Now my requirement is to input a html file and then use the text as mail body.I want to read the file from the location as the file would be in the same server. So no upload is required. How can I achieve that?

Exception handler
  • 172
  • 1
  • 3
  • 15
  • After you upload the file, You can read its text with `string text1 = File.ReadAllText("location/myfile.html");` – Manoz Oct 01 '18 at 10:48

1 Answers1

1

Using StreamReader to access the file, read as a stream.

Based on your question, I assume you already have a template.html file in your root application folder. Since it's a template, your html should contains some template key text, should add {{title}}, {{Name}}, {{Signature}} ...

For example: template.html

Dear {{Name}},

I would love to discuss with you about {{title}}

Regards,
{{Signature}}

Pseudo code:

string body = string.Empty;  
//using streamreader for reading my htmltemplate   

using(StreamReader reader = new StreamReader(Server.MapPath("~/template.html")))  
{  

    body = reader.ReadToEnd();  

}  
//Update your string with template key
body = body.Replace("{{Name}}", name);
body = body.Replace("{{Title}}", title);
body = body.Replace("{{Signature}}", signature);

return body;  
Jacky
  • 2,924
  • 3
  • 22
  • 34
  • Thanks for your answer. But the file will be dynamic. It is not in the project. It may be in another directory like desktop or D drive. I have a HttpPostedFile base file object in the method parameter. I need to get the full path of the file. But I can't get the original file path syntax. – Exception handler Oct 01 '18 at 11:22
  • 1
    @RobinKhan sorry for late reply, if the file is dynamic, the most you can access is root directory level of your .net Application. You can force user to upload to a folder, my be called `Template`, in your application, then using `Server.MapPath` to get your file directory. You can find some information from here https://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath – Jacky Oct 03 '18 at 01:28