I have unicode for Simplified Chinese. I would like to send an email with Chinese subject and content. The content works fine-which converts unicode to Chinese, however the subject alwasys shows unicode instead of Chinese. Can anybody help? Enclosed you can see a part of my code:

session = Session.getInstance(props, null);
		
		MimeMessage message = new MimeMessage( session );
		
		message.setHeader("Content-Type", "text/plain; charset=UTF-8");


		message.setFrom( new InternetAddress( from ) ); 
		message.addRecipient( Message.RecipientType.TO, new InternetAddress( to ) ); 
		message.setSubject(subject, "text/plain; charset=utf-8"); 
 
		BodyPart messageBodyPart = new MimeBodyPart(); 
		messageBodyPart.setContent( content, "text/html; charset=utf-8" ); 

		Multipart multipart = new MimeMultipart(); 

		multipart.addBodyPart( messageBodyPart ); 

		message.setContent( multipart ); 

		Transport.send( message );

Thanks,
MyDreamGirl

Dani AI

Generated

Short answer: the Subject header must contain real Unicode characters and be encoded with a proper charset name (for example "UTF-8"); passing a MIME Content-Type string or HTML entities into the Subject is the usual cause of what is seeing. JavaMail will produce a MIME-encoded Subject header when given a proper charset, but it cannot turn HTML numeric entities into characters by itself.

Practical checklist (most common fixes):

  • Make sure the Java String for the subject actually contains Chinese characters, not HTML numeric entities (decode entities first if needed; for example use an HTML unescape routine).

  • Call the API with a charset name, not a MIME type. For example:

    message.setSubject(subject, "UTF-8");

    or explicitly encode when required with MimeUtility.

  • For diagnosis, force the headers to be built and inspect the raw MIME to see what was actually sent:

    message.saveChanges();
    message.writeTo(System.out);

    Look for a Subject header like =?UTF-8?B?... to confirm proper encoding.

  • If the subject comes from a source (file, DB, HTTP), verify the decoding/encoding at the boundary (file saved/compiled as UTF-8, servlet parameter encoding, DB column charset, etc.). Also avoid manually setting the top-level Content-Type header on a multipart message; set charset on the body part.

As pointed out, headers default to ASCII unless encoded, so confirming the subject String and using the correct charset argument is usually enough. If the raw Subject header still looks wrong, paste that header for focused analysis.

Recommended Answers

All 5 Replies

I also tried following code and it does not work:

message.setSubject(MimeUtility.encodeText("亲爱的用户", "UTF-8", "B"));

Please help.

MyDreamGirl

a

nmaillet,

Do you want to say something?

I reviewed bioth links and I tried all possibilities and they do not work.

Any other suggestions?

MyDreamGirl

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.