简体   繁体   English

GetContent()空字符串和“ text / html”的空输入流

[英]GetContent() empty string and empty inputstream for “text/html”

I'm trying to write a short test to test some email problems I've been having. 我正在尝试编写一个简短的测试来测试我遇到的一些电子邮件问题。

My test mail (removed some personal info): 我的测试邮件(删除了一些个人信息):

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>

My test code: 我的测试代码:

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());
         }
    }
}

Result: 结果:

-1
Here

-1

So my problem is, no matter what I try I keep ending up with an empty content string or empty inputstreams. 所以我的问题是,无论我尝试什么,我总是以空的内容字符串或空的输入流结尾。

So what am I doing this wrong? 那我在做什么错呢? Or is it something I don't understand? 还是我不理解的东西? I was under the impression that I should be able to use the inputstream since the mimetype is text/html. 我的印象是我应该能够使用inputstream,因为mimetype是text / html。

Could the error have something to do with my test file? 该错误可能与我的测试文件有关吗? I only copied the mail and pasted it into a file I named "parsehtmlmail1.mime". 我只复制了邮件并将其粘贴到名为“ parsehtmlmail1.mime”的文件中。 If so, how come I can use getContentType() etc? 如果是这样,我怎么能使用getContentType()等?

Use the JavaMail demo program msgshow.java (download here ) to read your test message file to make sure the file format is correct. 使用JavaMail演示程序msgshow.java( 在此处下载)读取测试消息文件,以确保文件格式正确。 (Copy/paste isn't the most reliable way to create a properly formatted file.) "java msgshow -m (复制/粘贴不是创建格式正确的文件的最可靠方法。)“ java msgshow -m

The JavaMail FAQ has additional debugging tips. JavaMail FAQ具有其他调试技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM