JavaMail: Store.Connect Error
907023Dec 22 2011 — edited Dec 26 2011Hi,
I am new to Javamail and I want to use Javamail to develop some sample email notification application.
Looking forward for some Input.. Thanks
Configuration and Setup:
===============
1. I am running this on Fedora 14.
2. Java1.6
3. Using the monitor.java application to connect using IMAP to the store ie Root folder.
4. Then monitors this folder for any new messages.
5. I have provided the parameters in the run configuration.
6. I am running the linux default sendmail server as MTA and Dovecot to support IMAP. All the applications MTA, Dovecot and JavaMail Email notification program are running in the same machine. There is no network involved here.
I am able to get the Store for IMAP. However, while connecting to store I am getting the below error.
Error:
====
Session Created
Store Accessed...
javax.mail.MessagingException: * BYE Internal error occurred. Refer to server log for more information.;
nested exception is:
com.sun.mail.iap.ConnectionException: * BYE Internal error occurred. Refer to server log for more information.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:663)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at Monitor.main(Monitor.java:40)
Caused by: com.sun.mail.iap.ConnectionException: * BYE Internal error occurred. Refer to server log for more information.
at com.sun.mail.iap.Protocol.handleResult(Protocol.java:349)
at com.sun.mail.imap.protocol.IMAPProtocol.authplain(IMAPProtocol.java:545)
at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:716)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:643)
... 3 more
Source Code:
=========
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.activation.*;
import com.sun.mail.imap.*;
/* Monitors given mailbox for new mail */
public class monitor {
public static void main(String argv[]) {
if (argv.length != 5) {
System.out.println(
"Usage: monitor <host> <user> <password> <mbox> <freq>");
System.exit(1);
}
System.out.println("\nTesting monitor\n");
try {
Properties props = System.getProperties();
// Get a Session object
Session session = Session.getInstance(props, null);
// session.setDebug(true);
// Get a Store object
Store store = session.getStore("imap");
// Connect
store.connect(argv[0], argv[1], argv[2]);
// Open a Folder
Folder folder = store.getFolder(argv[3]);
if (folder == null || !folder.exists()) {
System.out.println("Invalid folder");
System.exit(1);
}
folder.open(Folder.READ_WRITE);
// Add messageCountListener to listen for new messages
folder.addMessageCountListener(new MessageCountAdapter() {
public void messagesAdded(MessageCountEvent ev) {
Message[] msgs = ev.getMessages();
System.out.println("Got " + msgs.length + " new messages");
// Just dump out the new messages
for (int i = 0; i < msgs.length; i++) {
try {
System.out.println("-----");
System.out.println("Message " +
msgs.getMessageNumber() + ":");
msgs[i].writeTo(System.out);
} catch (IOException ioex) {
ioex.printStackTrace();
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
});
// Check mail once in "freq" MILLIseconds
int freq = Integer.parseInt(argv[4]);
boolean supportsIdle = false;
try {
if (folder instanceof IMAPFolder) {
IMAPFolder f = (IMAPFolder)folder;
f.idle();
supportsIdle = true;
}
} catch (FolderClosedException fex) {
throw fex;
} catch (MessagingException mex) {
supportsIdle = false;
}
for (;;) {
if (supportsIdle && folder instanceof IMAPFolder) {
IMAPFolder f = (IMAPFolder)folder;
f.idle();
System.out.println("IDLE done");
} else {
Thread.sleep(freq); // sleep for freq milliseconds
// This is to force the IMAP server to send us
// EXISTS notifications.
folder.getMessageCount();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}