yahoo / imapnio

Java imap nio client that is designed to scale well for thousands of connections per machine and reduce contention when using large number of threads and cpus.
57 stars 50 forks source link

Can you provide an example #117

Closed cxc222 closed 2 years ago

cxc222 commented 2 years ago

As the title

Can you provide a complete example

Thanks ~

nvsnvikram commented 2 years ago

The readme has an example. I didn't understand what you are looking for.

cxc222 commented 2 years ago

sorry,I didn't understand the readme.md. I have imap address and account password,how to get the content ?

cxc222 commented 2 years ago

how to execute the command ? Thanks ~

fansu commented 2 years ago

First, read this RFC3501 to know how each IMAP protocol works and how each command is used. https://datatracker.ietf.org/doc/html/rfc3501

Second, as explained in readme, following example command can be used to fetch CAPABILITY for example. For FETCH command the UidFetchCommand is one example to use.

final Future<ImapAsyncResponse> capaCmdFuture = session.execute(new CapaCommand());

cxc222 commented 2 years ago

First, read this RFC3501 to know how each IMAP protocol works and how each command is used. https://datatracker.ietf.org/doc/html/rfc3501

Second, as explained in readme, following example command can be used to fetch CAPABILITY for example. For FETCH command the UidFetchCommand is one example to use.

final Future<ImapAsyncResponse> capaCmdFuture = session.execute(new CapaCommand());

thanks, I see ~

cxc222 commented 2 years ago

after I execute fetchCommand getting the response, how to convert to MimeMessage or how to get the subject ?

Thanks~

fansu commented 2 years ago

Following is just an example, values need to be adjusted. Learn something about how com.sun.mail.imap.protocol.FetchResponse works, it will be helpful.

final Future<ImapAsyncResponse> cmdFuture = session.execute(new UidFetchCommand(....));
final ImapAsyncResponse resp = cmdFuture.get(30 * 1000, TimeUnit.MILLISECONDS);
final IMAPResponse[] imapResp = resp.getResponseLines().toArray(new IMAPResponse[0]);

final int len = imapResp.length;
for (int i = 0; i < len; i++) {
    IMAPResponse response = imapResp[i];
    if (response.keyEquals("FETCH")) {
        final FetchResponse r = new FetchResponse(response, new FetchItem[0]);
        // Now you get FetchResponse, and you can do operation with it, notes following import is needed
        //    com.sun.mail.imap.protocol.FetchResponse
        //   com.sun.mail.imap.protocol.IMAPResponse
        final BODY body = r.getItem(BODY.class);
        InternetHeaders headers = new InternetHeaders(body.getByteArrayInputStream());
        String subject = headers.getHeader("Subject")[0]; // now you are able to get subject
    }
}
cxc222 commented 2 years ago

Following is just an example, values need to be adjusted. Learn something about how com.sun.mail.imap.protocol.FetchResponse works, it will be helpful.

final Future<ImapAsyncResponse> cmdFuture = session.execute(new UidFetchCommand(....));
final ImapAsyncResponse resp = cmdFuture.get(30 * 1000, TimeUnit.MILLISECONDS);
final IMAPResponse[] imapResp = resp.getResponseLines().toArray(new IMAPResponse[0]);

final int len = imapResp.length;
for (int i = 0; i < len; i++) {
    IMAPResponse response = imapResp[i];
    if (response.keyEquals("FETCH")) {
        final FetchResponse r = new FetchResponse(response, new FetchItem[0]);
        // Now you get FetchResponse, and you can do operation with it, notes following import is needed
        //    com.sun.mail.imap.protocol.FetchResponse
        //   com.sun.mail.imap.protocol.IMAPResponse
        final BODY body = r.getItem(BODY.class);
        InternetHeaders headers = new InternetHeaders(body.getByteArrayInputStream());
        String subject = headers.getHeader("Subject")[0]; // now you are able to get subject
    }
}

Thanks, I see ~

nvsnvikram commented 2 years ago

This library is for accessing messages through IMAP server and client using mime libraries like apache or sun to read the message content. Reading mime message content is outside of this library.