Я использую Javamail API для чтения почты с сервера Gmail. Я отправляю письмо с арабским контентом с идентификатора Gmail на другой идентификатор Gmail. Тип кодировки Charset для почты - windows-1256. Когда я загружаю почту, используя Javamail, я получаю контент в "??????" формат вместо арабских символов. Я конвертирую загруженный контент в формат UTF-8, но все еще не получаю надлежащее отображение.
Заранее спасибо,
Тим
Обновление:
Я использую следующий код для извлечения контента:
Object content = message.getContent();
if (message.isMimeType("text/html")
|| message.isMimeType("text/plain")) {
Al = (String) content;
}
После загрузки содержимого для кодировки UTF-8 используется следующий код:
byte[] utf8Bytes = s.getBytes("UTF-8");
s = new String(utf8Bytes, "UTF-8");
Обновление: полный код, который я использую для чтения содержимого почты в настоящее время
String gmailMultipartMailDownload(Multipart multipart, String Uids)
throws SocketException, UnsupportedDataTypeException, UnsupportedEncodingException {
String Content = new String("");
try {
int numParts = multipart.getCount();
for (int k = 0; k < numParts; k++)
{
BodyPart bodyPart = multipart.getBodyPart(k);
Object tmp = null;
try
{
tmp = bodyPart.getContent();
}
catch (UnsupportedDataTypeException UEE)
{
try {
InputStream is = bodyPart.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
{
sb.append(line + "\n");
}
br.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException UEE) {
UEE.printStackTrace();
try {
InputStream is = bodyPart.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
{
sb.append(line + "\n");
}
br.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
String disposition = bodyPart.getDisposition();
if (tmp instanceof InputStream) {
try{
if( super.Downloadfiles(bodyPart, hashCode, attnumcount,this.getgmailattachmentfolder()))
{
//Download Attachments
}
}catch (FileNotFoundException err) {
return Content;
}
catch(IOException fex){
fex.printStackTrace();
return Content;
}
}
else if (disposition != null
&& (disposition.equals(BodyPart.ATTACHMENT) || disposition.equals(BodyPart.INLINE) || disposition.equals("ATTACHMENT"))) {
try{
if( super.Downloadfiles(bodyPart, hashCode, attnumcount,this.getgmailattachmentfolder()))
{
//Download Attachments
}
}catch (FileNotFoundException err) {
System.out.println(err.getMessage());
return Content;
}
catch(IOException fex){
fex.printStackTrace();
return Content;}
} else if (bodyPart.isMimeType("multipart/*")) {
Multipart mp = (Multipart) bodyPart.getContent();
Content += gmailMultipartMailDownload(mp, Uids);
} else if (bodyPart.isMimeType("text/html")) {
Content += bodyPart.getContent().toString();
}
}
}
catch (Exception Ex) {
System.out.println("Content object error is "+Ex);
return Content;
} finally {
return Content;
}
}