我有一个纯文本文件,我需要在使用C#读取,操纵它一点,然后我需要通过电子邮件发送它。这很简单,但它还必须保持与原始状态相同的格式:
这是一个示例文件“mySample.txt”的摘录:
*****************************NB!!!!**********************************
*Please view http://www.sdfsdf.comsdfsdfsdf . *
*********************************************************************
*** DO NOT DELETE or ALTER ANY OF THE FOLLOWING TEXT ***
Company X PTY.
Lorem Ipsum Office
Last Change - 01 February 2008
APPLICATION TO ESTABLISH A COMMUNITY WITHIN
THE RESTIN DISTRICT OF THE IPSUM.
===================================================================
1. COMMUNITY and ACTION
Give the name of the community. This is the name that will be
used in tables and lists associating the community with the name
district and community forum. The community names that are
delegated by Lorem are at the district level
The Action field specifies whether this is a 'N'ew application, an
'U'pdate or a 'R' removal.
1a. Complete community name:**{0}**
1b. Action - [N]ew, [U]pdate, or [R]emoval :**{1}**如你所见,我在文件中有占位符{0}和{1},它将被一个自动化的过程替换。
在我的C#中,我使用流读取器将整个文件读取到一个StringBuilder对象中,然后使用StringBuilder.AppendFormat方法替换占位符。
问题是,当我将文本添加到电子邮件正文中并发送它时,格式最终看起来不同。它看起来像是在这个过程中删除了一堆空格或制表符。
private void Submit_Click(object sender, EventArgs e)
{
//create mail client
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(ConfigurationManager.AppSettings["SubmitToEmail"]);
message.Bcc.Add("xyz@test.com");
message.Subject = "Test Subject";
message.BodyEncoding = Encoding.ASCII;
message.IsBodyHtml = false;
message.Body = _PopulateForm(_GatherInput());//calls method to read the file and replace values
client.Send(message);
//cleanup
client = null;
message.Dispose();
message = null;
}有没有人有关于如何保持格式得体的想法?
谢谢,雅克
发布于 2012-02-07 17:32:14
答案似乎已经在.net和邮件消息对象中了。您必须创建一个alternateView对象并将其添加到其中,而不是将内容添加到Body属性中。这解决了我的问题。
你好,雅克
https://stackoverflow.com/questions/9130533
复制相似问题