private void PopulateEml(string emlFilePath) { try { CDO.Message msg = new CDO.MessageClass(); ADODB.Stream stream = new ADODB.StreamClass(); stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty); stream.LoadFromFile(emlFilePath); stream.Flush(); msg.DataSource.OpenObject(stream, "_Stream"); msg.DataSource.Save(); stream.Close(); _emlFile = new Eml { From = msg.From, To = msg.To, CC = msg.CC, Body = string.IsNullOrEmpty(msg.HTMLBody) ? msg.TextBody : msg.HTMLBody, Subject = msg.Subject }; } catch (Exception ex) { } }
public static ADODB.Stream OpenFileStream(FileInfo fileInfo, int retry) { var delay = 0; for (var i = 0; i < retry; i++) { try { ADODB.Stream stream = new ADODB.StreamClass(); stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty); stream.LoadFromFile(fileInfo.FullName); stream.Flush(); return stream; } catch (FileNotFoundException) { throw; } catch (IOException) { delay += 100; if (i == retry) throw; } Thread.Sleep(delay); } //We will never get here throw new IOException("Unable to open file - " + fileInfo.FullName); }
protected CDO.Message ReadMessage(String emlFileName) { CDO.Message msg = new CDO.MessageClass(); ADODB.Stream stream = new ADODB.StreamClass(); stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty); stream.LoadFromFile(emlFileName); stream.Flush(); msg.DataSource.OpenObject(stream, "_Stream"); msg.DataSource.Save(); return(msg); }
internal static CDO.Message ParseEmlFile(string file) { // Check file exists and is readable if (!File.Exists(file)) { return(null); } try { File.Open(file, FileMode.Open, FileAccess.Read).Dispose(); } catch (IOException) { return(null); } CDO.Message msg = new CDO.MessageClass(); ADODB.Stream stream = new ADODB.StreamClass(); stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, string.Empty, string.Empty); stream.LoadFromFile(file); stream.Flush(); msg.DataSource.OpenObject(stream, "_Stream"); msg.DataSource.Save(); stream.Close(); if (string.IsNullOrWhiteSpace(msg.Sender) && string.IsNullOrWhiteSpace(msg.To) && string.IsNullOrWhiteSpace(msg.From) && string.IsNullOrWhiteSpace(msg.TextBody) && string.IsNullOrWhiteSpace(msg.HTMLBody) && string.IsNullOrWhiteSpace(msg.From) ) { return(null); } return(msg); }
private static CDO.Message ReadMessageUnCached(String emlFileName) { for (int i = 0; i < maxRetries; i++) { try { CDO.Message msg = new CDO.MessageClass(); ADODB.Stream stream = new ADODB.StreamClass(); stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty); stream.LoadFromFile(emlFileName); stream.Flush(); msg.DataSource.OpenObject(stream, "_Stream"); msg.DataSource.Save(); return msg; } catch (COMException ex) { if (i + 1 >= maxRetries || ex.HResult != -2146825286) { // Rethrow last time or if it isn't a file lock problem. throw; } } catch (IOException ex) { if (i + 1 >= maxRetries || ex.HResult != -2147024864) { // Rethrow last time or if it isn't a file lock problem. throw; } } System.Threading.Thread.Sleep(100); } throw new InvalidOperationException("Should not arrive here."); }
public static CDO.Message LoadMessage(string emlFilePath) { try { CDO.Message msg = new CDO.MessageClass(); ADODB.Stream stream = new ADODB.StreamClass(); stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty); //http://stackoverflow.com/questions/936422/recommendations-on-parsing-eml-files-in-c-sharp //stream.Type = ADODB.StreamTypeEnum.adTypeBinary; // Don't parse UTF8 byte headers stream.LoadFromFile(emlFilePath); stream.Flush(); msg.DataSource.OpenObject(stream, "_Stream"); msg.DataSource.Save(); return msg; } catch (Exception ex) { throw new ApplicationException(string.Format("Could not load message '{0}'. {1}", emlFilePath, ex.Message), innerException: ex); } }
public static CDO.Message LoadMessage(string emlFilePath) { try { CDO.Message msg = new CDO.MessageClass(); ADODB.Stream stream = new ADODB.StreamClass(); stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty); //http://stackoverflow.com/questions/936422/recommendations-on-parsing-eml-files-in-c-sharp //stream.Type = ADODB.StreamTypeEnum.adTypeBinary; // Don't parse UTF8 byte headers stream.LoadFromFile(emlFilePath); stream.Flush(); msg.DataSource.OpenObject(stream, "_Stream"); msg.DataSource.Save(); return(msg); } catch (Exception ex) { throw new ApplicationException(string.Format("Could not load message '{0}'. {1}", emlFilePath, ex.Message), innerException: ex); } }
private static CDO.Message ReadMessageUnCached(String emlFileName) { for (int i = 0; i < maxRetries; i++) { try { CDO.Message msg = new CDO.MessageClass(); ADODB.Stream stream = new ADODB.StreamClass(); stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty); stream.LoadFromFile(emlFileName); stream.Flush(); msg.DataSource.OpenObject(stream, "_Stream"); msg.DataSource.Save(); return(msg); } catch (COMException ex) { if (i + 1 >= maxRetries || ex.HResult != -2146825286) { // Rethrow last time or if it isn't a file lock problem. throw; } } catch (IOException ex) { if (i + 1 >= maxRetries || ex.HResult != -2147024864) { // Rethrow last time or if it isn't a file lock problem. throw; } } System.Threading.Thread.Sleep(100); } throw new InvalidOperationException("Should not arrive here."); }
//SaveToFile public bool SaveToFile(object Stream, object FilePath) { Application.DoEvents(); // Try ADODB.StreamClass objStream = new ADODB.StreamClass(); //objStream = Interaction.CreateObject("ADODB.Stream"); objStream.Type = ADODB.StreamTypeEnum.adTypeBinary; objStream.Open(); objStream.Write(Stream); objStream.SaveToFile(FilePath.ToString(), ADODB.SaveOptionsEnum.adSaveCreateOverWrite); objStream.Close(); objStream = null; if (Information.Err().Number != 0) { return(false); } else { return(true); } }
static void Main(string[] args) { string filename = @"C:\Users\User1\Desktop\a.eml"; //string content = File.ReadAllText(filename); //dynamic msg = new Message(); //msg.DataSource.Open(filename); //dynamic msg = Activator.CreateInstance(Type.GetTypeFromProgID("CDO.Message")); //msg.Open(filename); //dynamic stream = Activator.CreateInstance(Type.GetTypeFromProgID("ADODB.Stream")); //stream.Open(); //stream.LoadFromFile(filename); //stream.Position = 0; //dynamic msg = Activator.CreateInstance(Type.GetTypeFromProgID("CDO.Message")); //msg.DataSource.OpenObject(stream, "_Stream"); //msg.Attachments.DeleteAll(); //msg.DataSource.SaveToObject(stream, "_Stream"); ////msg.DataSource.Save(); //stream.Position = 0; //stream.SaveToFile(filename, adSaveCreateOverWrite); //stream.Close(); ADODB.StreamClass stream = new ADODB.StreamClass(); stream.Open(); stream.LoadFromFile(filename); CDO.MessageClass msg = new CDO.MessageClass(); msg.DataSource.OpenObject(stream, "_Stream"); msg.Attachments.DeleteAll(); msg.DataSource.SaveToObject(stream, "_Stream"); stream.SaveToFile(filename, ADODB.SaveOptionsEnum.adSaveCreateOverWrite); stream.Close(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(msg); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(stream); }