本文介绍了如何使用MimeMultipart的JavaMail覆盖默认的唯一边界字符串并创建自己的边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用程序,我使用它需要一个特定的边界字符串(公司mime边界)。



我没有找到一种方法来覆盖MimeMultipart的默认行为,当我这样做时



Multipart mp = new MimeMultipart();



唯一的边界字符串总是由构造函数创建,我想覆盖此行为以拥有自己的边界字符串,但由于找不到任何API而无法执行此操作。



即使我在内容类型中设置它,它也不起作用并且创建一个唯一的边界字符串,因为MimeMultipart正在创建一个默认的边界字符串。



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



任何人都可以建议/帮助我。



如何覆盖此默认行为?

解决方案

来自:

尝试将此属性设置为 true 然后使用



<$ p添加您自己的属性$ p> mimeMsg.setHeader( 内容类型, );

我还没有实现它,但我相信它可以工作



更新



尝试对MimeMultipart类进行子类化并覆盖 getBoundaryMethod()。请参阅下面的示例代码:

  import javax.activation.DataSource; 
import javax.mail.MessagingException;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeMultipart;
公共类MyMimeMultyPart扩展了MimeMultipart {
/ **
*提供InputStream的DataSource。
* /
protected DataSource ds;

/ **
*表示数据是否已被解析。
* /
protected boolean parsed = true;

私有ContentType类型;

public MyMimeMultyPart(DataSource dataSource)抛出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的());
返回buf.toString();
}
}


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 ?

解决方案

From javax.mail.Multipart :

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();
    }
}

这篇关于如何使用MimeMultipart的JavaMail覆盖默认的唯一边界字符串并创建自己的边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 15:13