• 0

C# - Extracting images from MIME messages


Question

OK I have a MIME message set up where I have access to all the message parts. I can download the attachments. Now however, I am not sure how inline images (not attached) are supposed to display. When the HTML of the email is outputted, I get this...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR></HEAD>
<BODY>
this is a test for embedded <STRONG>images</STRONG
<IMG alt="" hspace=0 src="cid:651450501@16062004-2F84" align=baseline border=0>
</BODY>
</HTML>

What do I do with this "cid:651450501@16062004-2F84"??

That is exactly what it looks like from outlooks html source also.

17 answers to this question

Recommended Posts

  • 0

According some docs, that cid should be able to be resolved with the Attachments collection.

Here's a quote from C# Network Programming. Don't know if it'll help.

  Quote
The MIME format (Multipurpose Internet Mail Extensions) is defined in RFCs 2045 and 2046. MIME is more versatile than uuencode in that it includes additional information about the binary file within the converted file. The decoder can thus automatically detect and decode various types of binary files.

The MIME standard also provides a way for the MIME-encoded binary data to be directly incorporated into a standard RFC2822 message. Five new header fields (see Table 13.2) were defined to identify binary data types embedded in the mail message. E-mail clients that can handle MIME messages must be able to process these five new header fields. The fields are added immediately after the RFC2822 header fields, and before the message body. Any MIME attachments are added after the message body, as illustrated in Figure 13.7.

Table 13.2: The MIME Message Header Fields  Field

Description

MIME-Version

Specifies the version of MIME used in the encoding

Content-Transfer-Encoding

Specifies the encoding scheme used to encode the binary data into ASCII

Content-ID

Specifies a unique identifier for the message section

Content-Description

A short description identifying the message section

Content-Type

Specifies the type of content contained in the encoded data

Figure 13.7: The MIME message format

The MIME-Version Field

The MIME-Version field identifies the MIME encoding version that the sender used to encode the message:

MIME-Version: 1.0

Alternatively, some software packages add text after the version number to identify additional vendor version information:

MIME-Version: 1.0 (software test 2.3a)

The MIME decoding software of the receiving mail server ignores the additional text.

The Content-Transfer-Encoding Field

The Content-Transfer-Encoding field identifies how the binary data in the message is encoded. There are currently seven methods defined for MIME, listed in Table 13.3. Note that the first three methods define no encoding of the data. The 7-bit encoding method assumes that the encoded data is already 7-bit ASCII text characters, not binary data. This is the default used if no Content-Transfer-Encoding field is present.

Table 13.3: MIME Encoding Methods  Method

Description

7-bit

Standard 7-bit ASCII text

8-bit

Standard 8-bit ASCII text

binary

Raw binary data

quoted-printable

Encodes binary data to printable characters in the U.S.-ASCII character set

base64

Encodes 6 bits of binary data into an 8-bit printable character

ietf-token

Extension token encoding defined in RFC 2045

x-token

Two characters, X- or x-, followed (with no intervening space) by any token

Base64 is the most common method used for encoding binary data. This scheme encodes binary data by mapping 6-bit blocks of binary data to an 8-bit byte of ASCII text. There is less ?wasted space? in the encoded file than with the uuencode method, and it often results in a smaller encoded file.

The Content-ID Field

The Content-ID field identifies MIME sections with a unique identification code. One MIME content section can refer to another MIME message by using this unique field value.

The Content-Description Field

The Content-Description field is an ASCII text description of the data to help identify it in the e-mail message. The text can be any ASCII text of any length.

The Content-Type Field

The Content-Type field is where all the action is. This field identifies the data enclosed in the MIME message. Two separate values, a type and a subtype, identify the data. Here?s the field format:

Content-Type: type/subtype

Following are descriptions of the seven basic types of Content-Type identified in MIME:

text The text Content-Type identifies data that is in ASCII text format. The subtypes for the text Content-Type can be in one of three formats:

plain For unformatted ASCII text

html For text formatted with HTML tags

enriched For text formatted with rich text format (RTF) tags

The text Content-Type also specifies the character set used to encode the data with the charset parameter:

Content-Type: text/plain; charset=us-ascii

This line identifies the MIME section as being plain text, using the U.S. ASCII encoding system.

