我已经搜寻了好几天试图弄清楚如何使用C#Windows Form App将html格式的电子邮件从用户输入的文本框发送到某些电子邮件地址。我只发现了如何使用ASP.net来做到这一点,但我没有使用ASP.net。只需要将电子邮件格式设置为html,并将其部分替换为应用程序中的其他信息即可。这是我发现应该可以使用的功能,但是我猜想只能在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"))
部分说“服务器”不能使用。有没有其他方法可以读取html模板并替换其中的项目然后发送到电子邮件地址?任何帮助表示赞赏!
您将需要获取可执行应用程序的本地路径,该本地路径可能未在ASP.NET服务器上下文中运行(因此无法使用Server.MapPath()
)。您正在寻找的将是这样的:
using (StreamReader reader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + "EmailTemplate.htm")))
这应尝试使用名为“ EmailTemplate.htm”的文件相对于可执行文件的位置(在同一文件夹中,在其旁边)打开流。
查看此SO问题以获取有关获取可执行文件夹路径的更多信息:Get current folder path