0

I am using android JavaMail. I would like to parse the inputStream of the content myself. So I use

InputStreamReader reader = new InputStreamReader(messages[i].getInputStream());
int value;
StringBuilder out = new StringBuilder();
while((value = reader.read()) != -1) {
    out.append((char) value);
}
reader.close();
Log.i(TAG, out.toString());

The original string content is :

<body lang=3D"ZH-TW" link=3D"#0563C1" vlink=3D"#954F72" style=3D"text-justify-trim:punctuation">

But when in the printout result is

<body lang=3D"ZH-TW" link=3D"#0563C1" vlink=3D"#954F72" style=3D"text-justi=
fy-trim:punctuation">

There is extra "=" in the line and it breaks into two line.

"=" seems indicate that the line is not ended yet. How did it happen? If the line actually ends with =, then how can we differentiate?

rodent_la
  • 1,195
  • 4
  • 18
  • 38
  • In what sense do you want to "parse" the message yourself? Are you trying to handle the MIME encoding yourself? Or do you just care about the content of the message? – Bill Shannon Jul 02 '15 at 20:54
  • What's the Content-Type and Content-Transfer-Encoding for the message you're reading? If it's a multipart message and you're only showing us part of the content above, then you're reading the encoded content of the message. If it's a single part message and the Content-Transfer-Encoding header is set correctly, JavaMail will decode the message when you read it. You might want to read [this JavaMail FAQ entry](http://www.oracle.com/technetwork/java/javamail/faq/index.html#mainbody). – Bill Shannon Jul 02 '15 at 20:55
  • The test i am trying to do is not dealing with the mail server. Although the content is accessible by IMAP, but the Content-Type can be: 1. Application/xxx-FileTransfer 2. xxx/Session 3. xxx/Message 4. image/jpeg Content-Transfer-Encoding can be base64. So i would like to parse the content myself. – rodent_la Jul 03 '15 at 14:14
  • Most of those are not valid MIME types. Still, you should be able to read the content of the MIME parts, no matter the type, using JavaMail. But to figure out what's not working for you, I need more details. Can you post the entire MIME content of a sample message you're reading, along with the code you're using to read it? Also you might want to try the [JavaMail msgshow.java sample program](https://java.net/projects/javamail/pages/Home#Samples) to read the message, and to use as an example of how you can read the message content. – Bill Shannon Jul 04 '15 at 06:19
  • I will take a look at the sample and check my code. If i still have problem, then i will post the details :) – rodent_la Jul 04 '15 at 12:09
  • Hi Bill, i post a question below. Please help to take a check. Thanks a lot. – rodent_la Jul 15 '15 at 09:51

1 Answers1

0

Bill, I can work with work around problems with broken IMAP servers

according to the https://www.rfc-editor.org/rfc/rfc3501#section-6.4.5

Arguments:  sequence set
               message data item names or macro

so it seems that we can fetch with UID specified.

When i am using javamail, even though have the API fetch the message by UID.

    javax.mail.Message[] messages = folder.getMessagesByUID(localFolderObject.getUIDNext(), 
                                serverFolderObject.getUIDNext());
    
    for (int i = 0; i < messages.length; ++i) {
            // Get the message object from the folder 
            MimeMessage msg = (MimeMessage) messages[i];
           // Copy the message by writing into an byte array.
              ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        try {
                            msg.writeTo(bos);
                            bos.close();
                            Log.i(TAG, bos.toString());
                        } 
                        catch (IOException e) {
                            e.printStackTrace();
                        }
    }

the fetch issue as

A38 FETCH 1 (BODY[])

1 is message sequence number, not the UID.

The server wants us to fetch with

A5 UID FETCH 291 (BODY[])

Is there any API "UID Fetch" command for getting the message https://www.rfc-editor.org/rfc/rfc3501#section-6.4.8? I won't retreive all the messages every time. will keep the previous last NextUID and next time retrieve will start from previous save next UID. Due to fetch by sequence number, the server always return the first sequnce message instead of the one i want.

Community
  • 1
  • 1
rodent_la
  • 1,195
  • 4
  • 18
  • 38