0

We have a forum solution which allows people to submit posts via email. Currently, as the mail is parsed, images are just added as attachments to the post. What we would like to do is to parse the email and take embedded images from the mail and turn them into inline images in the output HTML. We need to support any Email client Outlook, Hotmail, Gmail etc

Outlook original Email:

<img id="Picture_x0020_1" src="cid:image001.jpg@01CD172C.038D3C80">

The desired result is that we save out the attachment and have the src as

<img id="Picture_x0020_1" src="http://www.site.com/default.aspx?action=ViewAttachment&paid=594">

I am aware that we can get image through something like: .NET How to extract embedded image from email message?

Do we need to crack open the RegEx or are there libraries that simplify this? I am sure that we aren't the only people who want to render an Email as HTML

Community
  • 1
  • 1
Phill Duffy
  • 2,805
  • 3
  • 33
  • 45
  • I don't understand what the problem is. It's about parsing the original mesasge? It's about exposing the image in the URL? – JotaBe Apr 10 '12 at 14:28

1 Answers1

1

You certainly could go down the road of writing code to extract the embedded images, and modifying the body yourself. It would probably end up being a lot of work tho.

We use the EasyMail.net from http://www.quiksoft.com/

Here's enough to get you started:

private POP3 pop3 = new POP3();
pop3.Connect(pop3Address, port);
var memoryStream = new MemoryStream();
pop3.DownloadMessage(position, memoryStream);
var email = new EmailMessage(memoryStream)
var streamView = new HTMLStreamView(email, urlPrefix);
string body = streamView.Body;
int counter = 0;

foreach (Stream stream in streamView.Streams)
{
    if (counter != 0)
    {
        stream.Position = 0;
        byte[] embeddedObjectBytes = new byte[(int)stream.Length];
        stream.Read(embeddedObjectBytes, 0, (int)stream.Length);

        string urlAndCounter = urlPrefix + counter.ToString();
        string uniqueUrlAndCounter = GetUniqueUrl(urlAndCounter);
        if (body.Contains(urlAndCounter + "\""))
        {
          body = body.Replace(urlAndCounter + "\"", uniqueUrlAndCounter + "\"");
        }
        else if (body.Contains(urlAndCounter + " "))
        {
          body = body.Replace(urlAndCounter + " ", uniqueUrlAndCounter + " ");
        }

        SaveEmbeddedObject(embeddedObjectBytes,uniqueUrlAndCounter);
    }

    counter++;
}
Eric Dahlvang
  • 8,252
  • 4
  • 29
  • 50