From: Ali on 3 Jan 2010 07:20 Hi, I need to extract the images (attached) of a .EML file. Any help or ideas? Thanks in advance. (I'm programming in c#) Submitted via EggHeadCafe - Software Developer Portal of Choice Screenscrape Realtime Quotes with Regex and XML http://www.eggheadcafe.com/tutorials/aspnet/a242f7bc-6dfb-465c-8ff0-6f63b4fbf718/screenscrape-realtime-quo.aspx
From: Asaf Shelly on 3 Jan 2010 10:49 Hi, Try remaing the file from *.eml to *.mht (version of HTML that has all resources inline in hex format) Asaf
From: kndg on 3 Jan 2010 23:37 Ali Seraj wrote: > Hi, I need to extract the images (attached) of a .EML file. > Any help or ideas? > Thanks in advance. > (I'm programming in c#) > > > Submitted via EggHeadCafe - Software Developer Portal of Choice > Screenscrape Realtime Quotes with Regex and XML > http://www.eggheadcafe.com/tutorials/aspnet/a242f7bc-6dfb-465c-8ff0-6f63b4fbf718/screenscrape-realtime-quo.aspx Hi Ali, Upon examination of eml file, it can be done quite easily. Below is sample code (not tested/optimized) using System; using System.IO; using System.Text; public class AttachmentExtractor { static void Main(string[] args) { if (args.Length > 0) { string filename = args[0]; using (var reader = new StreamReader(filename)) { string line; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("--MEboundary-")) { ParseMimeSegment(reader); } } } } } private static void ParseMimeSegment(TextReader reader) { string line; string filename = String.Empty; while ((line = reader.ReadLine()) != null) { if (String.IsNullOrEmpty(line)) break; if (line.StartsWith("Content-Disposition: attachment;")) // found attachment { int filenameStart = line.IndexOf('"') + 1; filename = line.Substring(filenameStart, line.Length - filenameStart - 1); } } if (filename.Length > 0) { ExtractAttachment(reader, filename); } } private static void ExtractAttachment(TextReader reader, string filename) { string line; var content = new StringBuilder(); while ((line = reader.ReadLine()) != null) { if (String.IsNullOrEmpty(line)) break; content.Append(line); } if (content.Length > 0) { byte[] buffer = Convert.FromBase64String(content.ToString()); using (Stream writer = new FileStream(filename, FileMode.Create)) { writer.Write(buffer, 0, buffer.Length); } } } } Regards.
From: kndg on 4 Jan 2010 00:12 Hahaha... does not work. Here is version 2. using System; using System.IO; using System.Text; public class AttachmentExtractor { static void Main(string[] args) { if (args.Length > 0) { string filename = args[0]; using (var reader = new StreamReader(filename)) { string line; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("Content-Disposition: attachment;")) // found attachment { if (!line.Contains("filename")) { line = reader.ReadLine(); // Thunderbird: filename start at second line } int filenameStart = line.IndexOf('"') + 1; filename = line.Substring(filenameStart, line.Length - filenameStart - 1); while ((line = reader.ReadLine()) != null) { if (String.IsNullOrEmpty(line)) break; } ExtractAttachment(reader, filename); } } } } } private static void ExtractAttachment(TextReader reader, string filename) { string line; var content = new StringBuilder(); while ((line = reader.ReadLine()) != null) { if (String.IsNullOrEmpty(line)) break; content.Append(line); } if (content.Length > 0) { byte[] buffer = Convert.FromBase64String(content.ToString()); using (Stream writer = new FileStream(filename, FileMode.Create)) { writer.Write(buffer, 0, buffer.Length); } } } }
From: kndg on 4 Jan 2010 01:40 Version 3: Support for embedded image using System; using System.IO; using System.Text; public class AttachmentExtractor { private static int imageCount; static void Main(string[] args) { if (args.Length > 0) { StreamReader reader = null; try { reader = new StreamReader(args[0]); string line; while ((line = reader.ReadLine()) != null) { if (line.ToLower().StartsWith("content-disposition: attachment;")) // found attachment { ExtractContent(reader, GetAttachment(reader, line)); } if (line.ToLower().StartsWith("content-type: image/")) // found embedded image { ExtractContent(reader, GetImage(reader, line)); } } } catch (IOException) { Console.WriteLine("Unable to open file!"); } finally { if (reader != null) reader.Close(); } } } private static string GetAttachment(TextReader reader, string line) { if (!line.Contains("filename")) { line = reader.ReadLine(); // Thunderbird: filename start at second line } return GetFilename(reader, line); } private static string GetImage(TextReader reader, string line) { if (!line.Contains("name")) { line = reader.ReadLine(); // Thunderbird: filename start at second line } if (!line.Contains("name")) // embedded image does not have name { AdvanceToEmptyLine(reader); return "image" + imageCount++ + ".jpg"; // default to jpeg } return GetFilename(reader, line); } private static string GetFilename(TextReader reader, string line) { string filename; int filenameStart = line.IndexOf('"') + 1; if (filenameStart > 0) { filename = line.Substring(filenameStart, line.Length - filenameStart - 1); } else // filename does not have quote { filenameStart = line.IndexOf('=') + 1; filename = line.Substring(filenameStart, line.Length - filenameStart); } AdvanceToEmptyLine(reader); return filename; } private static void AdvanceToEmptyLine(TextReader reader) { string line; while ((line = reader.ReadLine()) != null) { if (String.IsNullOrEmpty(line)) break; } } private static void ExtractContent(TextReader reader, string filename) { string line; var content = new StringBuilder(); while ((line = reader.ReadLine()) != null) { if (String.IsNullOrEmpty(line)) break; content.Append(line); } if (content.Length > 0) { byte[] buffer = Convert.FromBase64String(content.ToString()); using (Stream writer = new FileStream(filename, FileMode.Create)) { writer.Write(buffer, 0, buffer.Length); } } } }
|
Pages: 1 Prev: c# and hyper access Next: dotnet framework check and install |