[英]Convert MHTML to HTML using C#
我的任務是將 mHtml 嵌入到電子郵件正文中。 問題是 mhtml 不是普通的 html 文件,因此我無法將其直接嵌入到電子郵件中。
如何將 mhtml 轉換為 html 文件?
謝謝
我在此鏈接上找到了解決方案:
解決方案是在 MHTML 中提取編碼為 Base64 的 HTML。
var decoded_text = new StringBuilder();
using (var reader = new StreamReader(mhtFile))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (line != "Content-Transfer-Encoding: base64") continue;
reader.ReadLine(); //chew up the blank line
while ((line = reader.ReadLine()) != String.Empty)
if (line != null)
decoded_text.Append(
Encoding.UTF8.GetString(
Convert.FromBase64String(line)));
break;
}
}
當 html 中沒有變音符號字母(ěščřžýáíé - 例如捷克語變音符號或其他 2 字節字符)時,可接受的解決方案工作正常。 如果此類字符的第一個字節在變量“行”的末尾,第二個字節在下一個的開頭,則 html 結果中將顯示不可讀的字符。
var base64_text = new StringBuilder();
using (var reader = new StreamReader(mhtFile))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (line != "Content-Transfer-Encoding: base64") continue;
reader.ReadLine(); //chew up the blank line
while ((line = reader.ReadLine()) != String.Empty)
if (line != null)
base64_text.Append(line);
break;
}
return Encoding.UTF8.GetString(Convert.FromBase64String(base64_text.ToString()));
}
我在文本編輯器(記事本++)中從此頁面打開了 .mhtml,HTML 似乎在文件中,完好無損。 您必須向下滾動瀏覽所有 CSS。 我只是創建一些東西來從文件中提取 HTML 文本,而不是處理 base64 數據(如果某些東西不起作用,對我來說太混亂了)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.