/// <summary> /// 专门针对办公网的下载web方法 /// </summary> /// <param name="url">url是要保存的网页地址</param> /// <param name="filePath">filePath是保存到的文件路径</param> /// <returns></returns> public static bool SaveOaWebPageToMHTFile(string url, string filePath) { bool result = false; CDO.Message msg = new CDO.Message(); ADODB.Stream stm = null; msg.Configuration = new CDO.Configuration(); try { msg.MimeFormatted = true; msg.CreateMHTMLBody(url, CDO.CdoMHTMLFlags.cdoSuppressNone, "", ""); if (url.Substring(0, cConfig.strOaURL.Length) == cConfig.strOaURL) { stm = msg.GetStream(); stm.Charset = "GB2312"; using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { string s = Regex.Replace(stm.ReadText(), "<img width=\\\"100%\\\" height=\\\"100\\\"(.[^>]*)>", "", RegexOptions.IgnoreCase); s = Regex.Replace(s, "<P(.[^>]*)>", "<P>", RegexOptions.IgnoreCase); s = Regex.Replace(s, "<a(.[^>]*)javascript:window.close(.[^>]*)><img src=(.[^>]*)>", "", RegexOptions.IgnoreCase); s = Regex.Replace(s, "<table width=\\\"98%\\\"(.[^>]*)>", "", RegexOptions.IgnoreCase); byte[] array = Encoding.Default.GetBytes(s); fs.Write(array, 0, array.Length); //fs.Close(); } stm.Close(); } else { msg.GetStream().SaveToFile(filePath, ADODB.SaveOptionsEnum.adSaveCreateOverWrite); } msg = null; result = true; } catch //(Exception ex) { } finally { //cleanup here } return(result); }
//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; }