【发布时间】:2020-05-29 10:24:12
【问题描述】:
我已经搜索了好几天,试图弄清楚如何使用我的 C# Windows 窗体应用程序将 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 模板并替换其中的项目然后发送到电子邮件地址?任何帮助表示赞赏!
【问题讨论】:
-
这段代码没有发送电子邮件......它只是读取模板替换并返回 html 作为字符串......你是否尝试过一些明显的东西,比如用
@"O:\real\full\path\to\the\template.htm"替换Server.MapPath("~/EmailTemplate.htm") -
谢谢!
标签: c# winforms email smtp streamreader