message The message Content-Type identifies multiple RFC2822-formatted messages contained within a single message. It has three subtypes:

rfc822 Specifies a normal embedded RFC 822-formatted message

partial Specifies one section of a long message that was broken up into separate sections

external-body Specifies a pointer to an object that is not within the e-mail message

image The image Content-Type defines embedded binary data that represents a graphic image. Currently two subtypes are defined: the JPEG format and the GIF format.

video The video Content-Type defines embedded binary data that represents video data. The only subtype defined is the MPEG format.

audio The audio Content-Type defines embedded binary data that represents audio data. The only subtype for this is the basic format, which defines a single-channel Integrated Services Digital Network (ISDN) mu-law encoding at an 8KHz sample rate.

application The application Content-Type identifies embedded binary data that represents application data, such as spreadsheets, word processing documents, and other applications. There are two formal subtypes defined: the postscript format for Postscript-formatted print documents, and the octet-stream format, which defines messages containing arbitrary binary data. The octet-stream subtype represents most application-specific data, such as Microsoft Word documents and Microsoft Excel spreadsheets.

multipart The multipart Content-Type is a special type. It identifies messages that contain multiple data content types combined into one message. This format is common in e-mail packages that can present a message in a variety of ways, such as plain ASCII text and HTML, or in messages that contain multiple attachments. There are four subtypes used:

Mixed Specifies that each of the separate parts are independent of one another and should all be presented to the end customer in the order they are received.

Parallel Specifies that each of the separate parts are independent of one another but can be presented to the end customer in any order.

Alternative Specifies that each of the separate parts represents different ways of presenting the same information. Only one part should be presented to the end customer.

Digest Identifies the same method as the mixed subtype but specifies that the body of the message is always in RFC822-format.

Note  There are lots more Content-Types in addition to the basic seven shown here. Many e-mail packages even define their own types. As long as the mail server and the mail client understand the ContentType, it can be used. Be careful, though, when using non-standard Content-Types: other mail client packages may not recognize them.

  • 0

Ok weenur, got something working. What I did was I made it a response.binarywrite. It writes the image out, but now it is just the image, the other HTML of the page is overwritten I guess. :) Do you think this stream stuff you are talking about will preserve the existing html output?

  • 0

From what I can see, it looks like I have to store it locally on the server and modify the src attribute of the image. Everywhere I see, they say you can't output the binary of the image and the text of the HTML....

Doesn't make sense though.

  • 0

I'm writing my own Mime Parser. The HTML inside the mime message is already in html format, I just have to output it. However, I might have to modify the img src in the html. All I am finding is saving the image to a webserver, and modifying the img src tags to point to that image.

  • 0

OK. I've examined a message that had encoded images in them. Outlook resolved the CID itself. According to MS, you shouldn't need to do anything. IE understands CIDs in a img tag as long as the type is correct and the encoding is specified.

You could try saving to the user's isolated storage, too, instead of a webserver. i'm at a loss on this. BTW, I meant to ask if you writing your own HTML renderer, not parser. Are you using IE's web browser control?

  • 0

Hmmm...interesting thought. Wonder how I push the data so IE knows where to find it. That idea would have to be compatible with other browsers too, otherwise it's more complex than it needs to be probably.

  Quote
You could try saving to the user's isolated storage

Can you explain what you mean here?

I'm just outputting the HTML to the normal browser right now, no special browser control needed. I saw that the next Visual Studio has a browser control that runs from IE code....that's cool. You can make all kinds of customizations there! I will definitely be playng with that when I get a copy of the next IDE.

  • 0

well ultimately, the mime message is suppose to be displayed. I have everything working except for these embedded images/content in the HTML of the body of the message. I am just trying to display this embedded content like it should be in the message.

  • 0

hey weenur, remember this thread? I just got back into this email application I am writing. Bakc to the drawing board on this problem. Anything materialize since the last time we visited this?

  • 0
  chorpeac said:
hey weenur, remember this thread? I just got back into this email application I am writing. Bakc to the drawing board on this problem. Anything materialize since the last time we visited this?

584783450[/snapback]

