4

I have a web-app which I use which expects a specific boundary string like (“company mime boundary”).

I did not find a way to override the default behavior of MimeMultipart, when I do

Multipart mp = new MimeMultipart();

A unique boundary string is always created by the constructor and I want to override this behavior to have my own boundary string, but unable to do so as I did not find any API.

Even if I set it in content-type, it does not work and creates a unique boundary string always as the MimeMultipart is creating a default one.

mimeMsg.setHeader("Content-Type","multipart/mixed;boundary="company mime boundary");

Can anyone please suggest/help me on this.

How to override this default behavior ?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Sandeep
  • 199
  • 1
  • 4
  • 13

1 Answers1

4

From javax.mail.Multipart :

The mail.mime.multipart.ignoreexistingboundaryparameter System property may be set to true to cause any boundary to be ignored and instead search for a boundary line in the message

Try setting this property to true and then add your own using

mimeMsg.setHeader("Content-Type","");

I have not implemented it, but I am confident it can work

update

Try sub-classing the MimeMultipart class and overwrite the getBoundaryMethod(). See some sample code below:

import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeMultipart;
public class MyMimeMultyPart extends MimeMultipart {
    /**
     * DataSource that provides our InputStream.
     */
    protected DataSource ds;

    /**
     * Indicates if the data has been parsed.
     */
    protected boolean parsed = true;

    private ContentType type;

    public MyMimeMultyPart(DataSource dataSource) throws MessagingException {
        super(dataSource);
    }

    public MyMimeMultyPart(String subtype) {
        type = new ContentType("multipart", subtype, null);
        type.setParameter("boundary", getBoundary());
        contentType = type.toString();
    }

    public MyMimeMultyPart() {
        super();
    }

    private static int part;

    private synchronized static String getBoundary() {
        int i;
        synchronized (MimeMultipart.class) {
            i = part++;
        }
        StringBuffer buf = new StringBuffer(64);
        buf.append("----=_Part_").append(i).append('_').append((new Object()).hashCode()).append('.').append(System.currentTimeMillis());
        return buf.toString();
    }
}
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • Unfortunately, it does not work, i still see `------=_Part_1_89627259.1352127376871` gets added even after setting that property :( – Sandeep Nov 05 '12 at 15:01
  • Should we not add boundary in the same line like this? `Content-Type: multipart/mixed;boundary="company mime boundary" ` – Sandeep Nov 05 '12 at 15:02
  • 1
    Hm, reading some more about it I think it should be better to try and subclass the MimeMultipart class. See also this http://stackoverflow.com/questions/9081739/altering-a-multipart-xxx-content-type-without-altering-the-underlying-parts . It might help you – MaVRoSCy Nov 05 '12 at 15:16
  • Perfect @MaVRoSCy !! Can't thank you enough !!, only override of the method works. – Sandeep Nov 05 '12 at 17:24