我的代码存在问题。我尝试从我的邮件服务器获取电子邮件的文本。在检索邮件之后,我分析了邮件并尝试使用类型" text / plain"来获取bodypart。和/或" text / html"。 我使用以下方法来分割MimeMultipart:
private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws MessagingException, IOException{
String result = "";
int count = mimeMultipart.getCount();
BodyPart bodypart;
String html;
System.out.println("Number of parts: "+count);
for (int i = 0; i < count; i++) {
bodypart=mimeMultipart.getBodyPart(i);
System.out.println("Type of part "+(i+1)+" "+bodypart.getContentType());
System.out.println("Class: "+bodypart.getClass());
if(bodypart.isMimeType("text/plain")){
try{
//if(bodypart==null){
// System.out.println("Null in bodypart");
//}else{
System.out.println((String)bodypart.getContent());//cant get Content!
//}
}catch (IOException e){
System.out.println("IO Exception on attachment"+(i+1)+": "+e+"\n");
}
//result=(String) bodypart.getContent();
}else if (bodypart.isMimeType("text/html")){
try{
html = (String) bodypart.getContent();//cant get Content!
System.out.println(org.jsoup.Jsoup.parse(html).text());
//result = org.jsoup.Jsoup.parse(html).text();
}catch (IOException e){
System.out.println("IOException on part"+(i+1)+": "+e+"\n");
}
}else if (bodypart.isMimeType("multipart/*")){
System.out.println("Openning Multipart bodypart...!\n");
getTextFromMimeMultipart((MimeMultipart) bodypart.getContent());
}else{
System.out.println("No none part?!: "+bodypart.getContentType());
}
}
return result;
}
我的问题在于.getContent()。它总是抛出IOException:没有内容,尽管我知道我的邮件中有文字。
生成以下输出:
Trying:***mail Subject***...
-------------------------------------------------------------------------------
Number of parts: 1
Type of part 1 multipart/alternative;
boundary=_000_************************************************_(snippedout)
Class: class com.sun.mail.imap.IMAPBodyPart
Openning Multipart bodypart...!
Number of parts: 2
Type of part 1 text/plain; charset=Windows-1252
Class: class com.sun.mail.imap.IMAPBodyPart
IO Exception on attachment1: java.io.IOException: No content
Type of part 2 text/html; charset=Windows-1252
Class: class com.sun.mail.imap.IMAPBodyPart
IOException on part2: java.io.IOException: No content
我是否错过了至关重要的事情?我正在处理身体部位吗?
附加说明: 很抱歉没有提供通话功能。我确实打开了文件夹
Funktion来自:
public void findJunkonBody (String pfolder, String pdestfolder)throws MessagingException, IOException{
Folder folder ;
Store store = null;
Folder destfolder;
MailUtil mutil = new MailUtil(smtphost,user,pass,"imap");
String [] junktermsbody=new String[1];
junktermsbody[0]="Empfangsbestätigung";
try{
store=buildIMAPStore();
store.connect(smtphost,user,pass);
folder = store.getFolder(pfolder);
destfolder = store.getFolder(pdestfolder);
folder.open(Folder.READ_WRITE);
Message[] msgs = folder.getMessages();
int i;
int j;
MimeMultipart mmpart;
for (i=0; i<=(msgs.length-1);i++){
for(j=0;j<=(junktermsbody.length-1);j++){
if (msgs[i].isMimeType("text/plain")){
if(msgs[i].getContent().toString().contains(junktermsbody[j])){
System.out.println("Palin text: "+msgs[i].getSubject());
//move logik
}
}else if (msgs[i].isMimeType("multipart/*")){
mmpart =(MimeMultipart) msgs[i].getContent();
System.out.println("Trying:"+msgs[i].getSubject()+"...\n-------------------------------------------------------------------------------");
if (getTextFromMimeMultipart(mmpart).contains(junktermsbody[j])){
System.out.println("Time to move it!");
//move logik
}
}
}
}
folder.close(true);
}finally{
if (store != null) {store.close();}
//System.out.println("Junk on Body moved successfully");
System.out.println();
}
}
答案 0 :(得分:0)
当涉及普通/文本消息时,您需要首先打开java服务器邮件文件夹。 您需要在阅读内容之前打开该文件夹。
Folder folder = message.getFolder();
// Open folder in read-only mode
if (folder.isOpen()) {
if ((folder.getMode() & Folder.READ_WRITE) != 0) {
folder.close(false);
folder.open(Folder.READ_ONLY);
}
} else {
folder.open(Folder.READ_ONLY);
}
现在您可以阅读邮件正文内容:
Object content = message.getContent();