How to override a unique default boundary line and create my own border using MimeMultipart JavaMail?

I have a web application that I use that expects a specific boundary line like ("company mime border").

I have not found a way to override the default MimeMultipart behavior when I do

Multipart mp = new MimeMultipart();

A unique boundary line is always created by the constructor, and I want to override this behavior to have my own boundary line, but I could not do it because I did not find any API.

Even if I set it in the content type, it does not work and always creates a unique boundary line, since MimeMultipart creates by default.

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

Can anyone suggest / help me with this.

How to override this default behavior?

+4
source share
1 answer

From javax.mail.Multipart :

The mail.mime.multipart.ignoreexistingboundaryparameter System mail.mime.multipart.ignoreexistingboundaryparameter can be set to true to ignore any border and instead look for the border in the message

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

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

I have not implemented it, but I'm sure it can work

Update

Try subclassing the MimeMultipart class and overwrite getBoundaryMethod() . See the 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(); } } 
+3
source

Source: https://habr.com/ru/post/1444127/


All Articles