0

I read the template which contains the Swedish text(vår, än, tall) . then I try to send a mail . But while getting mail Question Mark(?) is observed in place of special or accent character . In Windows it work fine but in Linux it’s not support. I have used content type as :

text/html; charset=ISO-8859-1  (Windows work fine but in Linux it does not support)
text/html;charset=utf-8               (Windows work fine but in Linux it does not support)
text/x-vcard; charset=utf-8
text/plain; charset=ISO-8859-1; format=flowed

I set content Type as CONTENT_TYPE = "text/html;charset=utf-8"; Is there any solution ,to get proper mail.

private final static String CONTENT_TYPE = "text/html; charset=ISO-8859-1";
Message msg = new MimeMessage(session);
msg.setContent(message, CONTENT_TYPE);
System.setproperty("utf8");

I am getting ? in place of accent characters in Linux Env.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 1
    `System.setproperty("utf8");` won't help you as it's not linked to Email `Message` whatsoever. – Buhake Sindi Jun 17 '11 at 05:35
  • Agreeing with @ The Elite Gentleman. Apparently, it has to be `System.setProperty(key,value);` for it to make any sense in the first place. And it is meaningless in the context of email messages. – Vineet Reynolds Jun 17 '11 at 05:38
  • 1
    I think you ought to post the code that prepares the contents of the message. See [this question](http://stackoverflow.com/questions/773264/java-mail-problem-with-turkish-characters) for instance, where the problem description contains the message content. Also, you might want to clarify about what you mean by " In Windows it work fine but in Linux it’s not support". Is it the code that is generating an invalid message, or is it your email reader that cannot understand the message. – Vineet Reynolds Jun 17 '11 at 05:54
  • Is this a client problem or a sender problem? – Donal Fellows Jun 17 '11 at 08:25
  • After all, if the problem is just that a particular Linux system has a poor set of fonts installed, this isn't a SO question. (I suspect that the email message itself is correct.) – Donal Fellows Jun 17 '11 at 08:27

2 Answers2

1

If you want your String to be encoded correctly, you should use one of the methods:

MimeMessage.setText(String text, String charset) 
MimeMessage.setText(String text, String charset, String subtype)

which allow you to specify the charset of your text (i.e. "utf-8") and additionally the subtype (e.g. "plain", as in "text/plain")

Ron
  • 682
  • 6
  • 7
1

Actually problem was while read the text.b/c inputstreamreader was reading the file with UTF-8 character set which provided by linux as default.that's why question marks are observed in place of accent characters. And in windows inputstreamreader use characterset as MS-1252 which also support swedish text. that's why mail was coming proper in windows.So i come to know that always find the root cause problem was not to send a mail .problem was in reading the file.

For future reference, Analysis of the issue is as follows:

Root Cause: Input stream reader of java takes the default character set based on operating system if not specified explicitly. While trying to read the file using input stream reader of java: • For UNIX system: Default character set is “UTF-8” which do not support Swedish (Latin) characters. • For Windows system: Default character set is “MS-1252” which supports Latin characters.

Solution: Set the correct character set while initializing the input stream reader in java for reading the file from file system. This will avoid defaulting of character set based on operating system.

Use constructor (suggested): InputStreamReader(InputStream in, String charsetName)

Generally used constructor (avoid for supporting different charsets): InputStreamReader(InputStream in)

Sample code: new InputStreamReader(bfInputStream, CHARACTER_SET); where CHARACTER_SET = “ISO-8859-1”

  • Actually when web logic server runs in windows then mail get properly because windows use bydefault character set as MS-1252.But when weblogic server runs in Linux OS then OS use character set as UTF-8 that's why ? observed in mail.So explicitly need to specify the charSet to InputstreamReader as ISO-8859-1 which support swdish text. – Amit Thakur Jun 17 '11 at 13:40
  • 1
    UTF-8 is not a character set in of itself, it's an *encoding* of the Unicode character set, which most certainly contains Latin characters. The issue is just that characters at code points 128-255 are not going to be encoded as single bytes, but as multi-byte characters. If you use UTF-8 on both sides, it will work as well. UTF-8 isn't your problem, your problem is just that you were writing your text using one encoding and then reading it back using another encoding. – Daniel Pryden Sep 29 '11 at 16:24