代码之家  ›  专栏  ›  技术社区  ›  Cory House

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

  •  0
  • Cory House  · 技术社区  · 16 年前

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

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

                Dim TextStream
            Set TextStream = FSO.OpenTextFile(Filepath, ForReading, False, TristateUseDefault)
    
            ' Read file in one hit
            Dim Contents
            GetTemplate = TextStream.ReadAll ' return file contents
    

    我错过了什么?

    3 回复  |  直到 16 年前
        1
  •  3
  •   Russ    16 年前

    这就是我所做的。。。

    我获取一个文本或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变量),它将包含所有必要的换行符和格式。

    虽然我的示例是C#,(对不起,我手头没有任何经典ASP代码),但是查找和替换值的概念将应用于经典ASP。

        2
  •  1
  •   angus    16 年前