2017-07-19 48 views
0

我想设置我的mime消息的标题顺序。我尝试过实现mimemessage并重写writeTo方法。但是,我无法理解它是如何工作的。我发送了一条示例消息,但最终收到了两次头文件。任何人都可以请帮我清楚这一点。以下是我的消息类。使用javamail设置标题顺序

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

在此先感谢。

回答

0

嗯,你为什么需要控制标题顺序?

默认情况下,JavaMail将按照Internet RFC建议的顺序放置众所周知的标头。如果您有一些合法的理由将标题置于不同的顺序,您可以继承MimeMessage子类并覆盖createInternetHeaders方法,以提供您自己的InternetHeaders类的子类,该子类将按您希望的顺序放置标题。

或者您可以继承MimeMessage,只是覆盖writeTo方法以按您想要的顺序获取和输出标题。您可能会发现查看MimeMessage source code有帮助。

+0

我试过这两种情况。但是,当我重写上述方法并设置所需的顺序时,标题被视为内容。例如,我已将** To **标头设置为值[email protected]。但是,当我发送电子邮件给javax.mail.SendFailedException:没有收件人地址异常。如果我明确设置收件人地址,我正在收到带有两个** To **标题的电子邮件。请帮我找到我失踪的地方。谢谢回复。 –

+0

我需要看看代码,看看你做错了什么。如果您不想在此发布,请通过[email protected]发送给我。 –

+0

非常感谢。将代码从[email protected]发送到[email protected]。等待您的回复。谢谢。 –

0

最后我可以设置标题的顺序。非常感谢比尔香农。谢谢您的帮助。以下是我的留言班。

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