java 阿拉伯语_使用javamail API阅读邮件时的阿拉伯语内容问题

用户在使用JavaMail API从Gmail服务器读取包含阿拉伯语内容的邮件时遇到问题,接收到的内容显示为“??????”。尝试了将内容转为UTF-8编码但仍未能正确显示。代码片段展示了获取邮件内容和处理过程,包括检查MIME类型、读取输入流等。问题更新中提供了发送邮件的配置,确认邮件发送时已经设置了UTF-8编码。
摘要由CSDN通过智能技术生成

我正在使用

Javamail API从Gmail服务器读取邮件.我发送的邮件中包含从gmail ID到其他Gmail ID的阿拉伯语内容.邮件的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;

}

}

这对我有用:

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值