EzDevInfo.com

imap interview questions

Top imap frequently asked interview questions

How to get the list of available folders in a mail account using JavaMail

I am using JavaMail API to connect to my personal account. I have list of folders (labels) in my Gmail account which I created + the default folders like Inbox, Drafts etc. How can I list all the available folders (the default and the user created)?

I can access the particular folder using this API: Folder inbox = store.getFolder("Inbox");. Is there any other API to get the list of folders available in a mail account?


Source: (StackOverflow)

How can you send mail using IMAP?

I'm developing a lightweight Gmail client for mobile phones. It access Gmail by IMAP. Then I want to send a draft from the Drafts folder, but it has some attachments and I can't download all of them to send it by SMTP.

Moving/copying it to "Sent Mail" doesn't send it, just moves it to that folder.

How can I send a Draft directly without fetch all the content and attachments from the client? Is there any IMAP command to do it?


Source: (StackOverflow)

Advertisements

Delete Email on Server using javax.mail

I am receiving emails from the server using the IMAP protocol like it is described here. This is working very fine and I can store the emails and attachments on the disk.

Question: Do I have the possibility to delete files from the Server, so that they are no longer available, when a client tries to receive all emails? If so, please tell me how.


Source: (StackOverflow)

Javascript IMAP and SMTP client? [closed]

Is it possible to build a SMTP/IMAP client that can run in the browser that uses only Javascript?


Source: (StackOverflow)

Java Mail to Exchange Server "no login methods supported"

I am trying to implement a Java Mail servlet, first step is connecting to the IMAP server.

I am able to telnet to the server on port 143 (default IMAP port), telnet says: OK The Microsoft Exchange IMAP4 service is ready.

Now I am trying to connect to the server using the Java Mail API like this:

Properties props = new Properties();
session = Session.getDefaultInstance(props, null);
store = session.getStore("imap");
store.connect("host","user","password");

And I am able to connect to this same server using an existing Outlook Webapp with the same credentials I am trying to pass to it in java.

But running this with session.setDebug(true) produces the following output:

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "myHost", port 143, isSSL false
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 LOGINDISABLED STARTTLS UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG: protocolConnect login, host=myHost, user=myUser, password=<non-null>
javax.mail.MessagingException: No login methods supported!;

EDIT:

I added prop.setProperty("mail.imap.starttls.enable", "true") as suggested.

However, I started getting this debug output:

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: enable STARTTLS
DEBUG: trying to connect to host "myHost", port 143, isSSL false
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 LOGINDISABLED STARTTLS UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG: protocolConnect login, host=myHost, user=myUser, password=<non-null>
A1 STARTTLS
A1 OK Begin TLS negotiation now.
DEBUG IMAP: STARTTLS Exception: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Assuming that this was a certificate problem I followed these instructions and added mail server to my cacerts file. The test program included there ran totally fine and I could connect using an SSL url but I still got the same exception when running the java mail class.

I also tried changing to "imaps" with: session.getStore("imaps") (instead of "imap") and wasn't even able to get the "Microsoft Exchange Server is now ready" message. I think because it is trying to connect on port 993 whenever "imaps" was specified. But using telnet to port 993 does not show any connection to the email server.

So next I tried to force the program to use SSL on port 143 like this:

// Use SSL
prop.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.setProperty("mail.imap.socketFactory.fallback", "false");

// Use port 143
prop.setProperty("mail.imap.port", "143");
prop.setProperty("mail.imap.socketFactory.port", "143");

Only to receive this exception, so I don't think it wants anything to do with SSL:

DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "myHost", port 143, isSSL false
Not able to process the mail reading.
javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?;

Could that TLS exception above (DEBUG IMAP: STARTTLS Exception: javax.net.ssl.SSLHandshakeException:) come from TLS not being enabled on the Exchange Server?

I don't have ready access to the e-mail server but I could probably get someone to let me in.

SOLUTION:

HulkingUnicorn's comment below his answer pointed out this answer which was the exact handling needed. Apparently MS Exchange Server has this problem. Along with adding the class listed in that answer to my package, I simply implemented my mail connections like this and all was well:

Properties prop = new Properties();
prop.setProperty("mail.imap.starttls.enable", "true");
prop.setProperty("ssl.SocketFactory.provider", "my.package.name.ExchangeSSLSocketFactory");
prop.setProperty("mail.imap.socketFactory.class", "my.package.name.ExchangeSSLSocketFactory");
session = Session.getDefaultInstance(prop, null);
session.setDebug(true);
store = session.getStore("imap");
store.connect("myHost","myUser","myPassword");

Source: (StackOverflow)

Getting mail from GMail into Java application using IMAP

I want to access messages in GMail from a Java application using JavaMail and IMAP. Why am I getting a SocketTimeoutException?

Here is my code:

Properties props = System.getProperties();
props.setProperty("mail.imap.host", "imap.gmail.com");
props.setProperty("mail.imap.port", "993");
props.setProperty("mail.imap.connectiontimeout", "5000");
props.setProperty("mail.imap.timeout", "5000");

try {
  Session session = Session.getDefaultInstance(props, new MyAuthenticator());
  URLName urlName = new URLName("imap://MYUSERNAME@gmail.com:MYPASSWORD@imap.gmail.com");
  Store store = session.getStore(urlName);
  if (!store.isConnected()) {
    store.connect();
  }
} catch (NoSuchProviderException e) {
  e.printStackTrace();
  System.exit(1);
} catch (MessagingException e) {
  e.printStackTrace();
  System.exit(2);
}

I set the timeout values so that it wouldn't take "forever" to timeout. Also, MyAuthenticator also has the username and password, which seems redundant with the URL. Is there another way to specify the protocol? (I didn't see it in the JavaDoc for IMAP.)


Source: (StackOverflow)

IMAP: how to move a message from one folder to another

(using the IMAP commands, not with the assistance of any other mail package)


Source: (StackOverflow)

Accessing Imap in C# [closed]

Is there a built-in method to access an Imap server (with SSL) in C# or is there a good free library?


Source: (StackOverflow)

Fatal error: Call to undefined function imap_open() in PHP

I am trying to access my gmail account through my localhost. However, I am getting the response:

Fatal error: Call to undefined function imap_open()

Can someone point out what should I do to resolve the issue?

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'myid@gmail.com';
$password = 'mypassword';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' .imap_last_error());

Source: (StackOverflow)

How do I enable push-notification for IMAP (Gmail) using Python imaplib?

Is there a way to monitor a gmail account using imaplib without polling gmail each time I want to see if there is new mail. Or in other words, I just want the script to be notified of a new message so I can process it right away instead of any lag time between polls.

I see that the IMAP protocol supports this with the IDLE command, but I can't see anything documented with it in the imaplib docs, so any help with this would be great!


Source: (StackOverflow)

IMAP in Emacs Rmail?

Is there a way to enable Rmail to read mail from an IMAP server?


Source: (StackOverflow)

JavaMail: Keeping IMAPFolder.idle() alive

I am making a program that needs to monitor a Gmail account for new messages, and in order to get them ASAP I am using JavaMail's idle feature. Here is a code snippet from the thread I am using to call folder.idle():

//Run method that waits for idle input. If an exception occurs, end the thread's life.
public void run() {

    IMAPFolder folder = null;

            try {
                folder = getFolder();
                while(true)
                {
                  //If connection has been lost, attempt to restore it
                  if (!folder.isOpen())
                      folder = getFolder();
                  //Wait until something happens in inbox
                  folder.idle(true);
                  //Notify controller of event
                  cont.inboxEventOccured();
                }
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
             System.out.println("MailIdleWaiter thread ending.");
}

The getFolder() method basically opens the connection to the IMAP server and opens the inbox.

This works for a while, but after 10 minutes or so it stops getting updates (no exception is thrown).

I am looking for suggestions to keep the connection alive. Do I need a second thread whose only role is to sleep and renew the idle() thread every 10 minutes or is there an easier/better way?

Thanks in advance.


Source: (StackOverflow)

How to understand the equal sign '=' symbol in IMAP email text?

I am currently using Python imaplib to process email text.

I use fetch command to fetch the raw data email from GMail server. However, I found one thing really tricky - the equal sign '='. It is not a normal equal sign but a special symbol.

For example:

  1. '=' sometimes acts as the hyphenation mark at the end of text line:

    Depending upon your module selections, course lecturers may also contact yo=
    u with preparatory work over the next few weeks. It would be wise to start =
    reviewing the preparatory reading lists provided on the module syllabi now =
    
  2. Sometimes, it acts as a escape mark similar to '%', for example:

    a=20b is actually a<SPACE>b
    =46rom here is actually From here

I am totally confused about such weird notation. I think there must be a guidance to handle this because GMail can handle such thing correctly in their apps.

I see that this is related to HTML encoding, just like '%' will be encoded. But the problem is, all I get from the IMAP response is a string that contain this '=' symbol. How should I handle this? Using regular expression?


Source: (StackOverflow)

JavaMail IMAP over SSL quite slow - Bulk fetching multiple messages

I am currently trying to use JavaMail to get emails from IMAP servers (Gmail and others). Basically, my code works: I indeed can get the headers, body contents and so on. My problem is the following: when working on an IMAP server (no SSL), it basically takes 1-2ms to process a message. When I go on an IMAPS server (hence with SSL, such as Gmail) I reach around 250m/message. I ONLY measure the time when processing the messages (the connection, handshake and such are NOT taken into account).

I know that since this is SSL, the data is encrypted. However, the time for decryption should not be that important, should it?

I have tried setting a higher ServerCacheSize value, a higher connectionpoolsize, but am seriously running out of ideas. Anyone confronted with this problem? Solved it one might hope?

My fear is that the JavaMail API uses a different connection each time it fetches a mail from the IMAPS server (involving the overhead for handshake...). If so, is there a way to override this behavior?

Here is my code (although quite standard) called from the Main() class:

 public static int connectTest(String SSL, String user, String pwd, String host) throws IOException,
                                                                               ProtocolException,
                                                                               GeneralSecurityException {

    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", SSL);
    props.setProperty("mail.imaps.ssl.trust", host);
    props.setProperty("mail.imaps.connectionpoolsize", "10");

    try {


        Session session = Session.getDefaultInstance(props, null);

        // session.setDebug(true);

        Store store = session.getStore(SSL);
        store.connect(host, user, pwd);      
        Folder inbox = store.getFolder("INBOX");

        inbox.open(Folder.READ_ONLY);                
        int numMess = inbox.getMessageCount();
        Message[] messages = inbox.getMessages();

        for (Message m : messages) {

            m.getAllHeaders();
            m.getContent();
        }

        inbox.close(false);
        store.close();
        return numMess;
    } catch (MessagingException e) {
        e.printStackTrace();
        System.exit(2);
    }
    return 0;
}

Thanks in advance.


Source: (StackOverflow)