Link to home
Start Free TrialLog in
Avatar of Isisagate
Isisagate

asked on

JavaMail Seven-bit encoding error

We have an object that automatically checks an email address through a POP3/SMTP server about every 5 minutes. One email that was sent in had an encoding on it of sevenbit. As soon as that came it threw an error.

java.io.IOException: Unknown encoding: sevenbit
      at javax.mail.internet.MimePartDataSource.getInputStream(MimePartDataSource.java:70)
      at com.sun.mail.handlers.text_plain.getContent(text_plain.java:64)
      at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:745)
      at javax.activation.DataHandler.getContent(DataHandler.java:501)
      at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1339)

What can I do to fix this or accommodate this assuming this one source constantly sends in that encoding format this could be an issue.  I know my fall back would be to just move the email into a folder on the server or just delete it but I would prefer to be able to accommodate it.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isisagate
Isisagate

ASKER

I am not sure what you mean by "trying a valid 7-bit encoding instead"  the error line in my code looks like this...

Object ob = ((MimeMessage) msg[x]).getContent();


how should I go about changing this to hopefully be able to get the message body?
You'd need to explicitly handle the decoding
Do you have an example?
No. Have to work on this incrementally. Can you do a debug of

((MimeMessage) msg[x]).getDataHandler().getContentType()
Well I'm going to have a rough time debugging this I had to delete that email to get them back up and running quickly.

looking at the javadocs for JavaMail I see MimeUtility.decode(java.io.InputStream is, java.lang.String encoding);


not the questions is how do we take a Message and convert it to an InputStream?

Second how do we convert the Content-Transfer-Encoding header value to a string to detect "sevenbit" or "7-bit" to be able to correct it manually.

how do we convert the iostream back to a Message object.


if we can get those piece I am sure it will work from there.
Well it could be fairly tricky. Any chance of forwarding me a bad mail? Just interested in the headers
good thing it was still in the trash off the webmail... here are the headers...

Return-Path: <sales@vanline.com>
Received: from mail191.messagelabs.com [216.82.241.83] by mail48.safesecureweb.com with SMTP;
   Mon, 22 Oct 2007 01:06:43 -0400
Received: (qmail 3390 invoked from network); 22 Oct 2007 05:06:42 -0000
Received: from mailweb.vanlines.com (HELO mail.vanlines.com) (69.60.139.132)
  by server-10.tower-191.messagelabs.com with SMTP; 22 Oct 2007 05:06:42 -0000
Received: from nyc2-s1000 ([10.102.32.2])
      by vanlines.com (mail.vanlines.com)
      (MDaemon PRO v9.0.6)
      with ESMTP id md50000082999.msg;
      Mon, 22 Oct 2007 01:06:21 -0400
X-VirusChecked: Checked
X-Env-Sender: sales@vanline.com
X-Msg-Ref: server-10.tower-191.messagelabs.com!1193029602!10598479!1
X-StarScan-Version: 5.5.12.14.2; banners=-,-,-
X-Originating-IP: [69.60.139.132]
mime-version: 1.0
from: sales@vanline.com
to: leads@CLIENT.com
reply-to: sales@vanline.com
date: 22 Oct 2007 01:08:01 -0400
Subject: Vanlines.com Lead. TX - TX
content-type: text/plain; charset=us-ascii
content-transfer-encoding: sevenbit
X-Spam-Processed: mail.vanlines.com, Mon, 22 Oct 2007 01:06:21 -0400
      (not processed: spam filter heuristic analysis disabled)
X-MDRemoteIP: 10.102.32.2
X-Return-Path: sales@vanline.com
X-Envelope-From: sales@vanline.com
X-MDaemon-Deliver-To: leads@CLIENT.com
X-SmarterMail-Spam: SPF_None
Yes, as you i think know

>>content-transfer-encoding: sevenbit

should be

content-transfer-encoding: 7bit

Now it's a question of either

a. filtering that out before it gets to JavaMail
b. creating a subclass of the correct class to make that change in the processing chain
Actually 'they' are suggesting what i said originally. Look at the api notes for this method - it applies exactly to your situation:

http://java.sun.com/products/javamail/javadocs/javax/mail/internet/MimeBodyPart.html#getRawInputStream()
Well the desired result would be to compensate for the messed up header and still be able to proceed.

think this might do it? I'm not sure the header piece so I tried to do the rest...


Object ob =null;
                  try
                  {
                        ob = ((MimeMessage) msg[x]).getContent();
                  }
                  catch(Exception e)
                  {
                        String encoding = ""; //Convert the encoding header value to a string
                        
                        encoding = encoding.toLowerCase();
                        
                        if(encoding.indexOf("sevenbit") >-1 || encoding.indexOf("7-bit") >-1 )
                        {
                              InputStream msgIS = msg[x].getInputStream(); //get Message input Stream
                              
                              InputStream decodedIS = MimeUtility.decode(msgIS, "7bit"); //Decode as 7bit
                              
                              ob = (new MimeMessage(mySession, decodedIS)).getContent(); //convert back to MimeMessage
                        }
                        
                  }
>> InputStream msgIS = msg[x].getInputStream(); //get Message input Stream

Don't think that will work (see my last comment)
okay I will look at that, what about the header info?
Yes, it was what i expected. You've implemented that correctly in your last code afaics apart from the problem i mentioned
well I mean I left it as a stub....

 String encoding = ""; //Convert the encoding header value to a string

because I'm not sure where to pull the value from... Any idea how I go about it?

I think you are 100% right that it's their email script putting the wrong encoding name, the google searches after you suggested that seem to match up. I just need to try and get a working solution in the next 2 hours to try and get it included in this evening's build.



the current code I have is this... all I need is the string value of that header and this should work.. this code compiles. So almost there....



                  Object ob =null;
                  try
                  {
                        ob = ((MimeMessage) msg[x]).getContent();
                  }
                  catch(Exception e)
                  {
                        String encoding = ""; //Convert the encoding header value to a string

                        encoding = encoding.toLowerCase();

                        if(encoding.indexOf("sevenbit") >-1 || encoding.indexOf("7-bit") >-1 )
                        {
                              InputStream msgIS = ((MimeMessage)msg[x]).getRawInputStream(); //get Message input Stream

                              InputStream decodedIS = MimeUtility.decode(msgIS, "7bit"); //Decode as 7bit

                              ob = (new MimeMessage(this.mySession, decodedIS)).getContent(); //convert back to MimeMessage
                        }

                  }

That will return whatever is set even if it's a bad value right?
Should do
:-)

Let me know how you get on
Thought this would be a good thing to clearify.

We roled out the fix last night and it works great, except... Turns out the emails content may say "sevenbit" but it's really a dual encoding.. quoted-printable and 7bit. When it rolled out the following content was all that was unincoded from the body..

"______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ "


however if you look at it in a webmail client it doesn't show this rather all the lead content. Turns out when I ran some tests with a php script sending in the faulty headers, that is says sevenbit but has a chunk as quoted-printable and appending on to the body that 7bit content. so when you specify which one it is it only grabs the chunk in the correct format.  So it looks like their messagelabs.com setup if causing this. While I can justify putting in code to convert "sevenbit" and "7-bit" into "7bit" I definitely won't make it quoted-printable that's how IE quarks probably got so bad.

Thought you might like to know that.
Thanks Isisagate - what trouble is caused by standards non-compliance!
Yeah they need to reference RFC 2045...   http://tools.ietf.org/html/rfc2045 

I dug it up today when I was trying to figure out how to reply to a client about the new issue. Oh well I learned a lot about how JavaMail works and email in general dealing with this. Too bad it ate up so much of our time with less then desired results.
Bounce it with a curt message about the encoding ;-)