英文:
I am developing one email client app for Android. i am having problem in MultiPart message body reading
问题
以下是翻译好的内容:
我已经为消息阅读开发了以下代码:
else if (p.isMimeType("multipart/*")) {
Log.i("snt", "22");
System.out.println("这是一个多部分消息");
System.out.println("---------------------------");
Multipart mp = (Multipart) p.getContent();
int count = mp.getCount();
//for (int i = 0; i < count; i++)
{
System.out.println("数量为:" + String.valueOf(count));
Multipart multipart = mp;//(Multipart) msg[i].getContent();
for (int x = 0; x < multipart.getCount(); x++) {
BodyPart bodyPart = multipart.getBodyPart(x);
Integer ctr = MainActivity.maxSlno();
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
System.out.println("邮件有附件:");
Log.i("snt", "33");
DataHandler handler = bodyPart.getDataHandler();
File root = Environment.getExternalStorageDirectory();
String path = root.getPath() + "/" + handler.getName();//+ "/PSattachment.psz";
File savedFile = new File(path);
String destFilePath = path;//.getFileName();
FileOutputStream output = new FileOutputStream(destFilePath);
InputStream input = bodyPart.getInputStream();
byte[] buffer = new byte[4096];
int byteRead;
while ((byteRead = input.read(buffer)) != -1) {
output.write(buffer, 0, byteRead);
}
output.close();
MainActivity.addAttachment(String.valueOf(ctr), path);//handler.getName()
System.out.println("文件名:" + handler.getName());
} else {
Log.i("snt", "44");
String str;//=bodyPart.toString();//(bodyPart.getContent());
str = bodyPart.getContent().toString();
Log.i("snt", str);
detail_mail.mbody.setText(str);
}
英文:
I have developed code for Message reading its as follows
else if (p.isMimeType("multipart/*")) {
Log.i("snt", "22");
System.out.println("This is a Multipart");
System.out.println("---------------------------");
Multipart mp = (Multipart) p.getContent();
int count = mp.getCount();
//for (int i = 0; i < count; i++)
{
System.out.println("Count is : " + String.valueOf(count));
Multipart multipart = mp;//(Multipart) msg[i].getContent();
for (int x = 0; x < multipart.getCount(); x++) {
BodyPart bodyPart = multipart.getBodyPart(x);
Integer ctr = MainActivity.maxSlno();
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
System.out.println("Mail have some attachment : ");
Log.i("snt", "33");
DataHandler handler = bodyPart.getDataHandler();
File root = Environment.getExternalStorageDirectory();
String path = root.getPath() + "/" + handler.getName();//+ "/PSattachment.psz";
File savedFile = new File(path);
String destFilePath = path;//.getFileName();
FileOutputStream output = new FileOutputStream(destFilePath);
InputStream input = bodyPart.getInputStream();
byte[] buffer = new byte[4096];
int byteRead;
while ((byteRead = input.read(buffer)) != -1) {
output.write(buffer, 0, byteRead);
}
output.close();
MainActivity.addAttachment(String.valueOf(ctr), path);//handler.getName()
System.out.println("file name : " + handler.getName());
} else {
Log.i("snt", "44");
String str;//=bodyPart.toString();//(bodyPart.getContent());
str = bodyPart.getContent().toString();
Log.i("snt", str);
detail_mail.mbody.setText(str);
}
Above code is downloading attachments but body of the mail is not being printed. Instead of email body its giving value like javax.mail.internet.MimeMultipart@f3f0d6c. Kindly help me in reading body part Multipart email message.
Thank you in advance for your help.
答案1
得分: 0
问题可能是您没有处理嵌套的多部分内容。请参阅msgshow.java演示程序,了解处理嵌套多部分内容的示例。
英文:
The problem might be that you're not handling nested multiparts. See the msgshow.java demo program for an example of how to handle nested multiparts.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论