如何使用电子邮件模板txt文件并保留格式?

时间:2008-11-26 15:15:01

标签: email asp-classic formatting

我正在使用预先格式化的文本文件作为电子邮件的模板。该文件在我想要的地方有换行符。我想使用此模板发送纯文本电子邮件,但是当我这样做时,我将丢失所有格式。删除换行符。

如何解析此文件并保留换行符?我不想使用<pre>标记,因为我想发送纯文本电子邮件。

我正在使用经典的ASP ReadAll方法将模板拉成字符串:

            Dim TextStream
        Set TextStream = FSO.OpenTextFile(Filepath, ForReading, False, TristateUseDefault)

        ' Read file in one hit
        Dim Contents
        GetTemplate = TextStream.ReadAll ' return file contents

我错过了什么?

3 个答案:

答案 0 :(得分:3)

这就是我所做的......

我接受一个文本或HTML文件(我将显示文本,因为它较小,但完全相同的代码适用),并且我将熟知的值放入文本文件中,以后我可以替换它。

- 开始文本文件

We've generated a new password for you at your request, you can use this new password with your username to log in to various sections of our site.

Username: ##UserName##
Temporary Password: ##Password##

To use this temporary password, please copy and paste it into the password box.

Please keep this email for your records.

- 结束文本文件

然后简单的问题是创建一个键/值对列表,包含要替换的文本,以及替换它的值。将文件作为字符串加载到内存中,并循环键/值对,替换文本值。

ListDictionary dictionary = new ListDictionary
                                            {
                                                {"##UserName##", user.BaseUser.UserName},
                                                {"##Password##", newPassword}
                                            };


            string fromResources = GetFromResources("forgotpasswordEmail.html");
            string textfromResources = GetFromResources("forgotpasswordEmail.txt");
            foreach (DictionaryEntry entry in dictionary)
            {
                fromResources = fromResources.Replace(entry.Key.ToString(), entry.Value.ToString());
                textfromResources = textfromResources.Replace(entry.Key.ToString(), entry.Value.ToString());
            }

然后您可以通过电子邮件发送文本(在本例中为textfromResources变量),它将包含所有必要的换行符和格式。

就像我说的那样,你可以用HTML文件或你想要的任何类型的文件做同样的事情。

虽然我的例子是在C#中,(我没有任何经典的ASP代码,对不起),查找和替换值的概念将适用于经典的ASP。

答案 1 :(得分:1)

您显示的代码不应删除任何换行符。问题可能出在电子邮件生成部分。你能展示那部分吗?

邮件的内容类型是:text / plain?

答案 2 :(得分:0)