我已经编写了用于获取多部分电子邮件内容的代码...我收到此错误...我知道这个错误是由于某些非常愚蠢但我无法在经过数小时的调试后得到它...可能是一些愚蠢的错误,如;,}等 代码:
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.Flags.Flag;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import com.sun.mail.imap.IMAPNestedMessage;
private String getText(Part p) throws MessagingException, IOException {
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
Multipart mp = (Multipart)p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getText(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return s;
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart)p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
if (s != null)
return s;
}
}
else
{ return p.getContent().toString();}
return null;
}
public class FolderFetch3 {
public static void main(String[] args) throws MessagingException, IOException {
IMAPFolder folder = null;
Store store = null;
String subject = null;
Flag flag = null;
try
{
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
store.connect("imap.gmail.com","hjcooljohny75@gmail.com", "ferry@786");
//folder = (IMAPFolder) store.getFolder("[Gmail]/Spam"); // This doesn't work for other email account
folder = (IMAPFolder) store.getFolder("inbox"); //This works for both email account
if(!folder.isOpen())
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
System.out.println("No of Messages : " + folder.getMessageCount());
System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
System.out.println(messages.length);
for (int i=messages.length-1; i > messages.length-10;i--)
{
System.out.println("*****************************************************************************");
System.out.println("MESSAGE " + (i + 1) + ":");
MimeMessage msg = (MimeMessage)messages[i];
//System.out.println(msg.getMessageNumber());
//Object String;
//System.out.println(folder.getUID(msg)
//String n =messages[i];
subject = msg.getSubject();
System.out.println("Subject: " + subject);
System.out.println("From: " + msg.getFrom()[0]);
System.out.println("To: "+msg.getAllRecipients()[0]);
System.out.println("Date: "+msg.getReceivedDate());
System.out.println("Size: "+msg.getSize());
System.out.println(msg.getMessageID());
System.out.println(msg.getFlags());
String tex=getText(msg);
System.out.println("Body: \n"+ tex);
System.out.println(msg.getContentType());
}
}
finally
{
if (folder != null && folder.isOpen()) { folder.close(true); }
if (store != null) { store.close(); }
}
}
}
我收到此错误:
FolderFetch3.java:17: error: class, interface, or enum expected
private String getText(Part p) throws MessagingException, IOException {
^
FolderFetch3.java:23: error: class, interface, or enum expected
String text = null;
^
FolderFetch3.java:24: error: class, interface, or enum expected
for (int i = 0; i < mp.getCount(); i++) {
^
FolderFetch3.java:24: error: class, interface, or enum expected
for (int i = 0; i < mp.getCount(); i++) {
^
FolderFetch3.java:24: error: class, interface, or enum expected
for (int i = 0; i < mp.getCount(); i++) {
^
FolderFetch3.java:26: error: class, interface, or enum expected
if (bp.isMimeType("text/plain")) {
^
FolderFetch3.java:29: error: class, interface, or enum expected
continue;
^
FolderFetch3.java:30: error: class, interface, or enum expected
} else if (bp.isMimeType("text/html")) {
^
FolderFetch3.java:32: error: class, interface, or enum expected
if (s != null)
^
FolderFetch3.java:34: error: class, interface, or enum expected
} else {
^
FolderFetch3.java:36: error: class, interface, or enum expected
}
^
FolderFetch3.java:39: error: class, interface, or enum expected
} else if (p.isMimeType("multipart/*")) {
^
FolderFetch3.java:41: error: class, interface, or enum expected
for (int i = 0; i < mp.getCount(); i++) {
^
FolderFetch3.java:41: error: class, interface, or enum expected
for (int i = 0; i < mp.getCount(); i++) {
^
FolderFetch3.java:41: error: class, interface, or enum expected
for (int i = 0; i < mp.getCount(); i++) {
^
FolderFetch3.java:43: error: class, interface, or enum expected
if (s != null)
^
FolderFetch3.java:45: error: class, interface, or enum expected
}
^
FolderFetch3.java:48: error: class, interface, or enum expected
{ return p.getContent().toString();}
^
FolderFetch3.java:50: error: class, interface, or enum expected
}
^
答案 0 :(得分:0)
将getText(Part p)
方法放在FolderFetch3
类中。