Overview
This article will demonstrate how to use the IMAP component in Secure iNet Factory to communicate with an IMAP server via SSH using Java.
Code Example
Download IMAPSSHEx.java
01 import com.jscape.inet.email.EmailMessage;
02 import com.jscape.inet.imapssh.ImapSsh;
03 import com.jscape.inet.ssh.util.SshParameters;
04
05
06 public class IMAPSSHEx
07 {
08 public static void main(String[] args)
09 {
10
11 // connection information for SSH server
12 String sshHostname = "imap4.myserver.com";
13 String sshUsername = "jsmith";
14 String sshPassword = "secret";
15
16 // create new SshParameters instance
17 SshParameters sshParams = new SshParameters(sshHostname,sshUsername,sshPassword);
18 // new instance with SSH parameters hostname, username and password
19 ImapSsh imap = new ImapSsh(sshParams,"imap4.myserver.com","jsmith","secret");
20
21 try {
22 // connect to IMAP4 server
23 imap.connect();
24 // get messages
25 int messageCount = imap.getMessageCount();
26 for(int i = 1; i <= messageCount; ++i)
27 {
28 EmailMessage em = imap.getMessage(i);
29 }
30 // disconnect
31 imap.disconnect();
32 }
33 catch(Exception e)
34 {
35 System.out.println(e);
36 }
37 }
38 }
Lines 1-3 : Necessary import statements. Lines 11-19 : Create an SshParameters instance and pass it to an ImapSsh instance. Lines 21-30 : Connect to the server and get messages from it. Line 31 : Disconnect from the server.
Replace hostname, username and password variable values with your credentials.
|
Comments