我有一個純文本文件,需要使用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("[email protected]");
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;
}
任何人有任何想法如何保持格式的機智?
感謝, 雅克
純文本電子郵件不支持選項卡。如果您的原始文件包含選項卡,請將其展開爲空格。有一個命令可以在Visual Studio編輯器中執行此操作。 – arx 2012-02-03 14:56:38
你能比我說「看起來不一樣」嗎?如果你在十六進制編輯器中打開它,確切地說缺少哪些字節? – 2012-02-03 14:58:01
@arx命令是編輯 - >高級 - >解除選定的行。它必須在原始文本文件上完成。 – Alexandre 2012-02-03 14:59:11