aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes problem when sending non english characters through Java Mail API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "problem when sending non english characters through Java Mail API" Watch "problem when sending non english characters through Java Mail API" New topic
Author

problem when sending non english characters through Java Mail API

Badari Kandepi
Greenhorn

Joined: Feb 01, 2013
Posts: 4
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
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
Sounds like the encoding isn't set correctly somewhere along the way. How are you handling that?


Ping & DNS - my free Android networking tools app
Badari Kandepi
Greenhorn

Joined: Feb 01, 2013
Posts: 4
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
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
Using "new String()" without specifying the encoding is always suspect.
Badari Kandepi
Greenhorn

Joined: Feb 01, 2013
Posts: 4
how can we do in this case?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Badari Kandepi
Greenhorn

Joined: Feb 01, 2013
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

Then use one of the other two code snippets I provided.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem when sending non english characters through Java Mail API