Javamail 未连接
Javamail not connecting
第一次尝试使用 JavaMail。输入详细信息后,它会打印 true(就好像它已连接),但是当尝试查找文件夹时,它不起作用。
private static void login() throws MessagingException {
String host = "imap.gmail.com";
String username = "user@gmail.com";
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 分钟调用一次。
如您所说,check()
方法每两分钟调用一次。当您关闭其中的商店对象时,它会产生问题 store.close()
。所以它只会第一次工作,然后就不会了。
您也可以在 check()
之前每两分钟给 login()
打电话,或者不要关闭检查中的商店并将其保留 open/connected。
希望对您有所帮助。
第一次尝试使用 JavaMail。输入详细信息后,它会打印 true(就好像它已连接),但是当尝试查找文件夹时,它不起作用。
private static void login() throws MessagingException {
String host = "imap.gmail.com";
String username = "user@gmail.com";
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 分钟调用一次。
如您所说,check()
方法每两分钟调用一次。当您关闭其中的商店对象时,它会产生问题 store.close()
。所以它只会第一次工作,然后就不会了。
您也可以在 check()
之前每两分钟给 login()
打电话,或者不要关闭检查中的商店并将其保留 open/connected。
希望对您有所帮助。