Forums Register Login

problem when sending non english characters through Java Mail API

+Pie Number of slices to send: Send
Hi ,

I am trying to send some non english charactes using Java Mail API.
sending CancĂșnreenvĂ­e but i receviced like Canc??nreenv??e .
replacing question marks .
Can any one please give the solution for this?

Thanks in Advance
Badari Kandepi
+Pie Number of slices to send: Send
Sounds like the encoding isn't set correctly somewhere along the way. How are you handling that?
+Pie Number of slices to send: Send
I wrote like this

message.setSubject(msg.getSubject());

// Setting Attachment & Body content
Multipart multipart = new MimeMultipart();
BodyPart part1 = new MimeBodyPart();
part1.setContent(new String(msg.getBody().toByteArray()), "text/html" );
multipart.addBodyPart(part1);
List<Attachment> attachments=msg.getAttachments();
for(Attachment attachObj:attachments)
{
BodyPart part = new MimeBodyPart();
DataSource dc = new ByteArrayDataSource(attachObj.getBody().toByteArray(), attachObj.getContentType());
part.setDataHandler(new DataHandler(dc));
part.setFileName(MimeUtility.encodeText(attachObj.getFileName()));
multipart.addBodyPart(part);
}
message.setContent(multipart);
Thread.currentThread().setContextClassLoader( MailClient.class.getClassLoader());
Transport.send(message);
result = "Sent message successfully....";
+Pie Number of slices to send: Send
Using "new String()" without specifying the encoding is always suspect.
+Pie Number of slices to send: Send
how can we do in this case?
+Pie Number of slices to send: Send
You need to specify the charset. I believe the following will work:
Notice how I don't convert the body into a byte[] as that shouldn't be necessary. If it is however, you need to create a String with a specified encoding as well. In Java 7:
In Java 6 or before you need to use a String for the charset name and catch / rethrow the UnsupportedEncodingException, or create a Charset:
+Pie Number of slices to send: Send
 

Rob Spoor wrote:You need to specify the charset. I believe the following will work:
Notice how I don't convert the body into a byte[] as that shouldn't be necessary. If it is however, you need to create a String with a specified encoding as well. In Java 7:
In Java 6 or before you need to use a String for the charset name and catch / rethrow the UnsupportedEncodingException, or create a Charset:




it is not working for me . here msg.getBody() returns ByteArrayOutputStrem.please suggest me accordingly
+Pie Number of slices to send: Send
Then use one of the other two code snippets I provided.
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3870 times.
Similar Threads
Javax.mail.MessagingException
how to implement sending mail?
Mail API
How to send HTML page in mail insted of text
Sending strange characters in JavaMail
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:02:43.