Problems with MimeMessage.setSubject() and UTF8
Øyvind Harboe <[email protected]> Fri, 07 Nov 2003 16:38:04 +0100
| Newsgroups | gmane.comp.java.classpath.extensions.discuss |
|---|---|
| Organization | Zylin AS |
| Message-ID | <1068219484.17696.16.camel@famine> |
MimeMessage.setSubject() appears to not handle the case where a string
contains only e.g. Japaneese characters.
The culprit appears to be in MimeUntility.encodeWord().
asciiStatus() returns 1 on the string I pass in to setSubject()...
If I delete the check, the encoding happens correctly.
Øyvind
private static String encodeWord(String text, String charset,
String encoding, boolean word)
throws UnsupportedEncodingException
{
if (asciiStatus(text.getBytes())==1)
return text;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Send
{
public static void main(String[] args)
{
try
{
/** Set up session props and session. */
Properties sessionProps = System.getProperties();
String m_host="smtp.netpower.no";
sessionProps.put("mail.smtp.host", m_host);
Session m_session = Session.getDefaultInstance(sessionProps);
MimeMessage message = new MimeMessage(m_session);
String from="[email protected]";
message.setFrom(new InternetAddress(from));
String to="[email protected]";
message.addRecipients(Message.RecipientType.TO, to);
String subject="yvind spiser l";
String body="yvind spiser l ";
String str2="";
str2+=(char)0x79C1;
str2+=(char)0x306B;
str2+=(char)0x30D3;str2+=(char)0x30FC;str2+=(char)0x30EB;str2+=(char)0x304C;str2+=(char)0x3042;str2+=(char)0x3063;str2+=(char)0x3066;str2+=(char)0x3082;str2+=(char)0x3088;str2+=(char)0x3044;str2+=(char)0x304B;
subject+=str2;
body+=str2;
message.setSubject(subject, "UTF8");
message.setText(body, "UTF8");
Transport transport = m_session.getTransport("smtp");
transport.connect(m_host, "", "");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (AddressException e)
{
e.printStackTrace();
}
catch (NoSuchProviderException e)
{
e.printStackTrace();
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
}