public string ConvertWebControlToMHTString(string htmlString, mhtImageCollection MHTimages)
{
//Render WebControl to html
string html = htmlString; //If WebControl has images, make the html Word compatible
if ((MHTimages != null))
{
fixImageLocation(ref html, ref MHTimages);
} CDO.MessageClass msg = new CDO.MessageClass(); ADODB.Stream stm = null; System.IO.MemoryStream MS = null; CDO.IBodyPart iBp = default(CDO.IBodyPart); //Make a multipart mhtml document
CDO.IBodyPart mainBody = default(CDO.IBodyPart);
mainBody = msg;
mainBody.ContentMediaType = "multipart/related"; //Make the image parts of the document
if ((MHTimages != null))
{ mhtImage oMhtImage1 = default(mhtImage);
int icount = MHTimages.Count; for (int k = 1; k <= icount; k++)
{
mainBody.BodyParts.Add(k);
}
int j = 1;
foreach (mhtImage oMhtImage in MHTimages)
{
iBp = mainBody.BodyParts[j];
{
iBp.ContentMediaType = "image/" + oMhtImage.ImageFormat.ToString().ToLower();
iBp.ContentTransferEncoding = "base64"; //ContentLocation must be the same as in the html part to make them linked
iBp.Fields.Append("urn:schemas:mailheader:content-location", DataTypeEnum.adBSTR, 1000000, FieldAttributeEnum.adFldUnspecified, oMhtImage.ContentLocation);
iBp.Fields.Update();
iBp.Fields.Refresh();
}
try
{
MS = new System.IO.MemoryStream();
oMhtImage.Image.Save(MS, oMhtImage.ImageFormat);
byte[] bytearray = MS.ToArray();
stm = iBp.GetDecodedContentStream(); stm.Write(bytearray);
stm.Flush();
}
finally
{
MS.Close();
stm.Close();
}
j++;
}
} //Make the html part of the document
iBp = mainBody.BodyParts.Add(1);
//mainBody.BodyParts
iBp.ContentMediaType = "text/html";
//iBp.ContentTransferEncoding = "quoted-printable";
iBp.ContentTransferEncoding = "base64";
iBp.Charset = "gb2312";
//System.IO.Stream sstm = null;
//sstm = iBp.GetDecodedContentStream();
stm = iBp.GetDecodedContentStream();
//stm.Position = 2;
stm.Charset = "utf-8";
stm.Type = StreamTypeEnum.adTypeText;
//stm.Charset = "gb2312";
// MS = new System.IO.MemoryStream(); stm.WriteText(html, ADODB.StreamWriteEnum.adWriteChar); stm.Flush(); //stm.r
stm = mainBody.GetStream();
return stm.ReadText(stm.Size);
} stm.WriteText(html, ADODB.StreamWriteEnum.adWriteChar);改了段代码,但是假如中文有问题,原版vb的代码如下
http://www.codeproject.com/KB/aspnet/aspnethtml2mht.aspx
这里,如果html里面有中文的话,会在 return stm.ReadText(stm.Size);
这里输出乱码,据说是ADODB.Stream编码的问题,
没搞过啊,求高手帮忙解决
如:
<html><body><H1>测试功能</H1><br/><br/>测试功能</body></html>
打出的内容如下thread-index: AcqJPBcRTO514rokQpWFmrH855OB4QMIME-Version: 1.0
Content-Type: text/html;
charset"gb2312"
Content-Transfer-Encoding: 8bit
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579<html><body><H1>å®¡æ ¸ä¸ŠæŠ¥æµ‹è¯•åŠŸèƒ½</H1><br/><br/>å®¡æ ¸ä¸ŠæŠ¥æµ‹è¯•åŠŸèƒ½</body></html>
求高手帮解决
msg.MDNRequested = true;
msg.MimeFormatted = true;
msg.CreateMHTMLBody(url, CDO.CdoMHTMLFlags.cdoSuppressNone, string.Empty,
string.Empty);
ADODB.Stream stream = msg.GetStream();
stream.Charset = "utf-8";
string strText = stream.ReadText();