I remember. I have nothing, dude. I've been sooooooo frickin' busy, it isn't even remotely enjoyable. I would like to try to figure this out, though. Things are slowing down for me as I'm currently on a client's site doing the old 8-5. So, I should be getting some time in the next week or so.

  • 0

Ok well I am actively going to look into this too, because it is being done by other email applications, so I should be able to do it too. I have the data for the image, and all the metadata with encoding, I just need to find a way to display it with the message text.

  • 0

Ok buddy, I figured it out. :)

Here is what I did. First, my MimeMessage object has all the data and metadata for the regular and embedded attachments. So when I display the message, I display the regular html message body, but when I encounter an embedded image (determined when the src attribute has cid:someid), I replace the value of the image's src attribute, with a page called imageviewer.aspx?id=someid.

So it went from

<IMG alt=" hspace=0 src="cid:421121202@22102004-143C" align=baseline border=0>

to

&lt;IMG alt=" hspace=0 src="imageviewer.aspx?id=<421121202@22102004-143C>;" align=baseline border=0&gt;

Then when the html part of the message is rendered, that external imageviewer page is loaded too...

MemoryStream memStream = new MemoryStream((byte[])MailMessage.RelatedItems[Request.QueryString["id"]]); 
Bitmap bitmap = new Bitmap(memStream); 
memStream = new MemoryStream();
Response.ClearContent(); 
Response.ContentType="image/jpeg"; 
bitmap.Save(memStream, ImageFormat.Jpeg); 
memStream.WriteTo(Response.OutputStream); 
bitmap.Dispose();

I'm just wondering if this is efficient enough.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Thanks, I'll download it and see how it goes. Gonna be tough. I've used Nova Launcher for around 10+ years I think.
    • Wikipedia has become hot molasses with mostly filth masquerading as truth. Most of it is the imagination of few writers who think of themselves as above god. The narrative setup is mind boggling.
    • he donates it all to his foundation to launder, trying to avoid inheritance tax while buying up africa
    • No. 1/10th the cost of a Mac? The Mini starts at 599. What mini-pc costs 1/10th of that? The OS is nothing like macOS. Sure you can make it look like it, but it won’t work anything like it. Applications won’t install, or remove the same. Application compatibility will be different. Managing the OS is completely dissimilar, the biggest common ground will be a few CLI utilities. You can definitely get yourself an inexpensive little setup that has a good Linux distro and be happy. It won’t be 1/10th the cost or essentially a Mac though.
    • Vivaldi 7.4.3684.50 by Razvan Serea Vivaldi is a cross-platform web browser built for – and with – the web. A browser based on the Blink engine (same in Chrome and Chromium) that is fast, but also a browser that is rich in functionality, highly flexible and puts the user first. A browser that is made for you. Vivaldi is produced with love by a founding team of browser pioneers, including former CEO Jon Stephenson von Tetzchner, who co-founded and led Opera Software. Vivaldi’s interface is very customizable. Vivaldi combines simplicity and fashion to create a basic, highly customizable interface that provides everything a internet user could need. The browser allows users to customize the appearance of UI elements such as background color, overall theme, address bar and tab positioning, and start pages. Vivaldi features the ability to "stack" and "tile" tabs, annotate web pages, add notes to bookmarks and much more. Vivaldi 7.4.3684.50 changelog: [Chromium] Update to 136.0.7103.170 [Crash][Mail][JSError] RangeError: Maximum call stack size exceeded (VB-115288) Download: Vivaldi 64-bit | 125.0 MB (Freeware) Download: Vivaldi 32-bit | ARM64 View: Vivaldi Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Apprentice
      Adrian Williams went up a rank
      Apprentice
    • Reacting Well
      BashOrgRu earned a badge
      Reacting Well
    • Collaborator
      CHUNWEI earned a badge
      Collaborator
    • Apprentice
      Cole Multipass went up a rank
      Apprentice
    • Posting Machine
      David Uzondu earned a badge
      Posting Machine
  • Popular Contributors

    1. 1
      +primortal
      517
    2. 2
      ATLien_0
      261
    3. 3
      +Edouard
      191
    4. 4
      +FloatingFatMan
      178
    5. 5
      snowy owl
      135
  • Tell a friend

    Love Neowin? Tell a friend!