如何将MimeBodyPart ContentType设置为“text / html”? [英] How to set MimeBodyPart ContentType to "text/html"?
问题描述
下面的程序显示了HTML multipart mime类型的意外返回值。为什么这个程序打印text / plain而不是text / html?
The program below shows an unexpected return value for HTML multipart mime type. Why does this program print "text/plain" and not "text/html"?
public class Main {
public static void main(String[] args) throws javax.mail.MessagingException, java.io.IOException {
javax.mail.internet.MimeBodyPart mime_body_part = new javax.mail.internet.MimeBodyPart();
mime_body_part.setContent("<h1>foo</h1>", "text/html");
System.out.println(mime_body_part.getContentType());
}
}
我尝试过多种替代方法,包括设置包裹的ByteArrayDataSource在DataHandler中,但无济于事。当我用MimeMessage而不是MimeBodyPart尝试这个时,会发生同样的事情。
I have tried numerous alternative ways including setting a ByteArrayDataSource wrapped in a DataHandler, but to no avail. The same thing happens when I try this with a MimeMessage instead of a MimeBodyPart.
要在Linux上编译和运行:
To compile and run on Linux:
javac -classpath .:activation.jar:mail.jar Main.java
java -classpath .:activation.jar:mail.jar Main
推荐答案
致电 MimeMessage.saveChanges()
,这将通过将MIME结构级联到您身体部位的 MimeBodyPart.updateHeaders()
的调用来更新标题。这是 updateHeaders
调用,它将内容类型从 DataHandler
传输到部件的MIME 内容 - 键入
标题。
Call MimeMessage.saveChanges()
on the enclosing message, which will update the headers by cascading down the MIME structure into a call to MimeBodyPart.updateHeaders()
on your body part. It's this updateHeaders
call that transfers the content type from the DataHandler
to the part's MIME Content-Type
header.
当您在内部设置 MimeBodyPart
,JavaMail的内容时(并且显然不会创建一个 DataHandler
对象包装您传入的对象。该部分的 Content-Type
标头是不立即更新。
When you set the content of a MimeBodyPart
, JavaMail internally (and not obviously) creates a DataHandler
object wrapping the object you passed in. The part's Content-Type
header is not updated immediately.
在测试程序中没有直接的方法,因为你没有包含 MimeMessage
和 MimeBodyPart.updateHeaders()
不是 public
。
There's no straightforward way to do it in your test program, since you don't have a containing MimeMessage
and MimeBodyPart.updateHeaders()
isn't public
.
这是一个充分说明预期和意外输出的工作示例:
Here's a working example that illuminates expected and unexpected outputs:
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() );
}
}
这篇关于如何将MimeBodyPart ContentType设置为“text / html”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!