.NET How to extract embedded image from email message?

99封情书 提交于 2019-12-18 18:29:28

问题


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



来源:https://stackoverflow.com/questions/1259308/net-how-to-extract-embedded-image-from-email-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!