怎样用javamail收取含附件的邮件,熟悉javamail的请近

spczhou 2001-01-12 08:53:00
当一个邮件MIMEtype是“multipart/*”时,会得到Part对象,用下面的方法处理。为什么第18行得不到正文的内容呢?第24行处理正确,能正常显示附件名称。是代码有问题还是处理的方法不对。或者还有其他原因,请高手多多指正。


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="<pre>"+(String)part.getContent()+"</pre>";
19 }
20 else
21 {
22 String temp="";
23 if ((temp = part.getFileName()) != null)
24 s2= "<b>Filename:</b> " + temp + "<br>";
25 temp = null;
26 if ((temp = part.getDescription()) != null)
27 s3= "<b>Description:</b> " + temp + "<br>";
28 }
29 s=s1+s2+s3;
30 return s;
31 }
...全文
311 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
spczhou 2001-01-16
  • 打赏
  • 举报
回复
coding:
《用javamail显示复合邮件的内容的亲身体会》
文档中心,这是我的处女作。不妥之处请指教。
spczhou 2001-01-15
  • 打赏
  • 举报
回复
airwin兄:
我不太理解MIMEtype 和 content type得关系。这段文档,太隐晦了。我不太明白该怎么做。
但是我看了javamail得demo。这部分问题的处理好象和我付的原代码一样。
Virginer 2001-01-15
  • 打赏
  • 举报
回复
To spczhou:
你的coding能不能贴出来,大家分享分享!
spczhou 2001-01-15
  • 打赏
  • 举报
回复
经过本人不断努力,这个问题已经解决了。
错误得根源在于邮件各个部分组成了一个简单得树结构。所以在没有到达树叶之前。15行的if是永远不成立得。谢谢大家...
airwing 2001-01-13
  • 打赏
  • 举报
回复
isMimeType
public boolean isMimeType(java.lang.String mimeType)
throws MessagingException
Is this Part of the specified MIME type? This method compares only the primaryType and subType. The parameters of the content types are ignored.

For example, this method will return true when comparing a Part of content
type "text/plain" with "text/plain; charset=foobar".

If the subType of mimeType is the special character '*', then the subtype is ignored during the comparison.

这是JAVADOC,明白了吧。如果是MIMETYPE,那么content type是被忽略的
spczhou 2001-01-12
  • 打赏
  • 举报
回复
谢谢cming,但是问题仍然没有解决。
cming 2001-01-12
  • 打赏
  • 举报
回复
又或者换成
ct.match("text/*")
这样"text/html","text/plain"都能返回true.

以下是j2ee文档:
public boolean match(java.lang.String s)

Match with the specified content-type string. This method compares only the primaryType and subType . The parameters of both operands are ignored.

For example, this method will return true when comparing the ContentType for "text/plain" with "text/plain; charset=foobar". If the subType of either operand is the special character '*', then the subtype is ignored during the match. For example, this method will return true when comparing the ContentType for "text/plain" with "text/*"

附:pop3 RFC1939
cming 2001-01-12
  • 打赏
  • 举报
回复
试试看:
15:if (ct.match("text/plain"))
换成:15: if (ct.match("text/plain;charset=gb2312"))

我看了sina的我的邮件的编码.
以下是我的一封邮件内容.
Subject: attach test3
Date: Sun Nov 12 08:35:28 2000
X-Mailer: SinaMail 3.0Beta (FireToad)
X-Priority: 3
Content-Type: multipart/mixed;
boundary="----------97401812817428SINAEMAIL---"

This is a multi-part message in MIME format.

------------97401812817428SINAEMAIL---
Content-Disposition: inline
Content-Transfer-Encoding: binary
<font color=red>Content-Type: text/plain;charset="gb2312"</font>

KKK
______________________________________
.
JamesDT 2001-01-12
  • 打赏
  • 举报
回复
去查RFC文档中关于POP3协议的内容.收取邮件时,附件是被编码为文本在正文后面.所以,你要将正文收下后,才能接受附件.具体去开发文档中参阅POP3协议,关于附件编码问题,可以到RFC查阅相关文档. 祝好运!
spczhou 2001-01-12
  • 打赏
  • 举报
回复
它的确是“text/plain ”,因为我在263上,看过它得原文了,是“text/plain ”。
如果将15行换成 if (ct.match("text/html")) 依旧没有任何改变。

airwing 2001-01-12
  • 打赏
  • 举报
回复
那就说明它不是“text/plain ”
难道是html的?
spczhou 2001-01-12
  • 打赏
  • 举报
回复
alan_turing:
这些应该都是没有问题的。关键在于15行得if始终不成立。为什么?
alan_turing 2001-01-12
  • 打赏
  • 举报
回复
判断条件。。。。。。。。。。。。。
alan_turing 2001-01-12
  • 打赏
  • 举报
回复
你的if语句是不是有问题?
alan_turing 2001-01-12
  • 打赏
  • 举报
回复
关注!!!

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