Noel,
I have successfully reformatted the body attachment message, and have used
the Mail.setMessage(MimeMessage) method to modify the original multipart
message content. When I then referred to the actual file content it still
had the body attachment line length of 60? Why hasn't the method updated the
message content with the newly recreated version with the 76 line length
reformat using the mentioned method?
Please find the following mailet which does the actual reformatting:
public class ReformatAttachment extends GenericMailet {
/*public void init() throws MessagingException {
}*/
/**
* Reformats all body attachment line lengths to a max of 76 chars
* as per SMTP standard.
*/
public void service(Mail mail) throws MessagingException {
MimeMessage message = mail.getMessage();
if (message.isMimeType("multipart/*"))
{
//Create the reply message
MimeMessage reply = new
MimeMessage(Session.getDefaultInstance(System.getProperties(), null));
//Copy all headers
Enumeration heads = message.getAllHeaders();
while (heads.hasMoreElements()) {
Header head = (Header)heads.nextElement();
if (head.getValue() != null)
reply.addHeader(head.getName(),
head.getValue());
}
try {
//Create the message body
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart origPart = null;
int c = 0;
for (MimeMultipart multi =
(MimeMultipart)message.getContent(); c <
multi.getCount(); c++)
{
MimeBodyPart part = new MimeBodyPart();
origPart = (MimeBodyPart)multi.getBodyPart(c);
//Copy all part's headers
Enumeration partheads =
origPart.getAllHeaders();
if (partheads != null)
{
while (partheads.hasMoreElements()) {
Header header =
(Header)partheads.nextElement();
if (header.getValue() != null)
part.addHeader(header.getName(), header.getValue());
}
}
if (origPart.isMimeType("text/*") ||
"text/*".equalsIgnoreCase(origPart.getContentType()))
{
part.setContent(origPart.getContent(),
origPart.getContentType());
}
// ...else reformat content line lengths to 76
max chars
else
{
//Create the message
ByteArrayOutputStream bout = new
ByteArrayOutputStream();
OutputStream out = (OutputStream) bout;
InputStream in = null;
//InputStream in = origPart.getInputStream();
Object obj = origPart.getContent();
int i = 1;
int b = -1;
if (obj instanceof InputStream)
{
String enc = origPart.getEncoding();
in =
MimeUtility.decode((InputStream)obj, enc);
//Read in every byte
while ((b = in.read()) != -1)
{
if ( i == 77 ) {
out.write(10);
i = 1;
}
if ( b != 10 )
{
out.write(b);
i++;
}
}
out = MimeUtility.encode(out, enc);
out.flush();
out.close();
} // End if
//create the datasource
MimeBodyPartDataSource mbpds =
new MimeBodyPartDataSource(
origPart.getContentType(),
origPart.getFileName(),
bout.toByteArray()
);
part.setDataHandler(new DataHandler(mbpds));
part.setFileName(origPart.getFileName());
part.setDisposition("Attachment");
} // End if
multipart.addBodyPart(part);
} // End for
reply.setContent(multipart);
reply.setHeader("Content-Type",
multipart.getContentType());
} catch (IOException ioe) {
ioe.printStackTrace();
throw new MailetException("Unable to create multipart
body");
}
//Pass the revised MimeMessage for further processing...
mail.setMessage(reply);
System.out.println("step 6...");
} // End if
}
public String getMailetInfo() {
return "ReformatAttachment Mailet";
}
}
And the auxiliart class:
public class MimeBodyPartDataSource
implements DataSource {
//instance attributes
private String m_Type;
private String m_Name;
private byte[] m_Data;
/**
* Constructs a <code>MimeBodyPartDataSource</code>
* instance.
*
* @param type the content type of the constructed instance
* as <code>String</code>.
* @param name the name of the constructed instance
* as <code>String</code>.
* @param data the data of the constructed instance as
* <code>byte[]</code>.
*
* @return the newly constructed <code>MimeBodyPartDataSource
* </code>.
*/
public MimeBodyPartDataSource(
String type, String name, byte[] data) {
m_Type=type;
m_Name=name;
m_Data=data;
}//constructor
/**
* Returns the content type of this instance as
* <code>String</code>.
* <p>(DataSource implementation)
*
* @return the content type as <code>String</code>
*/
public String getContentType(){
return m_Type;
}//getContentType
/**
* Returns the name of this instance as <code>String</code>.
* <p>(DataSource implementation)
*
* @return the name as <code>String</code>
*/
public String getName(){
return m_Name;
}//getName
/**
* Returns the data of this instance as <code>
* InputStream</code>.
* <p>(DataSource implementation); wraps the data
* into a <code>ByteArrayInputStream</code>.
*
* @return the data of this instance as
* <code>InputStream</code>.
* @throws IOException if impossible.
*/
public InputStream getInputStream()
throws IOException {
return new ByteArrayInputStream(m_Data);
}//getInputStream
/**
* Throws an IOException in this implementation, because
* this instance represents a read-only <code>DataSource</code>.
* <p>(DataSource implementation)
*
* @return an <code>OutputStream</code> instance for writing
* to this <code>DataSource</code>.
* @throws IOException if impossible.
*/
public OutputStream getOutputStream()
throws IOException {
throw new IOException(
"Not supported."
);
}//getOutputStream
}//class MimeBodyPartDataSource
I would appreciate your input prior to patch submission.
Thanks in advance.
Sam.
>From: "Noel J. Bergman" <[EMAIL PROTECTED]>
>Reply-To: "James Users List" <[EMAIL PROTECTED]>
>To: "James Users List" <[EMAIL PROTECTED]>
>CC: "James-Dev Mailing List" <[EMAIL PROTECTED]>
>Subject: Matcher/Mailet Library
>Date: Tue, 14 May 2002 13:20:32 -0400
>
> > I have resolved this issue throught the creation of special processing
> > mailet which reformats all binary content type mimebodypart attachments
>to
>a
> > fixed length of 76 characters. This was needed for IMAP protocol
>processing
> > because it cannot handle 60 character widths sent from Hotmail.com,
> > Yahoo.com, Fastmail.fm mail servers. If you want I can submit this
>mailet
> > and associating config settings. Without this patch, JAMES IMAP server
>will
> > not be able to read in mimemultipart body attachments properly.
>
>My suggestion is yes, please do submit it.
>
>On a related note, is anything being done about a repository/library of
>contributed matchers and mailets? That would be quite handy, since I
>presume that not everyone's matcher and mailet will be added to the
>distribution. If not, I will consider undertaking that as a project.
>
>I envision storing the library in a database. What fields would people
>want?
>
> - name of component
> - description
> - version
> - status (stable, unstable, ...)
> - version of James necessary
> - author (valid e-mail required)
> - homepage (if the author will maintain an online presence)
> - download (.ZIP or .TAR distribution)
>
>Thoughts?
>
> --- Noel
>
>
>--
>To unsubscribe, e-mail:
><mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail:
><mailto:[EMAIL PROTECTED]>
>
_________________________________________________________________
MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>