M
Martin Gregorie
I have a JavaMail problem that has me stumped. I'm trying to build and
send a MIME message containing:
1)a text/plain part containing text describing the following part
2)a message/rfc822 part containing a complete mail message which
can be MIME or plain text.
The message is being built and dispatched OK - I can read it with
Evolution, see the plain text and open the attachment, but the latter is
somewhat mangled. Here's the code I'm using the assemble the part.
'headerText' contains the headers for the message that will form the
attachment, 'bodytext' contains the complete MIME body of the message
and 'name' contains the name to be used for the attachment (its actually
the subject line). The body and headers are separated because that was
the easiest way to retrieve them from a JavaMail Message. Here's the
method that builds the part:
public boolean addMessagePart(String headerText,
String bodyText,
String name)
{
boolean success = true;
MimeBodyPart b = null;
StringBuffer s = new StringBuffer();
String mt = new String("message/rfc822");
String fn = null;
String cv = null;
if (name.length() == 0)
{
fn = new String("no_name.eml");
cv = new String(mt);
}
else
{
fn = new String(name + ".eml");
cv = new String(mt + "; name=\"" + fn + "\"");
}
try
{
s.append(headerText);
s.append("\n\n");
s.append(bodyText);
b = new MimeBodyPart();
b.setText(s.toString());
b.setDisposition("attachment");
b.setFileName(fn);
b.removeHeader("Content-Type");
b.addHeader("Content-Type", cv);
body.addBodyPart(b); /* body is a class member */
msgReady = true; /* msgReady is a class member */
}
catch (MessagingException e)
{
error = e.getMessage() + " adding message part";
success = false;
}
return success;
}
However, when I look at the attachment with Evolution it is displayed as
plain text, not an attached mail message. This is evidently due to
additional headers that have been injected somehow. Here's what I get on
the receiving end:
------=_Part_0_26953436.1190840435551
Content-Type: message/rfc822; name="Revision to Your Alibris E-mail
Address.eml"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="Revision to Your Alibris
E-mail Address.eml"
From:
Date: Wed, 26 Sep 2007 22:00:50 +0100
Subject: No Subject
.........
Analysing this part of the message, which is the start of the part
containing the attached message:
- The first block (lines 1-5) are what I'd expect: the MIME part
headers as I intended them to look.
- The second block (lines 7-11) were not output by my code.
- The third block is the first header line in the message
forming the contents of this MIME part.
The message id in the second block, which contains the hostname of the
laptop I'm reading the mail on, indicates that this block was injected
somewhere between my Postfix MTA and my Evolution MUA. The transfer path
is JavaMail -> Postfix -> Dovecot -> Evolution. So, what I'd appreciate
help with are the following:
- what has added these headers?
Postfix isn't doing it: I've looked at the message (captured by
an 'always_bcc' instruction in Postfix). The 2nd block of
headers isn't present in the BCC copy.
Some time back I tested this code and then put it to one side.
It used to work correctly. About all that has changed since then is
that Dovecot has been upgraded. I have not upgraded Java, JavaMail
or Evolution during this period.
- is there anything I could/should be doing to the MIME part to prevent
downstream processes from mutilating my attachment?
Is there a way to encode the attachment so that it doesn't look like a
message (e.g Base64 encoding) but that will still allow a mail reader
to view it inline? If so, I'd like to know the name of the Java class
and package.
send a MIME message containing:
1)a text/plain part containing text describing the following part
2)a message/rfc822 part containing a complete mail message which
can be MIME or plain text.
The message is being built and dispatched OK - I can read it with
Evolution, see the plain text and open the attachment, but the latter is
somewhat mangled. Here's the code I'm using the assemble the part.
'headerText' contains the headers for the message that will form the
attachment, 'bodytext' contains the complete MIME body of the message
and 'name' contains the name to be used for the attachment (its actually
the subject line). The body and headers are separated because that was
the easiest way to retrieve them from a JavaMail Message. Here's the
method that builds the part:
public boolean addMessagePart(String headerText,
String bodyText,
String name)
{
boolean success = true;
MimeBodyPart b = null;
StringBuffer s = new StringBuffer();
String mt = new String("message/rfc822");
String fn = null;
String cv = null;
if (name.length() == 0)
{
fn = new String("no_name.eml");
cv = new String(mt);
}
else
{
fn = new String(name + ".eml");
cv = new String(mt + "; name=\"" + fn + "\"");
}
try
{
s.append(headerText);
s.append("\n\n");
s.append(bodyText);
b = new MimeBodyPart();
b.setText(s.toString());
b.setDisposition("attachment");
b.setFileName(fn);
b.removeHeader("Content-Type");
b.addHeader("Content-Type", cv);
body.addBodyPart(b); /* body is a class member */
msgReady = true; /* msgReady is a class member */
}
catch (MessagingException e)
{
error = e.getMessage() + " adding message part";
success = false;
}
return success;
}
However, when I look at the attachment with Evolution it is displayed as
plain text, not an attached mail message. This is evidently due to
additional headers that have been injected somehow. Here's what I get on
the receiving end:
------=_Part_0_26953436.1190840435551
Content-Type: message/rfc822; name="Revision to Your Alibris E-mail
Address.eml"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="Revision to Your Alibris
E-mail Address.eml"
From:
Date: Wed, 26 Sep 2007 22:00:50 +0100
Subject: No Subject
Customer Service <[email protected]>>From Alibris Customer Service <[email protected]>: From Alibris
.........
Analysing this part of the message, which is the start of the part
containing the attached message:
- The first block (lines 1-5) are what I'd expect: the MIME part
headers as I intended them to look.
- The second block (lines 7-11) were not output by my code.
- The third block is the first header line in the message
forming the contents of this MIME part.
The message id in the second block, which contains the hostname of the
laptop I'm reading the mail on, indicates that this block was injected
somewhere between my Postfix MTA and my Evolution MUA. The transfer path
is JavaMail -> Postfix -> Dovecot -> Evolution. So, what I'd appreciate
help with are the following:
- what has added these headers?
Postfix isn't doing it: I've looked at the message (captured by
an 'always_bcc' instruction in Postfix). The 2nd block of
headers isn't present in the BCC copy.
Some time back I tested this code and then put it to one side.
It used to work correctly. About all that has changed since then is
that Dovecot has been upgraded. I have not upgraded Java, JavaMail
or Evolution during this period.
- is there anything I could/should be doing to the MIME part to prevent
downstream processes from mutilating my attachment?
Is there a way to encode the attachment so that it doesn't look like a
message (e.g Base64 encoding) but that will still allow a mail reader
to view it inline? If so, I'd like to know the name of the Java class
and package.