如何将电子邮件页面/模板的内容提取到 .NET 中的 MailMessage 正文中

发布于 2024-08-24 10:38:12 字数 389 浏览 5 评论 0原文

我有一个电子邮件模板,它只是一个普通的静态 HTML 页面。

我需要将该页面放入 MailMessage 的正文中。

通常我要做的只是创建一个 StringBuilder 并一遍又一遍地附加这些行。但这是一个很长的页面,我不想用单引号等替换引号。

那么有没有一种方法可以将 html 页面的内容拉入字符串中?如:

emailMessage.IsBodyHtml     = true;
emailMessage.Subject        = "Test Subject";
emailMessage.Body           = GetContentsFrom("emailtemplate.html");

谢谢!

I have an e-mail template which is just a plain, static HTML page.

I need to get that page into the Body of a MailMessage.

Normally what I would do is just create a StringBuilder and append the lines over and over. But it's kind of a long page, and I don't want to go through replacing the quotes with single quotes etc..

So is there a way to just pull in the contents of an html page into a string? Such as:

emailMessage.IsBodyHtml     = true;
emailMessage.Subject        = "Test Subject";
emailMessage.Body           = GetContentsFrom("emailtemplate.html");

Thanks!

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

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

发布评论

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

评论(3

往事随风而去 2024-08-31 10:38:12

您可以将 GetContentsFrom 替换为 File.ReadAllText 吗?

Could you replace GetContentsFrom with File.ReadAllText?

一百个冬季 2024-08-31 10:38:12

我认为这个例程可以解决问题:

private static string GetTemplate(string templateFullPath)
{
    string template;

    if (!File.Exists(templateFullPath))
        throw new ArgumentException("Template file does not exist: " + templateFullPath);

    using (StreamReader reader = new StreamReader(templateFullPath))
    {
        template = reader.ReadToEnd();
        reader.Close();
    }

    return template;
}

I think this routine would do the trick:

private static string GetTemplate(string templateFullPath)
{
    string template;

    if (!File.Exists(templateFullPath))
        throw new ArgumentException("Template file does not exist: " + templateFullPath);

    using (StreamReader reader = new StreamReader(templateFullPath))
    {
        template = reader.ReadToEnd();
        reader.Close();
    }

    return template;
}
无妨# 2024-08-31 10:38:12

该页面是否托管在某处?你可以这样做:

private string HtmlPageInToString()

{

WebRequest myRequest;
myRequest = WebRequest.Create(@"http://yoururlhere/");

myRequest.UseDefaultCredentials = true;

WebResponse myResponse = myRequest.GetResponse();

Stream ReceiveStream = myResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

StreamReader readStream = new StreamReader(ReceiveStream, encode);

return readStream.ReadToEnd();

}

Is the page being hosted somewhere? You could do this:

private string HtmlPageInToString()

{

WebRequest myRequest;
myRequest = WebRequest.Create(@"http://yoururlhere/");

myRequest.UseDefaultCredentials = true;

WebResponse myResponse = myRequest.GetResponse();

Stream ReceiveStream = myResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

StreamReader readStream = new StreamReader(ReceiveStream, encode);

return readStream.ReadToEnd();

}

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