我一直在尝试使用Android中的JavaMail API删除电子邮件。我已经能够在我的应用程序中获取和查看电子邮件。虽然无法删除任何电子邮件,但没有错误或任何可能导致解决方案的警告。这是我到目前为止所尝试的。非常感谢帮助。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import android.content.Intent;
import android.os.AsyncTask;
import com.emazdoor.encmail.customlistview.MainList;
import com.sun.mail.imap.IMAPFolder;
public class DeleteEmail {
static Message[] messages;
public static void delete(final int number)
{
new AsyncTask<Void, Void, Void>() {
@Override
public Void doInBackground(Void... voids) {
try {
int num = number;
Store store = null;
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
store.connect("imap.googlemail.com", dumber.username,
dumber.password);
// create the folder object and open it
IMAPFolder emailFolder = (IMAPFolder) store.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
// retrieve the messages from the folder in an array and
// print it
messages = emailFolder.getMessages();
System.out.println("messages.length---" + messages.length);
for (int i = 0; i < 5; i++) {
Message message = messages[i];
}
if (messages != null && messages.length > 0)
messages[817].setFlag(Flags.Flag.DELETED, true);
// expunges the folder to remove messages which are marked
// deleted
emailFolder.close(true);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
}
我做错了吗?
我会在messages[variable].setFlag(Flags.Flag.DELETED, true);
中使用变量而不是硬编码值。
更新
我设置setDebug(true)
这就是我得到的......似乎一切都好......
07-06 23:06:34.886: I/System.out(10749): DEBUG: setDebug: JavaMail version 1.4.1
07-06 23:06:34.886: I/System.out(10749): DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc.,1.4.1]
07-06 23:06:34.886: I/System.out(10749): DEBUG: mail.imap.fetchsize: 16384
07-06 23:06:36.896: I/System.out(10749): * OK Gimap ready for requests from 103.255.6.118 q11mb517503595ywb
07-06 23:06:36.896: I/System.out(10749): A0 CAPABILITY
07-06 23:06:37.251: I/System.out(10749): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=XOAUTH
07-06 23:06:37.251: I/System.out(10749): A0 OK Thats all she wrote! q11mb517503595ywb
07-06 23:06:37.256: I/System.out(10749): IMAP DEBUG: AUTH: XOAUTH2
07-06 23:06:37.256: I/System.out(10749): IMAP DEBUG: AUTH: PLAIN
07-06 23:06:37.256: I/System.out(10749): IMAP DEBUG: AUTH: PLAIN-CLIENTTOKEN
07-06 23:06:37.261: I/System.out(10749): IMAP DEBUG: AUTH: XOAUTH
07-06 23:06:37.261: I/System.out(10749): DEBUG: protocolConnect login, host=imap.googlemail.com, user=emazdoor@gmail.com , password=<non-null>
07-06 23:06:37.266: I/System.out(10749): A1 AUTHENTICATE PLAIN
07-06 23:06:37.571: I/System.out(10749): +
07-06 23:06:37.601: I/System.out(10749): ZW1hemRvb3JAZ21haWwuY29tIABlbWF6ZG9vckBnbWFpbC5jb20gAGltYXNkdXJyYW5p
07-06 23:06:38.236: I/System.out(10749): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT
07-06 23:06:38.256: I/System.out(10749): A1 OK emazdoor@gmail.com authenticated (Success)
07-06 23:06:38.281: I/System.out(10749): A2 CAPABILITY
07-06 23:06:38.696: I/System.out(10749): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT
07-06 23:06:38.701: I/System.out(10749): A2 OK Success
07-06 23:06:38.716: I/System.out(10749): DEBUG: connection available -- size: 1
07-06 23:06:38.721: I/System.out(10749): A3 SELECT INBOX
07-06 23:06:39.191: I/System.out(10749): * FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)
07-06 23:06:39.191: I/System.out(10749): * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing \*)] Flags permitted.
07-06 23:06:39.196: I/System.out(10749): * OK [UIDVALIDITY 651814458] UIDs valid.
07-06 23:06:39.196: I/System.out(10749): * 814 EXISTS
07-06 23:06:39.196: I/System.out(10749): * 0 RECENT
07-06 23:06:39.196: I/System.out(10749): * OK [UIDNEXT 1771] Predicted next UID.
07-06 23:06:39.201: I/System.out(10749): * OK [HIGHESTMODSEQ 101785]
07-06 23:06:39.201: I/System.out(10749): A3 OK [READ-WRITE] INBOX selected. (Success)
07-06 23:06:39.316: I/System.out(10749): messages.length---814
07-06 23:06:39.316: I/System.out(10749): A4 STORE 3 +FLAGS (\Deleted)
07-06 23:06:40.121: I/System.out(10749): * 3 FETCH (FLAGS (\Seen \Deleted))
07-06 23:06:40.126: I/System.out(10749): A4 OK Success
07-06 23:06:40.156: I/System.out(10749): A5 EXPUNGE
07-06 23:06:40.601: I/System.out(10749): * 3 EXPUNGE
07-06 23:06:40.636: I/System.out(10749): * 813 EXISTS
07-06 23:06:40.641: I/System.out(10749): A5 OK Success
07-06 23:06:40.671: I/System.out(10749): A6 CLOSE
07-06 23:06:41.081: I/System.out(10749): A6 OK Returned to authenticated state. (Success)
07-06 23:06:41.116: I/System.out(10749): DEBUG: added an Authenticated connection -- size: 1
07-06 23:06:41.121: I/System.out(10749): A7 LOGOUT
07-06 23:06:41.531: I/System.out(10749): * BYE LOGOUT Requested
07-06 23:06:41.551: I/System.out(10749): A7 OK 73 good day (Success)
07-06 23:06:41.561: I/System.out(10749): DEBUG: IMAPStore connection dead
07-06 23:06:41.561: I/System.out(10749): DEBUG: IMAPStore cleanup, force false
07-06 23:06:41.566: I/System.out(10749): DEBUG: IMAPStore cleanup done