我正在尝试编写一个简短的测试来测试我一直遇到的一些电子邮件问题。
我的测试邮件(删除了一些个人信息):
Received: from <address> (<ip>) by
<address> (<ip>) with Microsoft SMTP
Server (TLS) id <ip>; <time>
Received: from <address> (<ip>) by
<address> (<ip>) with mapi id
<ip>; <time>
From: <name> <<mail>>
To: <name> <<mail>>
Subject: =?iso-8859-1?Q?<subject>?=
Thread-Topic: =?iso-8859-1?Q?<subject>?=
Thread-Index: <index>==
Date: <time>
Message-ID: <<message-id>>
References: <<references-id>>
<<references-id>>
<<references-id>>
In-Reply-To: <<references-id>>
Accept-Language: sv-SE, en-US
Content-Language: sv-SE
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 04
X-MS-Exchange-Organization-AuthSource: <<address>>
X-MS-Has-Attach:
X-MS-Exchange-Organization-SCL: -1
X-MS-Exchange-Inbox-Rules-Loop: <address>
X-MS-TNEF-Correlator:
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=1">
</head>
<body>
text, nothing special a bit of css and some html code
</body>
</html>
我的测试代码:
Session s = Session.getDefaultInstance(new Properties());
mime = EmailTest.class.getResourceAsStream("/mailtests/parsehtmlmail1.mime");
MimeMessage msg = new MimeMessage(s, mime);
InputStream is = msg.getRawInputStream();
System.out.println(is.read());
if(msg.getContentType().toLowerCase().startsWith("text/html")){
if(msg.isMimeType("text/html")){
if(msg.getContent() instanceof String) {
System.out.println("Here");
System.out.println(msg.getContent().toString());
InputStream is2 = msg.getInputStream();
System.out.println(is2.read());
}
}
}
结果:
-1
Here
-1
所以我的问题是,无论我尝试什么,我都会以空内容字符串或空输入流结束。
那么我做错了什么?或者是我不明白的事情?我的印象是我应该能够使用输入流,因为mimetype是text / html。
错误可能与我的测试文件有关吗?我只复制了邮件并将其粘贴到一个名为“parsehtmlmail1.mime”的文件中。如果是这样,为什么我可以使用getContentType()等?
答案 0 :(得分:0)
使用JavaMail演示程序msgshow.java(下载here)来读取测试消息文件,以确保文件格式正确。 (复制/粘贴不是创建格式正确的文件的最可靠方法。)“java msgshow -m
JavaMail FAQ还有其他调试提示。