简体   繁体   English

简化字符串中的电子邮件正文?

[英]Simplify e-mail body in string?

I'm just wondering about this one. 我只是想知道这一件事。 I'm creating an ASP.NET webform containing lots of textboxes etc. And I want to send an e-mail based on this stuff. 我正在创建一个包含大量文本框等的ASP.NET Web表单。我想基于此内容发送电子邮件。 And the e-mails body needs to go into a string. 电子邮件正文需要输入字符串。

And the string is supposed to contain HTML code, but the syntax changes because of the string. 并且该字符串应该包含HTML代码,但是语法会因该字符串而改变。 So how can I simplify this? 那么如何简化呢? Is there any software or something that lets me do this? 是否有任何软件或其他可以让我做到这一点的东西? Perhaps paste in some HTML code and then convert this to string format, ready to use with vb.net? 也许粘贴一些HTML代码,然后将其转换为字符串格式,准备与vb.net一起使用?

If I understand correctly, you are wanting to generate an HTML email from user input via textboxes. 如果我理解正确,则您希望通过文本框从用户输入生成HTML电子邮件。 If so, you can easily generate HTML using the HtmlTextWriter: 如果是这样,你可以使用的HtmlTextWriter容易生成HTML:

StringBuilder sb = new StringBuilder();
using(HtmlTextWriter writer = new HtmlTextWriter(new StringWriter(sb)))
{
    writer.WriteBeginTag("span");
    writer.WriteAttribute("style", "font-weight:bold;");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write(textboxName.Text);
    writer.WriteEndTag("span");
}
return sb.ToString();

The above will generate a string which looks like: 上面的代码将产生一个字符串,它看起来像:

<span style="font-weight:bold;">user input</span>

The HtmlTextWriter makes it very easy to ensure your markup is semantically correct and valid by mimicking the XML writers. 该HtmlTextWriter的使得它很容易,以确保您的标记是通过模仿XML编写者语义正确和有效的。

For converting a HTML formatted string back to plain text try this link, 要将HTML格式的字符串转换回纯文本,请尝试以下链接,

[ http://www.dreamincode.net/code/snippet1578.htm][Strip HTML from string using Regular Expressions] [ http://www.dreamincode.net/code/snippet1578.htm ] [使用正则表达式从字符串中剥离HTML]

This will only strip the tags and doesn't remove the html symbols like an ampersand but it will get you started. 这只会删除标签,不会删除与符号类似的html符号,但可以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM