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("[email protected]");
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?

1
  • After you upload the file, You can read its text with string text1 = File.ReadAllText("location/myfile.html"); Commented Oct 1, 2018 at 10:48

1 Answer 1

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;  
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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 stackoverflow.com/questions/275781/…

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.