我試圖使用基於imap協議的java郵件API來連接到我的郵箱。我檢查並確定我插入了正確的參數。這是下面的異常,我得到:請通過您的網絡瀏覽器登錄:https://support.google.com/mail/accounts/answer/78754(失敗)
[警示]請通過網絡瀏覽器登錄:https://support.google.com/mail/accounts/answer/78754(失敗)
我不知道爲什麼它是發生在另外我啓用IMAP選項在我的Gmail帳戶設置。
Ihis是我的代碼:
Properties protocol = new Properties();
protocol.setProperty("mail.store.protocol", "imaps");
try{
Session session = Session.getInstance(protocol, null);
Store store = session.getStore();
String host = prop.getProperty("host");
String email = prop.getProperty("username");
String password = prop.getProperty("password");
store.connect(host, email, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
model.addAttribute("msg","number of mails"+" "+messageCount);
Message[] messages = inbox.getMessages();
PrintWriter writer = new PrintWriter(username+".txt", "UTF-8");
for(int i=0;i<messageCount || prop.getProperty("status").equals(status.RUNNING.toString()) ;i++){
model.addAttribute("msg","Reading Mails");
Multipart mp = (Multipart) messages[i].getContent();
BodyPart bp = mp.getBodyPart(0);
writer.println("From:"+ messages[i].getFrom()+" "+"Subject:"+messages[i].getSubject()+" "+"Message:"+bp.getContent());
prop = loadProperties(username+".properties");
}
writer.close();
inbox.close(true);
store.close();
}catch (Exception e){
model.addAttribute("msg","Exception:"+e.getMessage());
}
您是否在您的Gmail帳戶中激活了「IMAP」協議?它不是默認打開的,您需要深入瞭解設置並啓用它https://support.google.com/mail/troubleshooter/1668960?hl=zh-CN –
yes是,imap已啓用,如上所述。 –
gmail的主機應該是imap.gmail.com嗎? –