Overview
The article will demonstrate how to use the imap component in Secure iNet Factory
to communicate with imap servers in java using ssl/tls.
Code Example
Download IMAPSSLEx.java
01 import java.util.Enumeration;
02 import com.jscape.inet.email.EmailMessage;
03 import com.jscape.inet.imapssl.ImapSsl;
04
05 public class IMAPSSLEx
06 {
07 public static void main(String[] args)
08 {
09 try
10 {
11 // replace these values with your own credentials
12 String hostname = "hostname";
13 String username = "username";
14 String password = "password";
15 ImapSsl ssl = new ImapSsl(hostname,993,username,password);
16
17 // establish implicit SSL connection on port 993
18 ssl.connect();
19
20 // get messages and print out subject
21 Enumeration e = ssl.getMessages();
22 while(e.hasMoreElements())
23 {
24 EmailMessage msg = (EmailMessage)e.nextElement();
25 System.out.println("Subject: " + msg.getSubject());
26 }
27
28 // disconnect
29 ssl.disconnect();
30 }
31 catch (Exception e)
32 {
33 e.printStackTrace();
34 }
35 }
36 }
Lines 1-3 : Necessary import statements. Line 15 : Create new ImapSsl instance. Lines 21-26 : Get messages and print them to the console. Line 29 : Disconnect from the server.
The ImapSsl class provides both
implicit and explicit SSL/TLS connections. Implicit SSL/TLS connections are made by default using remote port 993. Explicit
SSL/TLS connections are made using an unencrypted connection to port
143 of an IMAP server and then switching to an encrypted mode using the
STARTTLS command prior to sending any additional data.
|
01 import java.util.Enumeration;
02 import com.jscape.inet.email.EmailMessage;
03 import com.jscape.inet.imapssl.ImapSsl;
04
05 public class IMAPSSLEx
06 {
07 public static void main(String[] args)
08 {
09 try
10 {
11 // replace these values with your own credentials
12 String hostname = "hostname";
13 String username = "username";
14 String password = "password";
15
16 // create new ImapSsl instance
17 ImapSsl ssl = new ImapSsl(hostname,143,username,password);
18 // set connection type
19 ssl.setConnectionType(ImapSsl.STARTTLS);
20 // establish explicit SSL connection on port 143 using STARTTLS command
21 ssl.connect();
22 // get messages and print out subject
23 Enumeration e = ssl.getMessages();
24 while(e.hasMoreElements())
25 {
26 EmailMessage msg = (EmailMessage)e.nextElement();
27 System.out.println("Subject: " + msg.getSubject());
28 }
29
30 // disconnect
31 ssl.disconnect();
32 }
33 catch (Exception e)
34 {
35 e.printStackTrace();
36 }
37 }
38 }
Lines 1-3 : Neccesary imports. Line 17 : Create ImapSsl instance. Line 19 : Set the connection type (use STARTTLS command). Lines 23-28 : Get messages and print them out to the console.
|
Comments