Solved

javamail imap message content encoding

Posted on 2009-07-10
10
1,872 Views
Last Modified: 2013-11-29
I'm trying to download mail from an imap server and I keep getting Unknown Encoding exceptions.  Here's the exception:

java.io.IOException: Unknown encoding: 7Bit;
      javax.mail.internet.MimePartDataSource.getInputStream(MimePartDataSource.java:108)
      com.sun.mail.handlers.text_plain.getContent(text_plain.java:90)
      javax.activation.DataSourceDataContentHandler.getContent(Unknown Source)
      javax.activation.DataHandler.getContent(Unknown Source)
      javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1395)
      net.mainsequence.email.MailMessage.loadMessage(MailMessage.java:176)
      net.mainsequence.email.MailMessage.<init>(MailMessage.java:110)

Any ideas on how to fix this?
Object content = message.getContent();

			if(content instanceof String){

				mailContent = (String) content;

				textSegment = mailContent;

				htmlSegment = "";

			}else if(content instanceof InputStream){

				InputStream in = (InputStream) content;

				byte[] c = new byte[in.available()];

				in.read(c);

				String temp = new String(c);

				mailContent = temp;

				textSegment = mailContent;

				htmlSegment = "";

			}else if(content instanceof Multipart){

				Multipart multipart = (Multipart) message.getContent();	

				getMultiPart(multipart);

			}

Open in new window

0
Comment
Question by:csalem01
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24828082
Looks like you might have a malformed header with a stray or extra semicolon. You might need to catch the exception and extract the message to deal with it
0
 
LVL 92

Expert Comment

by:objects
ID: 24828291
encoding should be 7bit
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24829538
Encoding is not case sensitive
0
 

Author Comment

by:csalem01
ID: 24839973
i caught the exception and printed the message to a file using the messages getrawinputstream function and it seems like all the messages that I'm having a problem with were caught as spam by spamassassin.  I don't see any problem with the headers though.  Looking through the error log though I see there's also an exception thrown for a message with content type "utf-8" and one message with content type "iso-8859-10".  I attached one of the messages that was throwing the exception with "7Bit", any ideas with what the problem with that message is?
269094365369276.txt
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24840241
The problem is exactly what i told you above. I've just saved some of my mails+headers to file 'ct' and run the following (note the absence of semicolons):
goose@seegobin:/tmp$ grep 'Content-Transfer-Encoding' ct

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: 7bit

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: 7bit

Content-Transfer-Encoding: 7bit

  h=Received:X-YMail-OSG:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:Content-Type:Content-Transfer-Encoding;

Content-Transfer-Encoding: 7bit

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: 7bit

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: base64

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: quoted-printable

Content-Transfer-Encoding: base64

goose@seegobin:/tmp$ 

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 92

Expert Comment

by:objects
ID: 24844760
transfer encoding is incorrect as I pointed out above :)
It should be "7bit", not "7bit;"
0
 

Author Comment

by:csalem01
ID: 24852524
Thanks alot, that worked.  I attached the code that made it work.  

I'm also getting errors with these content types (I attached the messages for each):

Content-Type: text/plain;
 charset="iso-8859-10"

Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: utf-8

Any ideas on how to fix these?
String xEncoding[] = message.getHeader("Content-Transfer-Encoding");

			if(xEncoding != null){

				if(xEncoding.length > 0){

					if(xEncoding[0].equalsIgnoreCase("7Bit;")){

						tempMessage = new MimeMessage((MimeMessage)message);

						tempMessage.removeHeader("Content-Transfer-Encoding");

						tempMessage.addHeader("Content-Transfer-Encoding", "7Bit");

					}

				}

			}

Open in new window

980348665832080.txt
318235670833124.txt
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24852717
Those charsets are relatively exotic. I certainly don't have support for ISO-8859-10 in my runtime, but do have support for KOI8-R in one runtime. Make sure you've selected full internationalization support on install.

You could probably ignore the mails that are either wrong or unsupported
0
 

Author Comment

by:csalem01
ID: 24853803
The problem with the KOI8-R mail is the "Content-Transfer-Encoding: utf-8".

"utf-8" isn't in the mime spec.  I guess I'll just have to ignore those mails.

thanks alot for your help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24853864
:-)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
OWA address for sennatech.sg domain 6 39
JList custom Cell Renderer refresh 15 53
netstat -ano | find "8000" and taskkill /f /pid 2984 3 32
MySQL  on Tomcat 8 26
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now