• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem when sending non english characters through Java Mail API

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can we do in this case?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then use one of the other two code snippets I provided.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic