Arabic content problem when reading mail using JavaMail API

I'm using the JavaMail API to read mail from the Gmail server My email contains Arabic content from Gmail ID to other Gmail IDs The charset encoding type of the message is windows-1256 When I use JavaMail to download mail, I receive " The content format is not Arabic characters I converted the downloaded content to UTF - 8 format, but I still didn't get the correct display

Thank you in advance,

Tim

Update: I use the following code to get the content:

Object content = message.getContent(); 
if (message.isMimeType("text/html")  
      || message.isMimeType("text/plain")) { 
      Al = (String) content; 
}

After downloading the content, the following code is used for UTF-8 encoding:

byte[] utf8Bytes = s.getBytes("UTF-8"); 
s = new String(utf8Bytes,"UTF-8");

Update: the complete code I currently use to read email content

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,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;
    }
}

Solution

This works for me:

public boolean sendEmail(String sender,String recipient,String subject,String body)
    {
        try
        {
            // set properties of mail server
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

            Properties props = new Properties();
            props.setProperty("mail.transport.protocol","smtp");
            props.setProperty("mail.host","smtp.gmail.com");
            props.put("mail.smtp.auth","true");
            props.put("mail.smtp.port","465");
            props.setProperty("charset","utf-8");
            props.put("mail.smtp.socketFactory.port","465");
            props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback","false");
            props.setProperty("mail.smtp.quitwait","false");

            // connect to mail server
            javax.mail.Session session = javax.mail.Session.getDefaultInstance(props,new javax.mail.Authenticator()
            {
                protected PasswordAuthentication getpasswordAuthentication()
                { return new PasswordAuthentication(gmailUser,gmailPassword);   }  
            });

            // create email
            MimeMessage message = new MimeMessage(session);
            message.setSender(new InternetAddress(sender));
            message.setSubject(subject,"UTF-8");
            message.setContent(body,"text/plain; charset=utf-8");
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(recipient));

            // send email
            Transport.send(message);

            return true;
       }
       catch (Exception e)
       {
          System.out.println("Exception thown "+e.getMessage());
          return false;
       }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>