| 1 |
/* |
| 2 |
* Copyright (c) 1996, 2020 Oracle and/or its affiliates. All rights reserved. |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Distribution License v. 1.0, which is available at |
| 6 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: BSD-3-Clause |
| 9 |
*/ |
| 10 |
|
| 11 |
import java.util.*; |
| 12 |
import java.io.*; |
| 13 |
import jakarta.mail.*; |
| 14 |
import jakarta.mail.event.*; |
| 15 |
import jakarta.activation.*; |
| 16 |
|
| 17 |
import com.sun.mail.imap.*; |
| 18 |
|
| 19 |
/* Monitors given mailbox for new mail */ |
| 20 |
|
| 21 |
public class monitor { |
| 22 |
|
| 23 |
public static void main(String argv[]) { |
| 24 |
if (argv.length != 5) { |
| 25 |
System.out.println( |
| 26 |
"Usage: monitor <host> <user> <password> <mbox> <freq>"); |
| 27 |
System.exit(1); |
| 28 |
} |
| 29 |
System.out.println("\nTesting monitor\n"); |
| 30 |
|
| 31 |
try { |
| 32 |
Properties props = System.getProperties(); |
| 33 |
|
| 34 |
// Get a Session object |
| 35 |
Session session = Session.getInstance(props, null); |
| 36 |
// session.setDebug(true); |
| 37 |
|
| 38 |
// Get a Store object |
| 39 |
Store store = session.getStore("imap"); |
| 40 |
|
| 41 |
// Connect |
| 42 |
store.connect(argv[0], argv[1], argv[2]); |
| 43 |
|
| 44 |
// Open a Folder |
| 45 |
Folder folder = store.getFolder(argv[3]); |
| 46 |
if (folder == null || !folder.exists()) { |
| 47 |
System.out.println("Invalid folder"); |
| 48 |
System.exit(1); |
| 49 |
} |
| 50 |
|
| 51 |
folder.open(Folder.READ_WRITE); |
| 52 |
|
| 53 |
// Add messageCountListener to listen for new messages |
| 54 |
folder.addMessageCountListener(new MessageCountAdapter() { |
| 55 |
public void messagesAdded(MessageCountEvent ev) { |
| 56 |
Message[] msgs = ev.getMessages(); |
| 57 |
System.out.println("Got " + msgs.length + " new messages"); |
| 58 |
|
| 59 |
// Just dump out the new messages |
| 60 |
for (int i = 0; i < msgs.length; i++) { |
| 61 |
try { |
| 62 |
System.out.println("-----"); |
| 63 |
System.out.println("Message " + |
| 64 |
msgs[i].getMessageNumber() + ":"); |
| 65 |
msgs[i].writeTo(System.out); |
| 66 |
} catch (IOException ioex) { |
| 67 |
ioex.printStackTrace(); |
| 68 |
} catch (MessagingException mex) { |
| 69 |
mex.printStackTrace(); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
}); |
| 74 |
|
| 75 |
// Check mail once in "freq" MILLIseconds |
| 76 |
int freq = Integer.parseInt(argv[4]); |
| 77 |
boolean supportsIdle = false; |
| 78 |
try { |
| 79 |
if (folder instanceof IMAPFolder) { |
| 80 |
IMAPFolder f = (IMAPFolder)folder; |
| 81 |
f.idle(); |
| 82 |
supportsIdle = true; |
| 83 |
} |
| 84 |
} catch (FolderClosedException fex) { |
| 85 |
throw fex; |
| 86 |
} catch (MessagingException mex) { |
| 87 |
supportsIdle = false; |
| 88 |
} |
| 89 |
for (;;) { |
| 90 |
if (supportsIdle && folder instanceof IMAPFolder) { |
| 91 |
IMAPFolder f = (IMAPFolder)folder; |
| 92 |
f.idle(); |
| 93 |
System.out.println("IDLE done"); |
| 94 |
} else { |
| 95 |
Thread.sleep(freq); // sleep for freq milliseconds |
| 96 |
|
| 97 |
// This is to force the IMAP server to send us |
| 98 |
// EXISTS notifications. |
| 99 |
folder.getMessageCount(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
} catch (Exception ex) { |
| 104 |
ex.printStackTrace(); |
| 105 |
} |
| 106 |
} |
| 107 |
} |