Extract images from EML file in C#

A

Asaf Shelly

Hi,

Try remaing the file from *.eml to *.mht (version of HTML that has all
resources inline in hex format)

Asaf
 
K

kndg

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.
 
K

kndg

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);
}
}
}
}
 
K

kndg

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);
}
}
}
}
 
K

keith master

hi kndg,

i'm in need of extracting embedded images in EML files, and this post is what i have been looking for. is it possible that i could get the full source code for this? in above code you posted doesnot have the method ExtractContent(), kindly help me to get the full source code to this email (e-mail address removed)

i appreciate your help
Hi, I need to extract the images (attached) of a .EML file.

Any help or ideas?

Thanks in advance.

(I'm programming in c#)
On Sunday, January 03, 2010 10:49 AM Asaf Shelly wrote:
Hi,

Try remaing the file from *.eml to *.mht (version of HTML that has all
resources inline in hex format)

Asaf
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.
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);
}
}
}
}
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)
{
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top