簡體   English   中英

使用javamail設置標題順序

[英]Set Header order using javamail

我想設置MIME消息的標題順序。 我嘗試實現mimemessage和重寫writeTo方法。 但是,即時通訊無法理解它是如何工作的。 我已發送了示例消息,但最終兩次收到了標頭。 誰能幫我澄清一下。 下面是我的Message類。

public class MyMessage extends MimeMessage{
    private String subject;
    private String encodingtype;
    public MyMessage(Session session) {
        super(session);
        this.session=session;
    }
    @Override
    public void writeTo(OutputStream out) throws java.io.IOException, MessagingException{
        try{
            String replyto = ("\""+displayname+"\" <"+displayfrom+">");
            String fromheader = ("\""+displayname+"\" <"+mailfrom+">");
            out.write(("Date: "+new Date()+"\r\n").getBytes("UTF-8"));
            out.write(("From: "+fromheader+"\r\n").getBytes("UTF-8"));
            out.write(("Reply-To: "+replyto+"\r\n").getBytes("UTF-8"));
            out.write(("To: "+getAddress(email)+"\r\n").getBytes("UTF-8"));
            out.write(("Content-Type: text/html; charset=\"UTF-8\"\r\n").getBytes("UTF-8"));
            out.write(("Content-Transfer-Encoding: "+encodingtype+"\r\n").getBytes("UTF-8"));
            out.write("\r\n".getBytes("UTF-8"));
            out.write("<html><body><h1>HI</h1></body></html>\r\n".getBytes("UTF-8"));
        }catch(Exception e){
            System.out.println(e);
        }
    }
}

提前致謝。

嗯,為什么需要控制標題順序?

默認情況下,JavaMail將按照Internet RFC建議的順序放置眾所周知的標頭。 如果出於某些合理原因將標頭以不同的順序放置,則可以對MimeMessage進行子類化,並重寫createInternetHeaders方法以提供InternetHeaders類的自己的子類,該子類將標頭以所需的順序進行排列。

或者,您可以子類化MimeMessage並僅重寫writeTo方法以按所需順序獲取和輸出標頭。 您可能會發現查看MimeMessage源代碼很有幫助。

最后,我可以設置標題順序。 非常感謝Bill Shannon。 謝謝您的幫助。 下面是我的消息類。

public MyMessage(Session session, String fromdomain, String format, 
        String blastid, String listid, String offerid, int blastinstanceid,
        String displayname, String displayfrom, String mailfrom, String email, String subject,
        String encodingtype, String content) {
        super(session);
        this.session=session;
        this.fromdomain = fromdomain;
        this.format = format;
        this.blastid = blastid;
        this.listid = listid;
        this.offerid = offerid;
        this.blastinstanceid = blastinstanceid;
        this.displayname = displayname;
        this.displayfrom = displayfrom;
        this.mailfrom = mailfrom;
        this.email = email;
        this.subject = subject;
        this.content = content;
        try{
            setFrom(getAddress(displayfrom));
            setSentDate(new Date());
            setRecipients(RecipientType.TO, email);
            setSubject(subject);
            setReplyTo(getAddress2(mailfrom));
            setHeader("Message-Id", getUniqueMessageIDValue(session, 
fromdomain, format, blastid, listid, offerid, blastinstanceid));
        }catch(Exception e){
            System.out.println(e);
        }
    }
    @Override
    public void writeTo(OutputStream out, String[] ignoreList) throws 
java.io.IOException, MessagingException{
        LineOutputStream los = null;
        try{
            if (!saved)
                saveChanges();

        String replyto = ("\""+displayname+"\" <"+displayfrom+">");
        String fromheader = ("\""+displayname+"\" <"+mailfrom+">");
        los = new LineOutputStream(out);
        los.writeln("Date: "+getHeader("Date", null));
        los.writeln("Message-Id: " +getHeader("Message-Id",null).toString());
        los.writeln("From: "+fromheader);
        los.writeln("Reply-To: "+replyto);
        los.writeln("To: "+getHeader("To",",").toString());
        System.out.println("From header is "+getHeader("From",",")+" mail from is "+mailfrom);
        //out.write(Message.RecipientType.TO, getAddress(email));
        los.writeln("subject: "+getHeader("Subject",null).toString());

        Enumeration hdrLines = getNonMatchingHeaderLines(ignoreList);
        while (hdrLines.hasMoreElements())
            los.writeln((String)hdrLines.nextElement());
        los.writeln();
    }catch(Exception e){
        System.out.println(e);
    }finally{
        try{
            if(los != null) los.flush();
        }catch(Exception e){
            System.out.println(e);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM