How to fetch mails from IBM Domino Lotus using External java code (IMAP)?

63 Views Asked by At

I want to fetch mails from my inbox using external java code and read the message. I am able to connect to Gmail and fetch and read the mails using external java code with the help of javamail api. Now I am trying to do the same with IBM Domino lotus as well.

I am using the following code...

String host = "<Host name>";
String username = "<User Name>";
String password = "<password>";

Properties properties = new Properties();
properties.setProperty("mail.store.protocol", "imap");
properties.setProperty("mail.imap.host", host);
properties.setProperty("mail.imap.port", "143");

try {
    Session emailSession = Session.getInstance(properties);
    Store emailStore = emailSession.getStore("imap");
    emailStore.connect(host, username, password);
            
    //Getting Inbox folder
    Folder emailFolder = emailStore.getFolder("INBOX");
    emailFolder.open(Folder.READ_ONLY);
            
        Message[] messages =emailFolder.getMessages(); 
    System.out.println("No Of Mails : "+messages.length);
            
    //closing
    emailFolder.close(false);
    emailStore.close();
}
catch(NoSuchProviderException nspe) {
    System.out.println(nspe.getMessage());
        System.out.println(me.toString());
    nspe.printStackTrace();
}
catch(MessagingException me){
        System.out.println(me.getMessage());
        System.out.println(me.toString());
        me.printStackTrace();
}
catch(Exception e) {
    System.out.println(e.getMessage());
        System.out.println(me.toString());
    e.printStackTrace();
}
    

The line emailStore.connect(host, username, password); is throwing the following exception :

javax.mail.MessagingException: <Host name>;
  nested exception is:
    java.net.UnknownHostException:  <Host name>
javax.mail.MessagingException:  <Host name>;
  nested exception is:
    java.net.UnknownHostException: <Host name>

    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:611)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at package1.MyClass.main(MyClass.java:40)
Caused by: java.net.UnknownHostException:  <Host name>
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:267)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:107)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:103)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:578)
    ... 3 more

I have tried to change the port number to 993 and have also tried to set ssl.enable property to true. but I get the same exception again and again.

I have also checked the server settings. IMAP is enabled there.

This code is similar to the code which I have used to successfully connect to Gmail and fetch mails. that is why I am not sure if this is the correct way to connect to IBM Domino Lotus as well.

1

There are 1 best solutions below

0
Tode On

This will only be possible if your Domino administrator starts the "imap" task and enables your user to use IMAP (convert mail database to support imap).

It is NOT enough to enable the port in the server document to enable imap on a Domino server.

Without this setup on server side you need completely different classes and a completely different approach for your problem.