Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem when sending non english characters through Java Mail API

 
Badari Kandepi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like the encoding isn't set correctly somewhere along the way. How are you handling that?
 
Badari Kandepi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....";
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using "new String()" without specifying the encoding is always suspect.
 
Badari Kandepi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can we do in this case?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Badari Kandepi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then use one of the other two code snippets I provided.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic