2年前,我可以使用POP3,java程序中的SMTP在Gmail中提取从收件箱和垃圾箱中删除的电子邮件。这些电子邮件没有显示在我的收件箱或垃圾箱中,因为我已将其删除。但是仍然可以在控制台上获取它们。
这些电子邮件是否没有从Google服务器上永久删除? 他们有这样做的权利吗?
下面是代码:
import java.io.IOException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import com.sun.mail.pop3.POP3Store;
public class ReceiveSimpleEmail1
{
public static void main(String[] args) {
String host="pop.gmail.com";
int port=995;
String mailStorType="pop3";
String Username="abcd@gmail.com";
String Password="xxxxx";
receiveMail(host, port, mailStorType, Username, Password);
}
// method for Receive email.....!
public static void receiveMail(String pop3Host, int port, String sotreType,String user,String password){
/// 1) get session object
Properties props = new Properties();
props.put("mail.pop3.ssl.enable", "true"); // Use SSL
Session sessEmail = Session.getInstance(props);
// 2) create pop3 store object and connect with pop server
try {
POP3Store emailStore = (POP3Store) sessEmail.getStore(sotreType);
emailStore.connect(pop3Host, port, user, password);
// 3) create Folder object and open it
Folder emailFolder=emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
// 4) Retrieve the messages in the folder and display it
Message[] messages=emailFolder.getMessages();
for(Message m : messages){
System.out.println("-----------------------------------------");
System.out.println("Email Number : "+m.getMessageNumber());
System.out.println("Subject : "+m.getSubject());
System.out.println("From : "+m.getFrom());
try {
System.out.println("Subject : "+m.getContent().toString());
} catch (IOException e) {
System.out.println("No messages are available.............!");
e.printStackTrace();
}
} // end for loop
// 5) Close the Folder and email store
emailFolder.close(false);
emailStore.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}