MimeMessage.saveChanges()封闭的消息,这将通过将MIME结构层叠为
MimeBodyPart.updateHeaders()对您的身体部位的调用来更新标头。正是此
updateHeaders调用将内容类型从传输
DataHandler到了部分的MIME
Content-Type标头。
当设置a的内容时
MimeBodyPart,JavaMail会在内部(并且不是很明显)创建一个
DataHandler对象,该对象包装了传入的对象。部件的
Content-Type头 不会 立即更新。
在您的测试程序中,没有直接的方法可以执行此 *** 作,因为您没有包含
MimeMessage和
MimeBodyPart.updateHeaders()is
public。
这是一个说明预期和意外输出的工作示例:
public class MailTest { public static void main( String[] args ) throws Exception { Session mailSession = Session.getInstance( new Properties() ); Transport transport = mailSession.getTransport(); String text = "Hello, World"; String html = "<h1>" + text + "</h1>"; MimeMessage message = new MimeMessage( mailSession ); Multipart multipart = new MimeMultipart( "alternative" ); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText( text, "utf-8" ); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent( html, "text/html; charset=utf-8" ); multipart.addBodyPart( textPart ); multipart.addBodyPart( htmlPart ); message.setContent( multipart ); // Unexpected output. System.out.println( "HTML = text/html : " + htmlPart.isMimeType( "text/html" ) ); System.out.println( "HTML Content Type: " + htmlPart.getContentType() ); // Required magic (violates principle of least astonishment). message.saveChanges(); // Output now correct. System.out.println( "TEXT = text/plain: " + textPart.isMimeType( "text/plain" ) ); System.out.println( "HTML = text/html : " + htmlPart.isMimeType( "text/html" ) ); System.out.println( "HTML Content Type: " + htmlPart.getContentType() ); System.out.println( "HTML Data Handler: " + htmlPart.getDataHandler().getContentType() ); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)