接收邮件的一个算法问题
+------------------------- multipart/mixed ----------------------------+
| |
| +----------------- multipart/related ------------------+ |
| | | |
| | +----- multipart/alternative ------+ +----------+ | +------+ |
| | | | | 内嵌资源 | | | 附件 | |
| | | +------------+ +------------+ | +----------+ | +------+ |
| | | | 纯文本正文 | | 超文本正文 | | | |
| | | +------------+ +------------+ | +----------+ | +------+ |
| | | | | 内嵌资源 | | | 附件 | |
| | +----------------------------------+ +----------+ | +------+ |
| | | |
| +------------------------------------------------------+ |
| |
+----------------------------------+
以上这是邮件的一个基本结构,
23 Content-Type: multipart/related; type= "multipart/alternative ";
24 boundary= "----=_NextPart_001_007B_01C3115F.80DFC5E0 "
25
26
27 ------=_NextPart_001_007B_01C3115F.80DFC5E0
28 Content-Type: multipart/alternative;
29 boundary= "----=_NextPart_002_007C_01C3115F.80DFC5E0 "
30
31 ------=_NextPart_002_007C_01C3115F.80DFC5E0
32 Content-Type: text/plain; charset= "gb2312 "
33 Content-Transfer-Encoding: quoted-printable
34
35 bhw98, =C4=E3=BA=C3!
36 =D5=E2=CA=C7=CE=D2=D0=B4=B5=C4=B6=E0=B4=AE=BF=DA=CD=A8=D0=C5=B5=C4=B3=CC=D0=
37 =F2, =C7=EB=D6=B8=BD=CC!
38
39
40 ------=_NextPart_002_007C_01C3115F.80DFC5E0
41 Content-Type: text/html; charset= "gb2312 "
42 Content-Transfer-Encoding: quoted-printable
43
44 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
45 <HTML> <HEAD> <TITLE> =C7=E7=C0=CA </TITLE>
46 <META content=3D "text/html; charset=3Dgb2312 " http-equiv=3DContent-Type>
47 <STYLE> BODY {
48 COLOR: #0033cc; FONT-FAMILY: =CB=CE=CC=E5, Arial, Helvetica; FONT-SIZE: =
49 9pt; MARGIN-LEFT: 10px; MARGIN-TOP: 25px
50 }
51 </STYLE>
52 <META content=3D "MSHTML 5.00.2920.0 " name=3DGENERATOR> </HEAD>
53 <BODY background=3Dcid:007901c3111c$72b978a0$0100007f@bluesky =
54 bgColor=3D#ffffff>
55 <DIV>
56 <DIV> bhw98, =C4=E3=BA=C3! </DIV>
57 <P> =D5=E2=CA=C7=CE=D2=D0=B4=B5=C4=B6=E0=B4=AE=BF=DA=CD=A8=D0=C5=B5=C4=B3=CC=
58 =D0=F2, =C7=EB=D6=B8=BD=CC! </P> </DIV>
59 <P> </P> </BODY> </HTML>
60
61 ------=_NextPart_002_007C_01C3115F.80DFC5E0--
62
63 ------=_NextPart_001_007B_01C3115F.80DFC5E0
64 Content-Type: image/jpeg; name= "=?gb2312?B?x+fAyrGzvrAuSlBH?= "
65 Content-Transfer-Encoding: base64
66 Content-ID: <007901c3111c$72b978a0$0100007f@bluesky>
67
68 /9j/4AAQSkZJRgABAgEASABIAAD/7QVoUGhvdG9zaG9wIDMuMAA4QklNA+0AAAAAABAASAAAAAEA
69 AQBIAAAAAQABOEJJTQPzAAAAAAAIAAAAAAAAAAA4QklNBAoAAAAAAAEAADhCSU0nEAAAAAAACgAB
70 AAAAAAAAAAI4QklNA/UAAAAAAEgAL2ZmAAEAbGZmAAYAAAAAAAEAL2ZmAAEAoZmaAAYAAAAAAAEA
... ... ... ...
169 RxVw98Vawq12xQ44q0cKtHFDWKGsKt4EtiuKt4q//9k=
170
171 ------=_NextPart_001_007B_01C3115F.80DFC5E0--
以上是一封邮件的multipart/related 内嵌资源 和multipart/alternative 超文本
如何正确的把一封超文本的邮件接收下来,并且把内嵌资源正常显示在此超文本的页面中?
当然两个编码我都能从其中解出来,意思就是当我把这两个东西都下下来后,当我想显示该HTML时怎么把所嵌的资源也显示出来?(比如:图片)谢谢!
[解决办法]
学习
[解决办法]
看不出门道
[解决办法]
内嵌资源作成单独的临时文件?
[解决办法]
很简单,看这里:
63 ------=_NextPart_001_007B_01C3115F.80DFC5E0
64 Content-Type: image/jpeg; name= "=?gb2312?B?x+fAyrGzvrAuSlBH?= "
65 Content-Transfer-Encoding: base64
66 Content-ID: <007901c3111c$72b978a0$0100007f@bluesky>
67
68 /9j/4AAQSkZJRgABAgEASABIAAD/7QVoUGhvdG9zaG9wIDMuMAA4QklNA+0AAAAAABAASAAAAAEA
69 AQBIAAAAAQABOEJJTQPzAAAAAAAIAAAAAAAAAAA4QklNBAoAAAAAAAEAADhCSU0nEAAAAAAACgAB
70 AAAAAAAAAAI4QklNA/UAAAAAAEgAL2ZmAAEAbGZmAAYAAAAAAAEAL2ZmAAEAoZmaAAYAAAAAAAEA
... ... ... ...
169 RxVw98Vawq12xQ44q0cKtHFDWKGsKt4EtiuKt4q//9k=
170
171 ------=_NextPart_001_007B_01C3115F.80DFC5E0--
64行已经指明该段数据为 image/jpeg 格式;65行说明内容编码为 base64;
现在只要把那些看似“乱码”的数据解码(debase64,很多库直接提供)即可得到原始数据;
而原始数据中,name是gb2312编码,直接显示出来就可以支持汉字文件名;
68行开始就是原始jpeg数据,最笨的办法是把它们(经base64解码后)写入临时jpeg文件再显示出来;如果有已封装的jpeg图片显示类,并且这个类设计得还可以的话,它一定有一个运行时访问图片原始数据的接口,这时只要把解开的图片二进制数据拷贝过去就行了。
当然,如果你用的是第三方控件甚至不过是asp之类的话,那你的帖子发错地方了,到别的地方查现成接口吧。
[解决办法]
邮件的结构比你说的要更复杂点。图片可以尝试用 <img src= "... "/> 取出,其中...部分表示一个后台servlet,用于读取邮件中的图片形式附件并直接显示到信件内容中。希望对你有所帮助
[解决办法]
...