嘗試首次使用JavaMail。在詳細說明它打印真實(就像它被連接),但當試圖找到一個文件夾,它不起作用。Javamail未連接
private static void login() throws MessagingException {
String host = "imap.gmail.com";
String username = "[email protected]";
String password = "password";
Properties props = new Properties();
props.setProperty("mail.imap.ssl.enable", "true");
props.setProperty("mail.store.protocol", "imaps");
session = Session.getInstance(props);
store = session.getStore("imaps");
store.connect(host, 993, username, password);
System.out.println(store.isConnected()); //THIS HERE RETURNS TRUE
}
public static void check()
{
try {
//create the folder object and open it
Folder emailFolder = store.getFolder("INBOX"); //ENDS PROGRAM LOGGING, "Not Connected"
emailFolder.open(Folder.READ_ONLY);
javax.mail.Message[] messages = emailFolder.getMessages();
for (int i = 0, n = messages.length; i < n; i++) {
javax.mail.Message message = messages[i];
if(message.getSubject().contains("Optimism") && message.getSubject().contains("New reply to watched thread")) {
for(Guild g : jda.getGuilds()) {
if(g.getName().equalsIgnoreCase("Optimism"))
for (TextChannel c : g.getTextChannels())
if (c.getName() == "staff_chat") {
c.sendMessage("**New Thread Reply! - " + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()) + " EST.").queue();
}
}
}
}
//close the store and folder objects
emailFolder.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
寄託都在從第一循環的if語句的所有作品
注:這是在兩種方法作爲檢查方法被調用,每2分鐘。
'jda'代表什麼?這可能會帶來一些問題 – Sanjeev
如果在'check()'方法中執行'println(store.isConnected())',會發生什麼? – Andreas
@Sanjeev jda是我用於不和諧的另一個API的一部分(您可能聽說過它)。 – JackChap77