I have a small application written in c# as a console app that I want to use to send an email. I was planning on storing the email inside an xml file along with other information that the message will need like a subject. However there seems to be a problem because the XML file doesnt like </br>
characters.
我有一个用c#编写的小应用程序作为我想用来发送电子邮件的控制台应用程序。我计划将电子邮件存储在xml文件中,以及消息需要的其他信息,如主题。但是,似乎存在问题,因为XML文件不喜欢 字符。
Im wondering what I should do in order to store a html email do I just have to keeo the body html in a seperate html file and then read each line into a StreamReader object?
我想知道我应该怎么做才能存储一个HTML电子邮件,我只需要在一个单独的html文件中保存正文html然后将每行读入StreamReader对象?
4 个解决方案
#1
13
The easiest way would be to store the HTML content in a CDATA section:
最简单的方法是将HTML内容存储在CDATA部分中:
<mail>
<subject>Test</subject>
<body>
<![CDATA[
<html>
...
</html>
]]>
</body>
</mail>
#2
3
You can use CDATA section in your XML - here you can read about it.
您可以在XML中使用CDATA部分 - 在这里您可以阅读它。
#3
3
Use a CDATA section, that will contain your email HTML code :
使用CDATA部分,其中包含您的电子邮件HTML代码:
<?xml version="1.0"?>
<myDocument>
<email>
<![CDATA[
<html>
<head><title>My title</title></head>
<body><p>Hello world</p></body>
</html>
]]>
</email>
</myDocument>
#4
3
You could store the HTML as CDATA within the XML.
您可以将HTML作为CDATA存储在XML中。
But looking at what you are trying to do, you may wish instead look at the System.Web.UI.WebControls.MailDefinition class, as it already contains a reasonable way of using mail templates.
但是看看你想要做什么,你可能希望看一下System.Web.UI.WebControls.MailDefinition类,因为它已经包含了使用邮件模板的合理方法。
The msdn documentation gears towards it being used in WinForms apps, but you can simply use a ListDictionary to fill the replacements.
msdn文档在WinForms应用程序中使用它,但您只需使用ListDictionary来填充替换。
Here is a simplistic example, to give an idea of how MailDefinition can be used, I won't go into to much detail, as it's a little outside of the scope of the original question.
这是一个简单的例子,为了解如何使用MailDefinition,我不会详细介绍,因为它有点超出原始问题的范围。
protected MailMessage GetNewUserMailMessage(string email, string username, string password, string loginUrl)
{
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.BodyFileName = "~/mailtemplates/newuser.txt";
ListDictionary replacements = new ListDictionary();
replacements.Add("<%username%>", username);
replacements.Add("<%password%>", password);
replacements.Add("<%loginUrl%>", loginUrl);
return mailDefinition.CreateMailMessage(email, replacements, this);
}
#1
13
The easiest way would be to store the HTML content in a CDATA section:
最简单的方法是将HTML内容存储在CDATA部分中:
<mail>
<subject>Test</subject>
<body>
<![CDATA[
<html>
...
</html>
]]>
</body>
</mail>
#2
3
You can use CDATA section in your XML - here you can read about it.
您可以在XML中使用CDATA部分 - 在这里您可以阅读它。
#3
3
Use a CDATA section, that will contain your email HTML code :
使用CDATA部分,其中包含您的电子邮件HTML代码:
<?xml version="1.0"?>
<myDocument>
<email>
<![CDATA[
<html>
<head><title>My title</title></head>
<body><p>Hello world</p></body>
</html>
]]>
</email>
</myDocument>
#4
3
You could store the HTML as CDATA within the XML.
您可以将HTML作为CDATA存储在XML中。
But looking at what you are trying to do, you may wish instead look at the System.Web.UI.WebControls.MailDefinition class, as it already contains a reasonable way of using mail templates.
但是看看你想要做什么,你可能希望看一下System.Web.UI.WebControls.MailDefinition类,因为它已经包含了使用邮件模板的合理方法。
The msdn documentation gears towards it being used in WinForms apps, but you can simply use a ListDictionary to fill the replacements.
msdn文档在WinForms应用程序中使用它,但您只需使用ListDictionary来填充替换。
Here is a simplistic example, to give an idea of how MailDefinition can be used, I won't go into to much detail, as it's a little outside of the scope of the original question.
这是一个简单的例子,为了解如何使用MailDefinition,我不会详细介绍,因为它有点超出原始问题的范围。
protected MailMessage GetNewUserMailMessage(string email, string username, string password, string loginUrl)
{
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.BodyFileName = "~/mailtemplates/newuser.txt";
ListDictionary replacements = new ListDictionary();
replacements.Add("<%username%>", username);
replacements.Add("<%password%>", password);
replacements.Add("<%loginUrl%>", loginUrl);
return mailDefinition.CreateMailMessage(email, replacements, this);
}