• 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

    • But a human composed and chose the shot and took the picture. That's how the law sees it. Fortunately, this is the copyright office's determination based on judges' rulings, so that's a win for we human beings...for now.
    • Sifu developer's new soccer game Rematch scores one million copies sold on launch weekend by Pulasthi Ariyasinghe The development team behind fighting games like Sifu and Absolver, Sloclap, stepped into the competitive multiplayer arena last week with Rematch, a soccer/football game. The multiplatform sports title immediately boosted to almost 100,000 players on Steam alone. The studio has now revealed that over a million copies of Rematch have already been sold while also sharing some fun stats from the ongoing matches between players. Since its launch on June 19, over 11.78 million matches have been played in the title, resulting in 55.58 million goals, 33.54 million assists, and 69.12 million goalkeeper saves. The one-million sales milestone is also impressive, considering Rematch, a $29.99 premium title, is also available on the Microsoft subscription platform Game Pass. Both Xbox Game Pass Ultimate and PC Game Pass services received the title on the day of release for no extra cost. The studio confirmed today that Rematch has already reached 2.5 million players, including all subscription holders. The launch was not exactly smooth, however. While originally promised for launch, crossplay between platforms was ultimately missing. The studio said that implementing the highly requested feature is now its highest priority among bug fixes and server management. Multiple hotfixes also landed over the weekend to fix numerous matchmaking issues, desyncs, and crashes. Steam user reviews are slowly recovering following the hotfixes, with it currently touting a 78% Mostly Positive rating by players. In future updates, the studio plans to implement AI bots for training, spectating games, social elements for easily making squads, a tournament system with clubs, and major gameplay tweaks with a separate public test server. Rematch is now available across PC, Xbox Series X|S, and PlayStation 5 platforms as well as on Game Pass.
    • This is pretty nice. For one more year at least I don't have to install linux on my moms laptop and configure it so she understands it enough to sometimes read news online.
    • Just the settings need to be synced it seems, really doesn't matter much.
    • PDF24 Creator 11.27.0 by Razvan Serea PDF24 PDF Creator installs a virtual printer driver that allows you to convert any printable document or image into PDF format. You can also import documents from your scanner, combine multiple documents into one and delete selected pages from existing PDF files. The program supports creation of secure, digitally signed documents, PDF quality settings, integrated preview, emailing, and more. You can either drag'n drop documents onto the GUI or convert them from any other program by using the virtual printer driver - simply select the PDF24 printer instead of your regular paper printer. Advantages of PDF24 PDF Creator: Once installed, create PDF files forever Free upgrades included Conversion is simple and possible from all current programs Freeware Create PDF files from almost any application Easy to use Multilingual Several features of PDF24 PDF Creator: Merge and split PDF Extract pages from a PDF Copy pages from one PDF to another PDF Integrated preview for easy PDF editing Secure a PDF (Prevent from unauthorised opening, printing, etc.) Set PDF information such as author and title The following tools are included in the new PDF Toolbox of PDF24 Creator 11: Merge PDF Compress PDF Edit PDF Convert files to pdf Convert PDF files to other formats PDF to Text PDF to HTML PDF to JPG PDF to PNG PDF to PDF, PDF/A-1, PDF/A-2, PDF/A-3 PDF to Word PDF to PowerPoint PDF to Excel Protect PDF Unlock PDF Split PDF Rotate PDF pages Delete PDF pages Extract PDF pages Sort PDF pages Create a PDF from images Convert a PDF to images Extract images from PDFs Create online application as PDF Optimize PDF for the Web Insert watermark into a PDF Insert page numbers into a PDF Overlay PDF files Compare PDF files Sign PDF files Annotate PDF files Blacken PDF files Crop PDF Flatten PDF Download: PDF24 Creator 11.27.0 | MSI Setup | ~400.0 MB (Freeware) View: PDF24 Creator Homepage | Release Notes | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      DrRonSr earned a badge
      Week One Done
    • Week One Done
      Sharon dixon earned a badge
      Week One Done
    • Dedicated
      Parallax Abstraction earned a badge
      Dedicated
    • First Post
      956400 earned a badge
      First Post
    • Week One Done
      davidfegan earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      602
    2. 2
      ATLien_0
      226
    3. 3
      Michael Scrip
      167
    4. 4
      +FloatingFatMan
      154
    5. 5
      Som
      136
  • Tell a friend

    Love Neowin? Tell a friend!