如何在 asp.net mvc 中使用 html 文件作为邮件正文的输入

How to use html file as input for mail body in asp.net mvc

我正在开发一个邮件登陆页面。我想上传 html 文件,我将用它作为邮件正文来发送电子邮件。目前我正在这样设置邮件正文:

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 ("") ";

现在我的要求是输入一个 html 文件,然后使用文本作为邮件 body.I 想要从该位置读取文件,因为该文件将位于同一服务器中。所以不需要上传。我怎样才能做到这一点?

使用StreamReader访问文件,作为流读取。

根据您的问题,我假设您的根应用程序文件夹中已经有一个 template.html 文件。 由于它是一个模板,您的 html 应该包含一些模板关键文本,应该添加 {{title}}, {{Name}}, {{Signature}} ...

例如:template.html

Dear {{Name}},

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

Regards,
{{Signature}}

伪代码:

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;