Author: remi
Date: 2009-10-09 10:54:25 +0200 (Fri, 09 Oct 2009)
New Revision: 5614
Modified:
software_suite_v3/software/plugin/plugin-email/trunk/plugin-email/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java
Log:
* Added automatic detection of mail server protocol/port
Modified:
software_suite_v3/software/plugin/plugin-email/trunk/plugin-email/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java
===================================================================
---
software_suite_v3/software/plugin/plugin-email/trunk/plugin-email/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java
2009-10-09 07:10:26 UTC (rev 5613)
+++
software_suite_v3/software/plugin/plugin-email/trunk/plugin-email/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java
2009-10-09 08:54:25 UTC (rev 5614)
@@ -35,6 +35,10 @@
import javax.mail.Flags.Flag;
import javax.mail.internet.InternetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.net.InetSocketAddress;
+
import com.kysoh.tuxdroid.plugin.framework.plugin.SimplePlugin;
/**
@@ -304,6 +308,103 @@
writeState(stateRun, sessionId + "RUN");
}
+ /**
+ * Check if the mail server is responding to the x port.
+ * @param host Server host
+ * @param port Server port
+ * @return true or false
+ */
+ private boolean checkMailServer(String host, Integer port)
+ {
+ Socket socket = new Socket();
+ try
+ {
+ socket.connect(new InetSocketAddress(host, port), 1000);
+ }
+ catch (UnknownHostException e)
+ {
+ socket = null;
+ return false;
+ }
+ catch (IOException e)
+ {
+ socket = null;
+ return false;
+ }
+ socket = null;
+ return true;
+ }
+
+ /**
+ * Find the server mail port.
+ * @param host Server host
+ * @return Server port or -1.
+ */
+ private int findMailServerPort(String host)
+ {
+ int port1;
+ int port2;
+ int port3;
+ int port4;
+
+ if ((host.startsWith("pop")) || (host.startsWith("POP")))
+ {
+ port1 = 110;
+ port2 = 995;
+ port3 = 143;
+ port4 = 993;
+ }
+ else
+ {
+ port1 = 143;
+ port2 = 993;
+ port3 = 110;
+ port4 = 995;
+ }
+ if (checkMailServer(host, port1))
+ {
+ return port1;
+ }
+ else if (checkMailServer(host, port2))
+ {
+ return port2;
+ }
+ else if (checkMailServer(host, port3))
+ {
+ return port3;
+ }
+ else if (checkMailServer(host, port4))
+ {
+ return port4;
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
+ /**
+ * Get the mail server protocol from a port.
+ * @param port Mail server port.
+ * @return The mail server protocol.
+ */
+ private String portToProtocol(int port)
+ {
+ switch (port)
+ {
+ case 110:
+ return "pop3";
+ case 995:
+ return "pop3s";
+ case 143:
+ return "imap";
+ case 993:
+ return "imaps";
+ default:
+ return "pop3";
+ }
+ }
+
/**
*
* @return
@@ -313,37 +414,45 @@
*/
private Message[] connectToMailBox() throws MessagingException,
IOException, ClassNotFoundException
{
- this.configuration().setPort(-1);
+ String user;
+ String host;
+ String password;
+ Session session;
+ Properties props;
+ int port;
+ String protocol;
+ Store store;
+ Folder folder;
+ Message messages[];
+
+ /* Get user data */
+ user = configuration().getUser();
+ password = configuration().getPassword();
+ host = configuration().getHost();
+ /* Set ssl socket */
Security.setProperty("ssl.SocketFactory.provider",
"net.karmaLab.tuxDroid.plugins.DummySSLSocketFactory");
- /* Connect to the mail box */
+ /* Set text decoder */
System.setProperty("mail.mime.decodetext.strict", "false");
- Properties props = new Properties();
- Session session = Session.getDefaultInstance(props, null);
- Store store =
session.getStore(configuration().getProtocol().toString());
- int port = configuration().getPort();
- if (port == -1)
- {
- switch (configuration().getProtocol())
- {
- case imap:
- port = 143;
- break;
- case pop3:
- port = 110;
- break;
- case imaps:
- port = 993;
- break;
- case pop3s:
- port = 995;
- break;
- }
- }
+ /* Configure seesion */
+ props = new Properties();
+ session = Session.getDefaultInstance(props, null);
+ /* Find server port */
+ port = findMailServerPort(host);
+ if (port == -1)
+ {
+ throwMessage("Sorry, there was an error when connecting to the
mail server. Please check your email configuration.");
+ return null;
+ }
+ /* Get mail protocol */
+ protocol = portToProtocol(port);
+ /* Create mail store */
+ store = session.getStore(protocol);
+ /* Connect to the mailbox */
try
{
- store.connect(configuration().getHost(), port,
configuration().getUser(), configuration().getPassword());
+ store.connect(host, port, user, password);
}
- // Authentification exception (login / password / protocol /
etc)
+ /* Authentification exception (login / password / protocol /
etc) */
catch (javax.mail.AuthenticationFailedException e)
{
if (!getCommand().equals("check"))
@@ -352,7 +461,7 @@
}
return null;
}
- // Connection failed exception (timeout / network unreachable)
+ /* Connection failed exception (timeout / network unreachable)
*/
catch (javax.mail.MessagingException e)
{
if (!getCommand().equals("check"))
@@ -361,9 +470,11 @@
}
return null;
}
- Folder folder = store.getFolder(configuration().getFolder());
+ /* Connect and open inbox folder */
+ folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
- Message messages[] = folder.getMessages();
+ /* Get messages */
+ messages = folder.getMessages();
return messages;
}
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn