Handling Malformed Messages
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Lewis_Muir
Posted On:   Tuesday, September 25, 2001 02:35 PM

I'm writing an email client and would like to be able to gracefully handle malformed messages. It's one thing to try to correct a malformed message, but in the worse case, I'd like to be able to get the raw data for the message and consider it of mime type "application/octet-stream" and attach it to a new wrapper message so the user can download the original message in its entirety and deal with it however the user sees fit. I hoped to find a method like Message.validate() which would throw an exception indicating what is invalid about the message. I could then catch the exception, and invoke a method like Message.getRawInputStream() and do whatever I wanted with the raw data of the invalid/malformed message   More>>


I'm writing an email client and would like to be able to gracefully handle malformed messages.


It's one thing to try to correct a malformed message, but in the worse case, I'd like to be able to get the raw data for the message and consider it of mime type "application/octet-stream" and attach it to a new wrapper message so the user can download the original message in its entirety and deal with it however the user sees fit.


I hoped to find a method like Message.validate() which would throw an exception indicating what is invalid about the message. I could then catch the exception, and invoke a method like Message.getRawInputStream() and do whatever I wanted with the raw data of the invalid/malformed message. But, I don't see anything like this, nor do I see a good way around this.


How can I (1) validate an instance of Message and (2) get the message in its "raw" (byte array, InputStream, whatever) state where it has _not_ been processed by JavaMail in any way?


As a litmus test, I created a Message with a 'Content-Transfer-Encoding' header field that was invalid ("Content-Transfer-Encoding: WRONG;;;"). Below is a test class which demonstrates this.


Note this is just one example of a malformed message. Again, I would like to be able to handle an arbitrary kind and number of malformities.


Thanks!

			
MessageSandbox.java
---
import javax.activation.DataHandler;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.Session;

/**
* Place to try things out related to JavaMail Message.
*/
public class MessageSandbox {

/**
* run test for bad 'Content-Transfer-Encoding' header field
*/
public static void main(String[] args) {
getRawWithBadCte();
}


protected MessageSandbox() {
super();
}


/**
* how can i deal with a JavaMail Message that has a bad
* 'Content-Transfer-Encoding' header field?
*
* the actual Content-Transfer-Encoding is base64
*
* none of these work
*/
public static void getRawWithBadCte() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bos);
ps.println("From: test@no.where");
ps.println("To: test@no.where");
ps.println("Subject: here is a small txt file");
ps.println("Date: Tue, 25 Sep 2001 11:53:15 -0500");
ps.println("MIME-Version: 1.0");
ps.println("Content-Type: text/plain;");
ps.println(" name="word.txt"");
ps.println("Content-Transfer-Encoding: WRONG;;;");
ps.println("Content-Disposition: attachment;");
ps.println(" filename="word.txt"");
ps.println();
ps.println("VGhlIHNlY3JldCB3b3JkIGlzIFBBTkNBS0Uu");
ps.println();

Message message;
try {
message = new MimeMessage(Session.getInstance(new Properties()),
new ByteArrayInputStream(bos.toByteArray()));
} catch (Exception e) {
e.printStackTrace();
return;
}

try {
message.getInputStream();
} catch (Exception e) {
System.out.println(
"---While trying message.getInputStream()---");
e.printStackTrace();
System.out.println("---end---");
}

try {
message.getDataHandler().getDataSource().getInputStream();
} catch (Exception e) {
System.out.println(
"---While trying message.getDataHandler().getDataSource().getInputStream()---");
e.printStackTrace();
System.out.println("---end---");
}

try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
message.getDataHandler().writeTo(os);
os.toString();
} catch (Exception e) {
System.out.println("---While trying message.getDataHandler().writeTo(os)---");
e.printStackTrace();
System.out.println("---end---");
}

try {
DataHandler dh = new DataHandler(message.getDataHandler().getDataSource().getInputStream(),
"application/octet-stream");
ByteArrayOutputStream os = new ByteArrayOutputStream();
dh.writeTo(os);
os.toString();
} catch (Exception e) {
System.out.println("---While trying DataHandler w/ set mime type---");
e.printStackTrace();
System.out.println("---end---");
}
}
}
---
   <<Less
About | Sitemap | Contact