.NET How to extract embedded image from email mess

2019-02-11 03:54发布

问题:

Hello I'm working on a project in .NET 1.1 and I have a requirement to extract (and save it somewhere) embedded image from emails that I'm receiving.

Can someone give me a clue on where to start?

thank you

回答1:

The email downloaded from the POP server will be in text format, you will have to parse the whole email, and find all the <img /> tags having the src attribute set to cid:*

E.g.

<img src='cid:006901c6d391$dee64770$6c822ecf@Z2LC74Q' />

The format of an email containing the embedded image will be as follows -

From: foo1atbar.net 
To: foo2atbar.net 
Subject: A simple example 
Mime-Version: 1.0 
Content-Type: multipart/related; boundary="boundary-example"; type="text/html" 

--boundary-example 
Content-Type: text/html; charset="US-ASCII" 

... text of the HTML document, which might contain a URI 
referencing a resource in another body part, for example 
through a statement such as: 
<IMG SRC="cid:foo4atfoo1atbar.net" ALT="IETF logo"> 
...snip...
Content-Location: CID:somethingatelse ;this header is disregarded 
Content-ID: <006901c6d391$dee64770$6c822ecf@Z2LC74Q>
Content-Type: IMAGE/GIF 
Content-Transfer-Encoding: BASE64 

R0lGODlhGAGgAPEAAP/////ZRaCgoAAAACH+PUNv 
cHlyaWdodCAoQykgMTk5LiBVbmF1dGhvcml6ZWQgZHV 
wbGljYXRpb24gcHJvaGliaXRlZC4A etc... 

...snip...

If you take a look at the footer, it contains a BASE64 encoded version of your image. You can extract the the BASE64 string, convert it to bytes based on the email character set, and save it to a file (you can get the file extension based on the Content-Type). Tada, done!

Hope you've got an idea of how to do it!

EDIT

I also found a similary question here. He's using CDO (Collaboration Data Objects).