gpt4 book ai didi

java - 使用 javamail API 阅读邮件时出现阿拉伯语内容问题

转载 作者:搜寻专家 更新时间:2023-11-01 03:43:15 25 4
gpt4 key购买 nike

我正在使用 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;
}

关于java - 使用 javamail API 阅读邮件时出现阿拉伯语内容问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9407922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com