复合邮件:既含图片又含正文,一般是html格式显示。简单理解的话,就是非常漂亮的那种邮件。
(个人注解,不很全面)
当我们需要做一个收取邮件的程序时,我们该如何去显示复合邮件的内容呢?
今天我将我得一点心得写出来,希望后来者能够少走弯路。当然也希望同志多加指正。
首先,我们需要从folder中得到一个message对象。这一步不难实现。可以参看很多例子。当我们得到这个message对象后,下一步就是去显示他的内容了。在这部分开始前,首先需要简要介绍一下mimetype的几个重要类型:
| text/html texl/plain multipart/alternative multipart/related |
| object o = m.getcontent(); if (m.ismimetype("text/plain")m.ismimetype("text/html")) { s=" "+(string)o+""; return s; } |
| if (m.ismimetype("multipart/*") { multipart mp = (multipart)o; int cnt = mp.getcount(); for (int i = 0; i < cnt; i++) { w=w+" "+getpart(mp.getbodypart(i), i, 2,x); } } |
| 1 public string getpart(part part, int partnum) 2 throws messagingexception,ioexception 3 { 4 string s=""; 5 string s1=""; 6 string s2=""; 7 string s3=""; 8 string sct = part.getcontenttype(); 9 if (sct == null) 10 { 11 s="part 无效"; 12 return s; 13 } 14 contenttype ct = new contenttype(sct); 15 if (ct.match("text/plain")) 16 { 17 // display text/plain inline 18 s1=" "+(string)part.getcontent()+""; 19 } 20 else 21 { 22 string temp=""; 23 if ((temp = part.getfilename()) != null) 24 s2= "filename: " + temp + " "; 25 temp = null; 26 if ((temp = part.getdescription()) != null) 27 s3= "description: " + temp + " "; 28 } 29 s=s1+s2+s3; 30 return s; 31 } |
| public string getpart(part part, int partnum, int msgnum,int x) throws messagingexception,ioexception { string s=""; string s1=""; x参数来确定是以html格式显示还是以plain string s2=""; string s3=""; string s5=""; string sct = part.getcontenttype(); if (sct == null) { s="part 无效"; return s; } contenttype ct = new contenttype(sct); if (ct.match("text/html")ct.match("text/plain")) { // display text/plain inline s1=" "+(string)part.getcontent()+""; } else if(partnum!=0) { string temp=""; if ((temp = part.getfilename()) != null) { s2= "filename: " + temp + " "; } /* out.println(" httputils.getrequesturl(req) + "?message=" + msgnum + "&part=" + partnum + "\">display attachment"); */ } if(part.ismimetype("multipart/alternative")) { string s6=""; string s7=""; multipart mp = (multipart)part.getcontent(); int count = mp.getcount(); for (int i = 0; i < count; i++) { if(mp.getbodypart(i).ismimetype("text/plain")) s7=getpart(mp.getbodypart(i), i, 3,2); if(mp.getbodypart(i).ismimetype("text/html")) s6=getpart(mp.getbodypart(i), i, 3,1); } if(x==1){s5=s6;} if(x==2){s5=s7;} return s5; } s=s1+s2; return s; } |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器