private void saveBodypartToFileToolStripMenuItem_Click(object sender, EventArgs e) { if ((TreeView1.SelectedNode != null)) { CDO.IBodyPart aBP = default(CDO.IBodyPart); aBP = (CDO.IBodyPart)TreeView1.SelectedNode.Tag; //Dim aStream As ADODB.Stream string sFilename = null; string sUseFilename = ""; string sDatapath = "c:\\"; SaveFileDialog saveFileDialog1 = default(SaveFileDialog); saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.InitialDirectory = sDatapath; saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = false; sFilename = aBP.FileName; if (string.IsNullOrEmpty(sFilename)) { sFilename = "BodyPart (" + TreeView1.SelectedNode.Text + ").txt"; } else { sFilename = "BodyPart - " + sFilename; } saveFileDialog1.FileName = sFilename; if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { sUseFilename = saveFileDialog1.FileName; saveFileDialog1 = null; try { Cursor = Cursors.WaitCursor; aBP.SaveToFile(sUseFilename); Cursor = Cursors.Default; } catch (Exception ex) { MessageBox.Show("Error Saving Bodypart " + ex.ToString()); } } //Marshal.ReleaseComObject(aBP); } Cursor = Cursors.Default; }
public static string GetString(this CDO.IBodyPart bodypart) { var bodyBytes = bodypart.GetDecodedContentStream().ToByteArray(); var bodyPartCharset = Encoding.GetEncoding(bodypart.Charset); var byteArrayEncoding = Encoding.Unicode; // all UTF-x formats are saved in UTF-16 by ADODB if (bodyPartCharset.IsSingleByte) { byteArrayEncoding = bodyPartCharset; // US-ASCII } var result = byteArrayEncoding.GetString(bodyBytes); return(result); }
private void saveBodypartStreamToFileToolStripMenuItem_Click(object sender, EventArgs e) { if ((TreeView1.SelectedNode != null)) { CDO.IBodyPart aBP = default(CDO.IBodyPart); Cursor = Cursors.WaitCursor; aBP = (CDO.IBodyPart)TreeView1.SelectedNode.Tag; ADODB.Stream aStream = default(ADODB.Stream); string sFilename = null; bool bGotStream = false; aStream = new ADODB.Stream(); try { aStream = aBP.GetStream(); bGotStream = true; } catch (Exception ex) { MessageBox.Show("Error Extracting stream: " + ex.Message); } if (bGotStream == true) { sFilename = aBP.FileName; if (string.IsNullOrEmpty(sFilename)) { sFilename = "BodyPartStream (" + TreeView1.SelectedNode.Text + ").txt"; } else { sFilename = "BodyPartStream - " + sFilename; } Cursor = Cursors.WaitCursor; SaveBodyPart(aStream, sFilename); Cursor = Cursors.Default; } //Marshal.ReleaseComObject(aBP); //Marshal.ReleaseComObject(aStream); Cursor = Cursors.Default; } }
public static byte[] ToByteArray(this CDO.IBodyPart attachment) { byte[] data; var tempSubFolderPath = TempSubfolder(); try { var tempFilePath = Path.Combine(tempSubFolderPath, "attachment.tmp"); // ADODB cannot convert streams to .net byte arrays, but this works attachment.SaveToFile(tempFilePath); data = File.ReadAllBytes(tempFilePath); } finally { new DirectoryInfo(tempSubFolderPath).Delete(recursive: true); } return(data); }
private bool AddNode(CDO.IBodyPart aBodyPart, ref System.Windows.Forms.TreeNode pNode, ref System.Windows.Forms.TreeNode aNewNode, int iCount) { bool bReturn = false; aNewNode = new System.Windows.Forms.TreeNode(); string sText = ""; if (aBodyPart.FileName.Trim().Length != 0) { sText = "(" + aBodyPart.FileName.ToString() + ")"; } aNewNode.Text = (sText + "" + pNode.Text + "." + iCount.ToString() + ""); aNewNode.Tag = aBodyPart; aNewNode.ImageIndex = 0; aNewNode.SelectedImageIndex = 0; pNode.Nodes.Add(aNewNode); return(bReturn); }
private void TreeView1_AfterSelect_1(object sender, TreeViewEventArgs e) { try { if (bLoaded == true) { Cursor = Cursors.WaitCursor; if ((e.Node.Tag != null)) { CDO.IBodyPart aBP = null; if (e.Node.Tag != null) { aBP = (CDO.IBodyPart)e.Node.Tag; } if (aBP == null) { ExtractBodyPart(objCDOMsg.BodyPart); // Messag Body } else { ExtractBodyPart(aBP); // Body Part } // Marshal.ReleaseComObject(aBP); } else { StatusBar1.Panels[0].Text = " "; } Cursor = Cursors.Default; } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
//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; }