/// <summary> /// /// </summary> /// <param name="FilePath"></param> /// <returns></returns> public Message ResolveMailByLSoft(String FilePath) { Message oMsg = new Message(); ADODB.Stream stm = null; try { stm = new ADODB.Stream(); stm.Open(System.Reflection.Missing.Value, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, "", ""); stm.Type = ADODB.StreamTypeEnum.adTypeBinary; //二进制方式读入 stm.LoadFromFile(FilePath); //将EML读入数据流 oMsg.DataSource.OpenObject(stm, "_stream"); //将EML数据流载入到CDO.Message,要做解析的话,后面就可以了。 } catch (IOException) { } finally { stm.Close(); } return(oMsg); }
/// <summary> /// 解析邮件文件为邮件对象 /// </summary> /// <param name="FilePath"></param> /// <returns></returns> public Message ResolveMailByLSoft(String FilePath) { Message oMsg = new Message(); ADODB.Stream stm = null; //读取EML文件到CDO.MESSAGE,做分析的话,实际是用了下面的部分 try { stm = new ADODB.Stream(); stm.Open(System.Reflection.Missing.Value, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, "", ""); stm.Type = ADODB.StreamTypeEnum.adTypeBinary;//二进制方式读入 stm.LoadFromFile(FilePath); //将EML读入数据流 oMsg.DataSource.OpenObject(stm, "_stream"); //将EML数据流载入到CDO.Message,要做解析的话,后面就可以了。 } catch (IOException) { } finally { stm.Close(); } return oMsg; }
public CDOMessageConverter(string mailContent) { cdoMessage = new CDO.Message(); ADODB.Stream adoStream = cdoMessage.GetStream(); adoStream.Type = ADODB.StreamTypeEnum.adTypeText; adoStream.WriteText(mailContent.Trim(), ADODB.StreamWriteEnum.adWriteLine); adoStream.Flush(); adoStream.Close(); }
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 byte[] GetAttachmentContentFromIdx(int idx) { var attachments = mail.Attachments.OfType <CDO.IBodyPart>(); var attachment = attachments.Skip(idx).FirstOrDefault(); ADODB.Stream stm = attachment.GetDecodedContentStream(); // cast to COM IStream and load into byte array var comStream = (System.Runtime.InteropServices.ComTypes.IStream)stm; byte[] attachmentData = new byte[stm.Size]; comStream.Read(attachmentData, stm.Size, IntPtr.Zero); stm.Close(); return(attachmentData); }
/// <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); }
/// <summary> /// Read eml file into CDO message /// </summary> /// <param name="filepath">Path to eml file</param> /// <returns>CDO message</returns> private static CDO.Message ReadEml(string filepath) { var oMsg = new CDO.Message(); var stm = new ADODB.Stream(); stm.Open(System.Reflection.Missing.Value); stm.Type = ADODB.StreamTypeEnum.adTypeBinary; stm.LoadFromFile(filepath); oMsg.DataSource.OpenObject(stm, "_stream"); stm.Close(); return oMsg; }
//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; }