Ali said:
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/tutorial...0-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.