Index: kioslave/pop3/pop3.cpp
===================================================================
--- kioslave/pop3/pop3.cpp	(revision 768592)
+++ kioslave/pop3/pop3.cpp	(working copy)
@@ -26,6 +26,7 @@
  *
  */
 #include "pop3.h"
+#include "common.h"
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -88,18 +89,16 @@
     return -1;
   }
 
+  QCoreApplication app( argc, argv ); // needed for QSocketNotifier
+  KComponentData componentData("kio_pop3");
+
 #ifdef HAVE_LIBSASL2
-  if ( sasl_client_init( NULL ) != SASL_OK ) {
-    fprintf(stderr, "SASL library initialization failed!\n");
+  if (!initSASL())
     return -1;
-  }
 #endif
 
-  QCoreApplication app( argc, argv ); // needed for QSocketNotifier
-  KComponentData componentData("kio_pop3");
-  POP3Protocol *slave;
-
   // Are we looking to use SSL?
+  POP3Protocol *slave;
   if (strcasecmp(argv[1], "pop3s") == 0) {
     slave = new POP3Protocol(argv[2], argv[3], true);
   } else {
Index: kioslave/smtp/smtp.cpp
===================================================================
--- kioslave/smtp/smtp.cpp	(revision 768592)
+++ kioslave/smtp/smtp.cpp	(working copy)
@@ -36,6 +36,7 @@
 }
 #endif
 
+#include "common.h"
 #include "smtp.h"
 #include "request.h"
 #include "response.h"
@@ -87,10 +88,8 @@
   }
 
 #ifdef HAVE_LIBSASL2
-  if ( sasl_client_init( NULL ) != SASL_OK ) {
-    fprintf(stderr, "SASL library initialization failed!\n");
+  if (!initSASL())
     exit(-1);
-  }
 #endif
   SMTPProtocol slave( argv[2], argv[3], qstricmp( argv[1], "smtps" ) == 0 );
   slave.dispatchLoop();
Index: kioslave/imap4/imapparser.h
===================================================================
--- kioslave/imap4/imapparser.h	(revision 768592)
+++ kioslave/imap4/imapparser.h	(working copy)
@@ -51,8 +51,8 @@
 class parseString
 {
 public:
-  parseString() { pos = 0; }
-  char operator[](uint i) const { return data[i + pos]; }
+  parseString() : pos(0) {}
+  char operator[](uint i) const { return data.length() > (i + pos) ? data[i + pos] : '\0'; }
   bool isEmpty() const { return pos >= data.size(); }
   QByteArray cstr() const
   {
Index: kioslave/imap4/imap4.cpp
===================================================================
--- kioslave/imap4/imap4.cpp	(revision 768592)
+++ kioslave/imap4/imap4.cpp	(working copy)
@@ -88,6 +88,7 @@
 #include <kmimetype.h>
 #include <kcodecs.h>
 
+#include "common.h"
 #include "kdemacros.h"
 
 #define IMAP_PROTOCOL "imap"
@@ -114,10 +115,8 @@
   }
 
 #ifdef HAVE_LIBSASL2
-  if ( sasl_client_init( NULL ) != SASL_OK ) {
-    fprintf(stderr, "SASL library initialization failed!\n");
-    ::exit (-1);
-  }
+  if (!initSASL())
+    ::exit(-1);
 #endif
 
   //set debug handler
Index: kioslave/sieve/sieve-config.h.cmake
===================================================================
--- kioslave/sieve/sieve-config.h.cmake	(revision 768592)
+++ kioslave/sieve/sieve-config.h.cmake	(working copy)
@@ -1,3 +1,6 @@
 /* Define if the sieve KIO::Slave must be able to talk to timsieved <= 1.1.0
    */
 /* #undef HAVE_BROKEN_TIMSIEVED */
+
+/* Define if you have cyrus-sasl2 libraries */
+#cmakedefine HAVE_LIBSASL2 1
Index: kioslave/sieve/sieve.cpp
===================================================================
--- kioslave/sieve/sieve.cpp	(revision 768592)
+++ kioslave/sieve/sieve.cpp	(working copy)
@@ -25,6 +25,7 @@
  */
 
 #include "sieve.h"
+#include "common.h"
 
 extern "C" {
 #include <sasl/sasl.h>
@@ -72,10 +73,8 @@
 			return -1;
 		}
 
-		if ( sasl_client_init( NULL ) != SASL_OK ) {
-			fprintf(stderr, "SASL library initialization failed!\n");
-			::exit (-1);
-		}
+		if (!initSASL())
+			::exit(-1);
 
 		kio_sieveProtocol slave(argv[2], argv[3]);
 		slave.dispatchLoop();
Index: kioslave/CMakeLists.txt
===================================================================
--- kioslave/CMakeLists.txt	(revision 768592)
+++ kioslave/CMakeLists.txt	(working copy)
@@ -1,3 +1,5 @@
+include_directories( ${CMAKE_CURRENT_SOURCE_DIR} )
+
 if(SASL2_FOUND)
   add_subdirectory( sieve )
 endif(SASL2_FOUND)
Index: kioslave/common.h
===================================================================
--- kioslave/common.h	(revision 0)
+++ kioslave/common.h	(revision 0)
@@ -0,0 +1,31 @@
+
+#ifdef HAVE_LIBSASL2
+
+#include <stdio.h>
+#include <QFile>
+#include <KStandardDirs>
+
+extern "C" {
+#include <sasl/sasl.h>
+}
+
+inline bool initSASL()
+{
+#ifdef Q_OS_WIN32
+  QByteArray libInstallPath( QFile::encodeName(KGlobal::dirs()->installPath("lib")) );
+  if ( sasl_set_path(SASL_PATH_TYPE_PLUGIN, libInstallPath.data()) != SASL_OK
+    || sasl_set_path(SASL_PATH_TYPE_CONFIG, libInstallPath.data()) != SASL_OK )
+  {
+    fprintf(stderr, "SASL path initialization failed!\n");
+    return false;
+  }
+#endif
+
+  if ( sasl_client_init( NULL ) != SASL_OK ) {
+    fprintf(stderr, "SASL library initialization failed!\n");
+    return false;
+  }
+  return true;
+}
+
+#endif // HAVE_LIBSASL2
