private void SaveBodyPart(ADODB.Stream aStream, string sFileName) { //Dim sr As StreamWriter //Dim sFileBody As String string sDataPath = "c:\\"; string sUseFilename = null; SaveFileDialog saveFileDialog1 = default(SaveFileDialog); saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.InitialDirectory = sDataPath; saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = false; saveFileDialog1.FileName = sFileName; if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { sUseFilename = saveFileDialog1.FileName; saveFileDialog1 = null; try { Cursor = Cursors.WaitCursor; aStream.SaveToFile(sUseFilename); Cursor = Cursors.Default; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Cannot write to file.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }
static void Main(string[] args) { #region 获取包含所有日志链接的HTML文本 //string baseQueryUrl = "http://www.douban.com/people/oranjeruud/notes?start="; //for (int i = 0; i < 23; i++) //{ // string queryUrl = baseQueryUrl + (i * 10); // HttpWebRequest httpWebRequest = HttpWebRequest.CreateHttp(queryUrl); // httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95"; // HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//取得响应 // if (httpWebResponse.StatusCode.Equals(HttpStatusCode.OK))//若响应状态码为200,说明成功,可以分析得到的数据 // { // StreamReader reader = new StreamReader(new BufferedStream(httpWebResponse.GetResponseStream(), 4 * 200 * 1024)); // GetBlogLink(reader.ReadToEnd()); // reader.Close(); // } // Thread.Sleep(3000); //} #endregion #region 根据获得的日志链接获取日志网页和mht文件 Message msg = new CDO.MessageClass(); CDO.Configuration c = new CDO.ConfigurationClass(); msg.Configuration = c; StreamReader reader = new StreamReader("links.txt"); int i = 0; while (!reader.EndOfStream) { i++; string link = reader.ReadLine(); HttpWebRequest httpWebRequest = HttpWebRequest.CreateHttp(link); httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95"; HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); //取得响应 if (httpWebResponse.StatusCode.Equals(HttpStatusCode.OK)) //若响应状态码为200,说明成功,可以分析得到的数据 { StreamReader httpReader = new StreamReader(new BufferedStream(httpWebResponse.GetResponseStream(), 4 * 200 * 1024)); string htmlContent = httpReader.ReadToEnd(); httpReader.Close(); string title = GetBlogTitle(htmlContent); #region 保存为html文件 StreamWriter writer = new StreamWriter(i + ".html"); writer.Write(htmlContent); writer.Close(); #endregion #region 保存为mht文件 //msg.HTMLBody = htmlContent; msg.CreateMHTMLBody(link, CDO.CdoMHTMLFlags.cdoSuppressNone, "", ""); ADODB.Stream stream = msg.GetStream(); stream.SaveToFile(title + ".mht", ADODB.SaveOptionsEnum.adSaveCreateOverWrite); #endregion } if (i == 1) { break; } } reader.Close(); #endregion }
public static void SaveToMHT(string strOutputFile, string data) { string tmpFileName = string.Format("Export{0}.html", DateTime.Now.Ticks); string strPath = Path.GetTempPath(); if (!Directory.Exists(strPath)) { Directory.CreateDirectory(strPath); } string strFilePath = Path.Combine(strPath, tmpFileName); //Server.MapPath(string.Format("./{0}", m_tmpFile)); using (StreamWriter sw = File.CreateText(strFilePath)) { sw.Write(data); sw.Close(); } string url = "file://" + strFilePath; // GetBaseURL() + m_tmpFile; CDO.MessageClass message = new CDO.MessageClass(); ADODB.Stream stream = null; try { message.CreateMHTMLBody(url, CDO.CdoMHTMLFlags.cdoSuppressNone, "", ""); stream = message.GetStream(); stream.SaveToFile(strOutputFile, ADODB.SaveOptionsEnum.adSaveCreateOverWrite); } catch (Exception) { using (FileStream fs = File.Create(strOutputFile)) { using (BinaryWriter sw = new BinaryWriter(fs)) { sw.Write(new byte[] { 0xFF, 0xFE }); sw.Write(System.Text.Encoding.Unicode.GetBytes(data)); //.Unicode. strHTML.ToCharArray()); //Write(strHTML); sw.Close(); } } } finally { if (stream != null) { stream.Close(); } } if (File.Exists(strFilePath)) { File.Delete(strFilePath); } }
public static byte[] ToByteArray(this ADODB.Stream stream) { byte[] data; var tempSubFolderPath = TempSubfolder(); try { var tempFilePath = Path.Combine(tempSubFolderPath, "stream.tmp"); // ADODB cannot convert streams to .net byte arrays, but this works stream.SaveToFile(tempFilePath); data = File.ReadAllBytes(tempFilePath); } finally { new DirectoryInfo(tempSubFolderPath).Delete(recursive: true); } return(data); }
//LoadMessageEnvelopeFields() //lvAttachments // private void LoadAttachemntsFields() //{ // ListViewItem aListItem = default(ListViewItem); // lvAttachments.Items.Clear(); // lvAttachments.View = View.Details; // // Set the view to show details. // lvAttachments.GridLines = true; // // Display grid lines. // //Dim sValue As String // foreach (CDO.IBodyPart iBP in objCDOMsg.Attachments) // { // aListItem = new ListViewItem(iBP.FileName); // if (iBP.ContentMediaType != null) { // aListItem.SubItems.Add(iBP.ContentMediaType); // } else { // aListItem.SubItems.Add(""); // } // if (iBP.ContentTransferEncoding != null) { // aListItem.SubItems.Add(iBP.ContentTransferEncoding); // } else { // aListItem.SubItems.Add(""); // } // if (iBP.Charset != null) { // aListItem.SubItems.Add(iBP.Charset); // } else { // aListItem.SubItems.Add(""); // } // if (iBP.ContentClass != null) { // aListItem.SubItems.Add(iBP.ContentClass); // } else { // aListItem.SubItems.Add(""); // } // if (iBP.ContentClass != null) { // aListItem.SubItems.Add(iBP.ContentClassName); // } else { // aListItem.SubItems.Add(""); // } // lvAttachments.Items.AddRange(new ListViewItem[] { aListItem }); // aListItem = null; // // Marshal.ReleaseComObject(iBP); // } //} private void ExtractBodyPart(CDO.IBodyPart iBP) { string sText = null; ADODB.Stream aStream = default(ADODB.Stream); string sWebView = string.Empty; try { if (iBP.FileName.Trim().Length != 0) { //aStream = iBP.GetEncodedContentStream(); string sFoldePath = Path.GetTempPath(); string sFileName = iBP.FileName; string sFile = Path.Combine(sFoldePath, sFileName); iBP.SaveToFile(sFile); this.wbView.Navigate(sFile); } else { string sExtension = string.Empty; if (iBP.ContentMediaType == "text/html") { sExtension = "html"; } if (iBP.ContentMediaType == "text/plain") { sExtension = "txt"; } if (iBP.ContentMediaType == "text/xml") { sExtension = "xml"; } if (sExtension != string.Empty) { aStream = iBP.GetDecodedContentStream(); string sFoldePath = Path.GetTempPath(); string sFileName = Path.GetRandomFileName(); if (sExtension != string.Empty) { sFileName += ("." + sExtension); } string sFile = Path.Combine(sFoldePath, sFileName); aStream.SaveToFile(sFile); //SaveBodyPart(aStream, sFile); this.wbView.Navigate(sFile); } else { this.wbView.DocumentText = ""; } } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error bulding view for Browser view.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } // Get Encoded Stream try { aStream = iBP.GetEncodedContentStream(); sText = aStream.ReadText(); aStream.Close(); txtEncoded.Text = sText; txtStream.BackColor = Color.White; // Marshal.ReleaseComObject(aStream); } catch (Exception ex) { string x = ex.Message.ToString(); txtEncoded.Text = ""; txtStream.BackColor = Color.Gray; } // Get Decoded Stream try { aStream = iBP.GetDecodedContentStream(); sText = aStream.ReadText(); aStream.Close(); txtDecoded.Text = sText; txtStream.BackColor = Color.White; // Marshal.ReleaseComObject(aStream); } catch (Exception ex) { string x = ex.Message.ToString(); txtDecoded.Text = ""; txtStream.BackColor = Color.Gray; } // Get Stream try { aStream = iBP.GetStream(); sText = aStream.ReadText(); aStream.Close(); txtStream.Text = sText; txtStream.BackColor = Color.White; //Marshal.ReleaseComObject(aStream); } catch (Exception ex) { string x = ex.Message.ToString(); txtStream.Text = ""; txtStream.BackColor = Color.Gray; } ListViewItem aListItem = default(ListViewItem); ListView1.Items.Clear(); ListView1.View = View.Details; // Set the view to show details. ListView1.GridLines = true; // Display grid lines. string sValue = null; // Extract Bodypart Fields foreach (ADODB.Field objField in iBP.Fields) { aListItem = new ListViewItem(objField.Name); //aListItem.Tag = anInstance.href try { sValue = objField.ToString(); sValue = objField.Value as string; aListItem.SubItems.Add(sValue); } catch (Exception ex) { string x = ex.Message.ToString(); sValue = "(Cannot Extract - may not be set)"; aListItem.SubItems.Add(sValue); } ListView1.Items.AddRange(new ListViewItem[] { aListItem }); aListItem = null; //Marshal.ReleaseComObject(objField); } // ---- Body Part Properties -------------------------------- ListView2.Items.Clear(); ListView2.View = View.Details; // Set the view to show details. ListView2.GridLines = true; // Display grid lines. if (iBP.Charset != null) { AddLvItem(ref ListView2, "Charset", iBP.Charset); } if (iBP.ContentClass != null) { AddLvItem(ref ListView2, "ContentClass", iBP.ContentClass); } if (iBP.ContentMediaType != null) { AddLvItem(ref ListView2, "ContentMediaType", iBP.ContentMediaType); } if (iBP.ContentTransferEncoding != null) { AddLvItem(ref ListView2, "ContentTransferEncoding", iBP.ContentTransferEncoding); } if (iBP.FileName != null) { AddLvItem(ref ListView2, "FileName", iBP.FileName); } AddLvItem(ref ListView2, "BodyParts.Count", iBP.BodyParts.Count); aStream = null; }