ASP.NET 将 .htm 文件作为电子邮件模板发送

发布于 2024-09-16 10:28:50 字数 62 浏览 3 评论 0原文

您好,您可以使用 mailMessage 类发送 .htm 文件作为 html 模板吗?我似乎不知道该怎么做?

Hi can you send a .htm file as the html template using the mailMessage class? I cant seem to work out how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自←快乐 2024-09-23 10:28:50

假设您已经创建了一个带有标记的 html 模板(例如 [[[FROM]]]),那么您需要使用 StringBuilder。例如:

public static string GetEmailTemplateContent(string path)
{
    string emailHtmlTemplate = "";
    StreamReader rdr = null;
    try
    {
        rdr = new StreamReader(path);
        emailHtmlTemplate = rdr.ReadToEnd();
    }
    catch (IOException ex)
    {
        throw new Exception(ex.Message.ToString());
    }
    finally
    {
        if (rdr != null)
        {
            rdr.Close();
            rdr.Dispose();
        }
    }
    return emailHtmlTemplate;
}

然后,您只需替换标记并将电子邮件正文填充到更改后的模板即可。

string emailTemplate = GetEmailTemplateContent("C:\\somepath");
emailTemplate = emailTemplate.Replace("[[[FROM]]]", from);
MailMessage email = new MailMessage();
email.Body = emailText;

Assuming you have created an html template that has markers (e.g. [[[FROM]]]), you then need to use StringBuilder. For example:

public static string GetEmailTemplateContent(string path)
{
    string emailHtmlTemplate = "";
    StreamReader rdr = null;
    try
    {
        rdr = new StreamReader(path);
        emailHtmlTemplate = rdr.ReadToEnd();
    }
    catch (IOException ex)
    {
        throw new Exception(ex.Message.ToString());
    }
    finally
    {
        if (rdr != null)
        {
            rdr.Close();
            rdr.Dispose();
        }
    }
    return emailHtmlTemplate;
}

And then, you just need to replace the markers and fill the email body to the changed template.

string emailTemplate = GetEmailTemplateContent("C:\\somepath");
emailTemplate = emailTemplate.Replace("[[[FROM]]]", from);
MailMessage email = new MailMessage();
email.Body = emailText;
友欢 2024-09-23 10:28:50

我建议您使用 String 或 StringBuilder 类并创建您自己的模板子系统。我的意思是,只需将模板内容分配给字符串,并用实际值替换出现的标记。

如果您想要一个示例,请查看 Drupal 如何提供 !username 等变量。如果您需要代码示例,请随时在此处发表评论。

希望这有帮助,

I recommend you to use the String or StringBuilder classes and make your own template subsystem. I mean, just assign the template content to the String, and replace occurrences of tokens by real values.

If you want an example, please see how Drupal offers variables like !username, and so on. If you need code examples, please don't you hesitate posting a comment here.

Hope this helps,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文