[balsa] Replace libesmtp in Balsa



commit ce950372dc3ddb3f33b0005740db69e2ef7b003c
Author: Albrecht Dreß <albrecht dress arcor de>
Date:   Tue Feb 7 22:24:23 2017 -0500

    Replace libesmtp in Balsa
    
    * configure.ac: remove libesmtp and sendmail checks, add gnutls and the libnetclient "specials"
    * Makefile.am: add libnetclient folder
    * libinit_balsa/Makefile.am, src/Makefile.am: include libnetclient
    * libbalsa/Makefile.am: include libnetclient, add new GMime filter
    * libbalsa/gmime-filter-header.[hc]: implement filter as to strip RFC 5322 message headers
    * libbalsa/identity.[hc], libbalsa/libbalsa.h, libbalsa/send.h, libinit_balsa/assistant_page_user.[hc], 
src/balsa-app.[hc], src/balsa-message.c, src/balsa-mime-widget-message.c, src/balsa-mime-widget-vcalendar.c, 
src/main-window.c, src/pref-manager.c, src/save-restore.c, src/sendmsg-window.c: strip libesmtp-related 
conditionals
    * libbalsa/send.c: replace all libesmtp related stuff by libnetclient, strip all sendmail-related 
implementations; *HUGE* change...
    * libbalsa/server.[hc]: add "Security" option (but keep ssl and tls)
    * libbalsa/smtp-server.[hc]: strip libesmtp-related conditionals, use Security option in GUI
    * po/POTFILES.in: add libnetclient files to translatables (*Note:* all the *.po files still must be 
updated!)

 Makefile.am                         |    2 +-
 configure.ac                        |  102 ++--
 libbalsa/Makefile.am                |    3 +
 libbalsa/identity.c                 |   41 +-
 libbalsa/identity.h                 |    8 +-
 libbalsa/libbalsa.h                 |    2 -
 libbalsa/send.c                     | 1487 ++++++++++-------------------------
 libbalsa/send.h                     |   21 -
 libbalsa/server.c                   |    9 +-
 libbalsa/server.h                   |    2 +
 libbalsa/smtp-server.c              |  131 +---
 libbalsa/smtp-server.h              |    5 -
 libinit_balsa/Makefile.am           |    1 +
 libinit_balsa/assistant_page_user.c |    8 -
 libinit_balsa/assistant_page_user.h |    2 -
 po/POTFILES.in                      |    2 +
 src/Makefile.am                     |    2 +
 src/balsa-app.c                     |   20 -
 src/balsa-app.h                     |    7 -
 src/balsa-message.c                 |   15 -
 src/balsa-mime-widget-message.c     |    6 -
 src/balsa-mime-widget-vcalendar.c   |    8 -
 src/main-window.c                   |    4 -
 src/pref-manager.c                  |   22 -
 src/save-restore.c                  |   22 +-
 src/sendmsg-window.c                |   11 -
 26 files changed, 511 insertions(+), 1432 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 51591d9..7a412a3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
 #intl dir needed for tarball --disable-nls build.
 DISTCHECK_CONFIGURE_FLAGS="--disable-extra-mimeicons"
-SUBDIRS = po sounds images doc libbalsa libinit_balsa src ui
+SUBDIRS = po sounds images doc libbalsa libnetclient libinit_balsa src ui
 # set tar in case it is not set by automake or make
 
 man_MANS=balsa.1
diff --git a/configure.ac b/configure.ac
index 0852eb2..af13355 100644
--- a/configure.ac
+++ b/configure.ac
@@ -98,11 +98,6 @@ AC_ARG_WITH(compface,
                   [Enable Compface (default=no)]),
                   [with_compface=$withval],[with_compface=no])
 
-AC_ARG_WITH(esmtp,
-   AC_HELP_STRING([--with-esmtp=DIR],
-                  [Enable libESMTP (default=yes)]),
-                  [ with_esmtp=$withval ],[ with_esmtp=yes ])
-
 AC_ARG_WITH(gss,
    AC_HELP_STRING([--with-gss],
                   [Enable GSS (default=no)]),
@@ -170,6 +165,16 @@ AC_ARG_ENABLE(more-warnings,
                   [Enable maximum compiler warnings (default=yes)]),
                   [set_more_warnings="$enableval"], [set_more_warnings=yes])
 
+dnl Documentation and Testing requirements for libnetclient
+AC_ARG_WITH(libnetclient-docs,
+   AC_HELP_STRING([--with-libnetclient-docs],
+                  [Check requirements for building the libnetclient API docs (see libnetclient/README; 
default=no)]),
+                  [with_libncdocs=$withval],[with_libncdocs=no])
+
+AC_ARG_WITH(libnetclient-test,
+   AC_HELP_STRING([--with-libnetclient-test],
+                  [Check requirements for running libnetclient tests (see libnetclient/README; default=no)]),
+                  [with_libncdtest=$withval],[with_libncdtest=no])
 
 
 dnl #####################################################################
@@ -231,14 +236,6 @@ if test "$have__nl_measurement_measurement" = "yes"; then
            [Define if _NL_MEASUREMENT_MEASUREMENT is available])
 fi
 
-# sendmail
-#
-if test x"$with_esmtp" = xno; then
-AC_PATH_PROG(SENDMAIL, sendmail, no, $PATH:/usr/sbin:/usr/lib)
-AC_DEFINE_UNQUOTED(SENDMAIL, "$ac_cv_path_SENDMAIL",
-                  [Path to sendmail program.])
-fi
-
 dnl #####################################################################
 dnl GNOME libs
 dnl #####################################################################
@@ -282,6 +279,7 @@ gtk+-3.0 >= 3.10.0
 gmime-2.6
 gio-2.0
 gthread-2.0
+gnutls
 $gnome_extras
 ])
 
@@ -292,6 +290,12 @@ PKG_CHECK_MODULES(BALSA_AB, [
    $gnome_extras
 ])
 
+PKG_CHECK_MODULES(LIBNETCLIENT, [
+       glib-2.0
+       gio-2.0
+       gnutls
+])     
+
 AC_MSG_CHECKING(whether res_init is available)
 AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <resolv.h>;]],
                                [[res_init();]])],
@@ -550,35 +554,6 @@ else
     AC_MSG_RESULT([no])
 fi
 
-# LibESMTP configuration.
-#
-AC_MSG_CHECKING([whether to build ESMTP support])
-if test "x$with_esmtp" != xno  ; then
-    AC_MSG_RESULT([yes])
-    test "$with_esmtp" != yes && LIBS="$LIBS -L$with_esmtp/lib"
-    AC_CHECK_LIB(esmtp, smtp_start_session,BALSA_LIBS="$BALSA_LIBS -lesmtp",
-       AC_MSG_ERROR([*** You enabled ESMTP but esmtp library is not found.]),
-                 -lpthread)
-    AC_CHECK_LIB(esmtp, smtp_option_require_all_recipients, 
-                 AC_DEFINE(ENABLE_ESMTP, 1, 
-                           [Defined when ESTMP support is to be enabled.]),
-                 AC_MSG_ERROR([*** You enabled ESMTP but esmtp library
-                               does not support 'require_all_recipients'.
-                               Please get a Balsa-aware libESMTP from
-                               http://pawsa.fedorapeople.org/balsa/download.html
-                               or compile libESMTP with
-                               './configure --enable-require-all-recipients'.]),
-                 -lpthread)
-    AC_CHECK_LIB(esmtp, smtp_starttls_set_password_cb,
-       AC_DEFINE(HAVE_SMTP_TLS_CLIENT_CERTIFICATE,1,
-                [Defined when libESMTP has TLS support.]),
-                 ,
-                -lpthread)
-    test "$with_esmtp" != yes && CPPFLAGS="$CPPFLAGS -I$with_esmtp/include"
-else
-    AC_MSG_RESULT([no])
-fi
-
 # OpenSSL configuration.
 #
 PKG_CHECK_MODULES(OPENSSL, [openssl],
@@ -865,11 +840,44 @@ dnl however, we do not mess with the icon caches - it messes up more
 dnl things that it fixes, for eg. user installs, package builds etc.
 dnl AC_PATH_PROG(GTK_UPDATE_ICON_CACHE, gtk-update-icon-cache)
 
+dnl Documentation and Testing requirements for libnetclient
+
+if test x$with_libncdocs != xno; then
+       AC_PATH_PROGS(DOXYGEN, doxygen, no)
+       if test "x$DOXYGEN" = "xno" ; then
+               AC_MSG_ERROR([*** Cannot build libnetclient API documentation, see libnetclient/README for 
fixing this issue ***])
+       fi
+fi
+
+if test x$with_libncdtest != xno; then
+       AC_CHECK_HEADER([sput.h],[have_sput_h=yes],[have_sput_h=no])
+       AC_PROG_SED
+       AC_PATH_PROGS(NCAT, ncat, no)
+       AC_PATH_PROGS(VALGRIND, valgrind, no)
+       AC_PATH_PROGS(LCOV, lcov, no)
+       AC_PATH_PROGS(GENHTML, genhtml, no)
+       AC_PATH_PROGS(SCREEN, screen, no)
+       AC_PATH_PROGS(GTLSSRV, gnutls-serv, no)
+       AC_PATH_PROGS(SUDO, sudo, no)
+       AC_PATH_PROGS(INETSIM, inetsim, no)
+       if test "x$have_sput_h" = "xno" || \
+          test "x$NCAT" = "xno" || \
+          test "x$VALGRIND" = "xno" || \
+          test "x$LCOV" = "xno" || \
+          test "x$GENHTML" = "xno" || \
+          test "x$SCREEN" = "xno" || \
+          test "x$GTLSSRV" = "xno" || \
+          test "x$SUDO" = "xno" || \
+          test "x$INETSIM" = "xno" ; then
+           AC_MSG_ERROR([*** Cannot run libnetclient tests, see libnetclient/README for fixing this issue 
***])
+       fi
+fi
+
 dnl #####################################################################
 dnl 8. Output
 dnl #####################################################################
 
-AC_OUTPUT([
+AC_CONFIG_FILES([
 Makefile
 balsa.1
 balsa.spec
@@ -885,9 +893,14 @@ doc/Makefile
 libbalsa/Makefile
 libbalsa/imap/Makefile
 libinit_balsa/Makefile
+libnetclient/Makefile
+libnetclient/test/Makefile
+libnetclient/test/start-test-env.sh
 src/Makefile
 ui/Makefile
-])
+],
+[chmod +x libnetclient/test/start-test-env.sh])
+AC_OUTPUT
 
 dnl ##########################################################################
 dnl Summary
@@ -897,7 +910,6 @@ echo ""
 echo "================ Final configuration ==================="
 echo "    Installing into prefix: $prefix"
 echo "   Enable compile warnings: $set_more_warnings"
-echo "                Use ESMTP : $with_esmtp"
 echo "               HTML widget: $use_html_widget"
 echo "                 Use GNOME: $with_gnome"
 echo "              Use Canberra: $with_canberra"
diff --git a/libbalsa/Makefile.am b/libbalsa/Makefile.am
index d3f2c2a..d770c41 100644
--- a/libbalsa/Makefile.am
+++ b/libbalsa/Makefile.am
@@ -88,6 +88,8 @@ libbalsa_a_SOURCES =          \
        filter.h                \
        folder-scanners.c       \
        folder-scanners.h       \
+       gmime-filter-header.c      \
+       gmime-filter-header.h      \
        gmime-stream-gio.c      \
        gmime-stream-gio.h      \
        html.c                  \
@@ -164,6 +166,7 @@ EXTRA_DIST =                                \
        ${libbalsa_smime_extra_dist}
 
 AM_CPPFLAGS = -I${top_builddir} -I${top_srcdir} -I${top_srcdir}/libbalsa \
+       -I${top_srcdir}/libnetclient \
        -I${top_srcdir}/libbalsa/imap \
        $(BALSA_DEFS)
 
diff --git a/libbalsa/identity.c b/libbalsa/identity.c
index c6c3be6..fa971ea 100644
--- a/libbalsa/identity.c
+++ b/libbalsa/identity.c
@@ -33,10 +33,8 @@
 #  include "macosx-helpers.h"
 #endif
 
-#if ENABLE_ESMTP
 #include <string.h>
 #include "smtp-server.h"
-#endif                          /* ENABLE_ESMTP */
 
 /*
  * The class.
@@ -139,10 +137,8 @@ libbalsa_identity_finalize(GObject * object)
     g_free(ident->reply_string);
     g_free(ident->forward_string);
     g_free(ident->signature_path);
-#if ENABLE_ESMTP
     if (ident->smtp_server)
         g_object_unref(ident->smtp_server);
-#endif                          /* ENABLE_ESMTP */
     g_free(ident->face);
     g_free(ident->x_face);
     g_free(ident->force_key_id);
@@ -387,7 +383,6 @@ libbalsa_identity_get_signature(LibBalsaIdentity* identity, GtkWindow *parent)
     return ret;
 }
 
-#if ENABLE_ESMTP
 void
 libbalsa_identity_set_smtp_server(LibBalsaIdentity * ident,
                                   LibBalsaSmtpServer * smtp_server)
@@ -400,7 +395,6 @@ libbalsa_identity_set_smtp_server(LibBalsaIdentity * ident,
     if (smtp_server)
        g_object_ref(smtp_server);
 }
-#endif                          /* ENABLE_ESMTP */
 
 
 /* Used by both dialogs: */
@@ -614,8 +608,6 @@ static void ident_dialog_add_file_chooser_button(GtkWidget * grid,
 static void ident_dialog_add_boxes(GtkWidget * grid, gint row,
                                    GtkDialog * dialog, const gchar * key1,
                                    const gchar * key2);
-#if ENABLE_ESMTP
-#endif /* ENABLE_ESMTP */
 static gchar *ident_dialog_get_text(GObject *, const gchar *);
 static gboolean ident_dialog_get_bool(GObject *, const gchar *);
 static gchar *ident_dialog_get_path(GObject * dialog, const gchar * key);
@@ -654,7 +646,6 @@ static void ident_dialog_free_values(GPtrArray * values);
 static void display_frame_set_gpg_mode(GObject * dialog,
                                        const gchar * key, gint * value);
 
-#if ENABLE_ESMTP
 static void ident_dialog_add_smtp_menu(GtkWidget * grid, gint row,
                                        GtkDialog * dialog,
                                        const gchar * label_name,
@@ -663,7 +654,6 @@ static void ident_dialog_add_smtp_menu(GtkWidget * grid, gint row,
 static void display_frame_set_server(GObject * dialog,
                                      const gchar * key,
                                      LibBalsaSmtpServer * smtp_server);
-#endif /* ENABLE_ESMTP */
 
 static gpointer ident_dialog_get_value(GObject * dialog,
                                        const gchar * key);
@@ -969,14 +959,9 @@ static const struct {
 static void md_sig_path_changed_cb(GtkToggleButton * sig_button,
                                    GObject * dialog);
 
-#if ENABLE_ESMTP
 static GtkWidget*
 setup_ident_frame(GtkDialog * dialog, gboolean createp, gpointer tree,
                   GSList * smtp_servers)
-#else                           /* ENABLE_ESMTP */
-static GtkWidget*
-setup_ident_frame(GtkDialog * dialog, gboolean createp, gpointer tree)
-#endif                          /* ENABLE_ESMTP */
 {
     GtkNotebook *notebook = GTK_NOTEBOOK(gtk_notebook_new());
     GtkWidget *grid;
@@ -1025,10 +1010,8 @@ setup_ident_frame(GtkDialog * dialog, gboolean createp, gpointer tree)
     ident_dialog_add_boxes(grid, row++, dialog,
                            path_info[LBI_PATH_TYPE_FACE].box_key,
                            path_info[LBI_PATH_TYPE_XFACE].box_key);
-#if ENABLE_ESMTP
     ident_dialog_add_smtp_menu(grid, row++, dialog, _("SMT_P server:"),
                                "identity-smtp-server", smtp_servers);
-#endif /* ENABLE_ESMTP */
 
     /* create the "Signature" tab */
     grid = append_ident_notebook_page(notebook, _("Signature"), NULL);
@@ -1474,11 +1457,9 @@ ident_dialog_update(GObject * dlg)
     g_free(id->forward_string);
     id->forward_string  = ident_dialog_get_text(dlg, "identity-forwardstring");
     id->send_mp_alternative = ident_dialog_get_bool(dlg, "identity-sendmpalternative");
-#if ENABLE_ESMTP
     if(id->smtp_server) g_object_unref(id->smtp_server);
     id->smtp_server = ident_dialog_get_value(dlg, "identity-smtp-server");
     g_object_ref(id->smtp_server);
-#endif /* ENABLE_ESMTP */
 
     g_free(id->signature_path);
     id->signature_path  = ident_dialog_get_text(dlg, "identity-sigpath");
@@ -1689,7 +1670,6 @@ help_ident_cb(GtkWidget * widget)
    identites, the default one. Additionally, a callback is passed that
    will be executed when the identity list is modified: new entries
    are added or other entries are removed. */
-#if ENABLE_ESMTP
 static void
 lbi_free_smtp_server_list(GSList ** smtp_server_list)
 {
@@ -1697,15 +1677,11 @@ lbi_free_smtp_server_list(GSList ** smtp_server_list)
     g_slist_free(*smtp_server_list);
     g_free(smtp_server_list);
 }
-#endif /* ENABLE_ESMTP */
 
 void
 libbalsa_identity_config_dialog(GtkWindow *parent, GList **identities,
-                               LibBalsaIdentity **default_id,
-#if ENABLE_ESMTP
-                               GSList * smtp_servers,
-#endif /* ENABLE_ESMTP */
-                                void (*changed_cb)(gpointer))
+                               LibBalsaIdentity **default_id, GSList * smtp_servers,
+                               void (*changed_cb)(gpointer))
 {
     static GtkWidget *dialog = NULL;
     GtkWidget* frame;
@@ -1713,9 +1689,7 @@ libbalsa_identity_config_dialog(GtkWindow *parent, GList **identities,
     GtkWidget* hbox;
     GtkTreeView* tree;
     GtkTreeSelection *select;
-#if ENABLE_ESMTP
     GSList **smtp_server_list;
-#endif /* ENABLE_ESMTP */
 
     /* Show only one dialog at a time. */
     if (dialog) {
@@ -1755,7 +1729,6 @@ libbalsa_identity_config_dialog(GtkWindow *parent, GList **identities,
 
     gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, FALSE, 0);
 
-#if ENABLE_ESMTP
     smtp_server_list = g_new(GSList *, 1);
     *smtp_server_list = g_slist_copy(smtp_servers);
     g_slist_foreach(smtp_servers, (GFunc) g_object_ref, NULL);
@@ -1764,9 +1737,6 @@ libbalsa_identity_config_dialog(GtkWindow *parent, GList **identities,
     g_object_weak_ref(G_OBJECT(display_frame),
                      (GWeakNotify) lbi_free_smtp_server_list,
                      smtp_server_list);
-#else  /* ENABLE_ESMTP */
-    display_frame = setup_ident_frame(GTK_DIALOG(dialog), FALSE, tree);
-#endif /* ENABLE_ESMTP */
 
     gtk_box_pack_start(GTK_BOX(hbox), display_frame, TRUE, TRUE, 0);
 
@@ -1849,10 +1819,8 @@ display_frame_update(GObject * dialog, LibBalsaIdentity* ident)
                             ident->forward_string);
     display_frame_set_boolean(dialog, "identity-sendmpalternative",
                               ident->send_mp_alternative);
-#if ENABLE_ESMTP
     display_frame_set_server(dialog, "identity-smtp-server",
                              ident->smtp_server);
-#endif /* ENABLE_ESMTP */
 
     display_frame_set_path(dialog, "identity-sigpath",
                            ident->signature_path, FALSE);
@@ -2017,11 +1985,9 @@ libbalsa_identity_save(LibBalsaIdentity* ident, const gchar* group)
     libbalsa_conf_set_string("ReplyString", ident->reply_string);
     libbalsa_conf_set_string("ForwardString", ident->forward_string);
     libbalsa_conf_set_bool("SendMultipartAlternative", ident->send_mp_alternative);
-#if ENABLE_ESMTP
     libbalsa_conf_set_string("SmtpServer",
                              libbalsa_smtp_server_get_name(ident->
                                                            smtp_server));
-#endif                          /* ENABLE_ESMTP */
 
     libbalsa_conf_set_string("SignaturePath", ident->signature_path);
     libbalsa_conf_set_bool("SigExecutable", ident->sig_executable);
@@ -2150,7 +2116,6 @@ ident_dialog_free_values(GPtrArray * values)
     g_ptr_array_free(values, TRUE);
 }
 
-#if ENABLE_ESMTP
 static void
 ident_dialog_add_smtp_menu(GtkWidget * grid, gint row, GtkDialog * dialog,
                            const gchar * label_name,
@@ -2196,8 +2161,6 @@ display_frame_set_server(GObject * dialog, const gchar * key,
     }
 }
 
-#endif                          /* ENABLE_ESMTP */
-
 /*
  * Get the value of the active option menu item
  */
diff --git a/libbalsa/identity.h b/libbalsa/identity.h
index 6346888..ad04647 100644
--- a/libbalsa/identity.h
+++ b/libbalsa/identity.h
@@ -84,9 +84,7 @@ G_BEGIN_DECLS
        gboolean warn_send_plain;
        gint crypt_protocol;
         gchar *force_key_id;
-#if ENABLE_ESMTP
        LibBalsaSmtpServer *smtp_server;
-#endif                          /* ENABLE_ESMTP */
     };
 
     struct _LibBalsaIdentityClass 
@@ -116,11 +114,9 @@ G_BEGIN_DECLS
     void libbalsa_identity_set_sig_prepend(LibBalsaIdentity*, gboolean);
     gchar* libbalsa_identity_get_signature(LibBalsaIdentity*, 
                                            GtkWindow *parent);
-#if ENABLE_ESMTP
     void libbalsa_identity_set_smtp_server(LibBalsaIdentity * ident,
                                            LibBalsaSmtpServer *
                                            smtp_server);
-#endif                          /* ENABLE_ESMTP */
 
     void libbalsa_identity_set_gpg_sign(LibBalsaIdentity*, gboolean);
     void libbalsa_identity_set_gpg_encrypt(LibBalsaIdentity*, gboolean);
@@ -129,9 +125,7 @@ G_BEGIN_DECLS
     void libbalsa_identity_config_dialog(GtkWindow * parent,
                                          GList ** identities,
                                          LibBalsaIdentity ** current,
-#if ENABLE_ESMTP
-                                        GSList * smtp_servers,
-#endif                          /* ENABLE_ESMTP */
+                                                                                GSList * smtp_servers,
                                          void (*changed_cb)(gpointer));
 
     typedef void (*LibBalsaIdentityCallback) (gpointer data,
diff --git a/libbalsa/libbalsa.h b/libbalsa/libbalsa.h
index 49b00c1..bff2fbe 100644
--- a/libbalsa/libbalsa.h
+++ b/libbalsa/libbalsa.h
@@ -34,9 +34,7 @@ typedef struct _LibBalsaMessage LibBalsaMessage;
 typedef struct _LibBalsaMessageHeaders LibBalsaMessageHeaders;
 typedef struct _LibBalsaMessageBody LibBalsaMessageBody;
 typedef struct _LibBalsaServer LibBalsaServer;
-#if ENABLE_ESMTP
 typedef struct _LibBalsaSmtpServer LibBalsaSmtpServer;
-#endif                          /* ENABLE_ESMTP */
 typedef struct _LibbalsaVfs LibbalsaVfs;
 
 
diff --git a/libbalsa/send.c b/libbalsa/send.c
index d38a00b..b7322f4 100644
--- a/libbalsa/send.c
+++ b/libbalsa/send.c
@@ -22,7 +22,7 @@
 # include "config.h"
 #endif                          /* HAVE_CONFIG_H */
 
-#define _DEFAULT_SOURCE 1 
+#define _DEFAULT_SOURCE 1
 #define _POSIX_C_SOURCE 199309L
 #include "send.h"
 
@@ -41,61 +41,35 @@
 #include "missing.h"
 #include "information.h"
 
-#if ENABLE_ESMTP
-#include <auth-client.h>
-#include <libesmtp.h>
+#include "net-client-smtp.h"
+#include "gmime-filter-header.h"
 #include "smtp-server.h"
-#include <stdarg.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#endif /* ESMTP */
-
-#include <sys/utsname.h>
 
 #include <glib/gi18n.h>
 
 typedef struct _MessageQueueItem MessageQueueItem;
 
 struct _MessageQueueItem {
-    char tempfile[_POSIX_PATH_MAX];
     LibBalsaMessage *orig;
     GMimeStream *stream;
     LibBalsaFccboxFinder finder;
-#if !ENABLE_ESMTP
-    MessageQueueItem *next_message;
-    enum {MQI_WAITING, MQI_FAILED,MQI_SENT} status;
-#else
-    long message_size;
-    long sent;
-    long acc;
-    long update;
-    int refcount;
-    gboolean error;
-#endif
+    NetClientSmtpMessage *smtp_msg;
+    gint64 message_size;
+    gint64 sent;
+    gint64 acc;
+    gint64 update;
 };
 
 typedef struct _SendMessageInfo SendMessageInfo;
 
-struct _SendMessageInfo{
-    LibBalsaMailbox* outbox;
-#if ENABLE_ESMTP
-    /* [BCS] - The smtp_session_t structure holds all the information
-       needed to transfer the message to the SMTP server.  This structure
-       is opaque to the application. */
-    smtp_session_t session;
-#else
-    LibBalsaFccboxFinder finder;
-#endif
+struct _SendMessageInfo {
+    LibBalsaMailbox *outbox;
+    NetClientSmtp *session;
+    gchar *mta_name;
+    GList *items;              /* of MessageQueueItem */
     gboolean debug;
 };
 
-/* Variables storing the state of the sending thread.
- * These variables are protected in MT by send_messages_lock mutex */
-#if ENABLE_ESMTP
-#else
-static MessageQueueItem *message_queue;
-#endif
 static int sending_threads = 0; /* how many sending threads are active? */
 /* end of state variables section */
 
@@ -132,64 +106,48 @@ msg_queue_item_new(LibBalsaFccboxFinder finder)
 {
     MessageQueueItem *mqi;
 
-    mqi = g_new(MessageQueueItem, 1);
+    mqi = g_new0(MessageQueueItem, 1U);
     mqi->finder = finder;
-#if !ENABLE_ESMTP
-    mqi->next_message = NULL;
-    mqi->status = MQI_WAITING;
-#else
-    mqi->error = FALSE;
-#endif
-    mqi->stream=NULL;
-    mqi->tempfile[0] = '\0';
     return mqi;
 }
 
 static void
-msg_queue_item_destroy(MessageQueueItem * mqi)
+msg_queue_item_destroy(MessageQueueItem *mqi)
 {
-    if (*mqi->tempfile)
-       unlink(mqi->tempfile);
-    if (mqi->stream)
-       g_object_unref(mqi->stream);
-    if (mqi->orig)
-       g_object_unref(mqi->orig);
+       if (mqi->smtp_msg != NULL) {
+               net_client_smtp_msg_free(mqi->smtp_msg);
+       }
+    if (mqi->stream != NULL) {
+       g_object_unref(G_OBJECT(mqi->stream));
+       }
+    if (mqi->orig != NULL) {
+       g_object_unref(G_OBJECT(mqi->orig));
+       }
     g_free(mqi);
 }
 
-#if ENABLE_ESMTP
 static SendMessageInfo *
-send_message_info_new(LibBalsaMailbox* outbox, smtp_session_t session,
-                      gboolean debug)
+send_message_info_new(LibBalsaMailbox *outbox, NetClientSmtp *session, const gchar *name)
 {
     SendMessageInfo *smi;
 
-    smi=g_new(SendMessageInfo,1);
+    smi = g_new0(SendMessageInfo, 1);
     smi->session = session;
     smi->outbox = outbox;
-    smi->debug = debug;
-
-    return smi;
-}
-#else
-static SendMessageInfo *
-send_message_info_new(LibBalsaMailbox* outbox,
-                      LibBalsaFccboxFinder finder, gboolean debug)
-{
-    SendMessageInfo *smi;
-
-    smi=g_new(SendMessageInfo,1);
-    smi->outbox = outbox;
-    smi->finder = finder;
-    smi->debug = debug;
-
+    smi->mta_name = g_strdup(name);
     return smi;
 }
-#endif
 
 static void
 send_message_info_destroy(SendMessageInfo *smi)
 {
+       if (smi->session != NULL) {
+               g_object_unref(G_OBJECT(smi->session));
+       }
+       if (smi->items != NULL) {
+               g_list_free(smi->items);
+       }
+       g_free(smi->mta_name);
     g_free(smi);
 }
 
@@ -203,7 +161,7 @@ do_multipart_crypto(LibBalsaMessage * message, GMimeObject ** mime_root,
                    GtkWindow * parent, GError ** error);
 #endif
 
-static guint balsa_send_message_real(SendMessageInfo* info);
+static gboolean balsa_send_message_real(SendMessageInfo* info);
 static LibBalsaMsgCreateResult
 libbalsa_message_create_mime_message(LibBalsaMessage* message,
                                     gboolean flow, gboolean postponing,
@@ -436,15 +394,11 @@ static void libbalsa_set_message_id(GMimeMessage * mime_message);
 LibBalsaMsgCreateResult
 libbalsa_message_queue(LibBalsaMessage * message, LibBalsaMailbox * outbox,
                       LibBalsaMailbox * fccbox,
-#if ENABLE_ESMTP
-                       LibBalsaSmtpServer * smtp_server,
-#endif /* ESMTP */
+                          LibBalsaSmtpServer * smtp_server,
                       gboolean flow, GError ** error)
 {
     LibBalsaMsgCreateResult result;
-#if ENABLE_ESMTP
     guint big_message;
-#endif /* ESMTP */
     gboolean rc;
 
     g_assert(error != NULL);
@@ -459,7 +413,6 @@ libbalsa_message_queue(LibBalsaMessage * message, LibBalsaMailbox * outbox,
                                   fccbox->url);
     g_mime_object_set_header(GMIME_OBJECT(message->mime_msg), "X-Balsa-DSN",
                             message->request_dsn ? "1" : "0");
-#if ENABLE_ESMTP
     g_mime_object_set_header(GMIME_OBJECT(message->mime_msg), "X-Balsa-SmtpServer",
                              libbalsa_smtp_server_get_name(smtp_server));
 
@@ -495,9 +448,6 @@ libbalsa_message_queue(LibBalsaMessage * message, LibBalsaMailbox * outbox,
         message->mime_msg = mime_msg;
     } else
         rc = libbalsa_message_copy(message, outbox, error);
-#else                           /* ESMTP */
-    rc = libbalsa_message_copy(message, outbox, error);
-#endif                          /* ESMTP */
 
     return rc ?  LIBBALSA_MESSAGE_CREATE_OK : LIBBALSA_MESSAGE_QUEUE_ERROR;
 }
@@ -506,7 +456,6 @@ libbalsa_message_queue(LibBalsaMessage * message, LibBalsaMailbox * outbox,
    send the given messsage (if any, it can be NULL) and all the messages
    in given outbox.
 */
-#if ENABLE_ESMTP
 static gboolean lbs_process_queue(LibBalsaMailbox * outbox,
                                   LibBalsaFccboxFinder finder,
                                   LibBalsaSmtpServer * smtp_server,
@@ -537,89 +486,131 @@ libbalsa_message_send(LibBalsaMessage * message, LibBalsaMailbox * outbox,
 
     return result;
 }
-#else /* ENABLE_ESMTP */
-LibBalsaMsgCreateResult
-libbalsa_message_send(LibBalsaMessage* message, LibBalsaMailbox* outbox,
-                     LibBalsaMailbox* fccbox, LibBalsaFccboxFinder finder,
-                      gboolean flow, gboolean debug,
-                     GError ** error)
-{
-    LibBalsaMsgCreateResult result = LIBBALSA_MESSAGE_CREATE_OK;
-
-    if (message != NULL)
-       result = libbalsa_message_queue(message, outbox, fccbox, flow, error);
-    if (result == LIBBALSA_MESSAGE_CREATE_OK)
-       if (!libbalsa_process_queue(outbox, finder, debug))
-           return LIBBALSA_MESSAGE_SEND_ERROR;
- 
-    return result;
-}
-#endif /* ENABLE_ESMTP */
 
-#if ENABLE_ESMTP
-/* [BCS] - libESMTP uses a callback function to read the message from the
-   application to the SMTP server.
- */
-#define BUFLEN 8192
 
-static const char *
-libbalsa_message_cb (void **buf, int *len, void *arg)
+static void
+add_recipients(NetClientSmtpMessage *message, InternetAddressList * recipient_list, gboolean request_dsn)
 {
-    MessageQueueItem *current_message = arg;
-    GMimeStreamMem *mem_stream = GMIME_STREAM_MEM(current_message->stream);
-    char *ptr;
+       if (recipient_list != NULL) {
+               const InternetAddress *ia;
+               NetClientSmtpDsnMode dsn_mode;
+               int i;
 
-    if (len == NULL) {
-       g_mime_stream_reset(current_message->stream);
-       return NULL;
-    }
+               /* XXX  - It would be cool if LibBalsaAddress could contain DSN options
+              for a particular recipient.  For the time being, just use a switch */
+               if (request_dsn) {
+                       dsn_mode = NET_CLIENT_SMTP_DSN_SUCCESS + NET_CLIENT_SMTP_DSN_FAILURE + 
NET_CLIENT_SMTP_DSN_DELAY;
+               } else {
+                       dsn_mode = NET_CLIENT_SMTP_DSN_NEVER;
+               }
 
-    /* note: the following calculation works *only* for messages < 2 GB
-     * due to the limited range of int, but this should be safe... */
-    *len = g_mime_stream_length(current_message->stream)
-           - g_mime_stream_tell(current_message->stream);
-    ptr = (char *) mem_stream->buffer->data
-           + g_mime_stream_tell(current_message->stream);
-    g_mime_stream_seek(current_message->stream, *len, GMIME_STREAM_SEEK_CUR);
+               for (i = 0; i < internet_address_list_length(recipient_list); i++) {
+                       ia = internet_address_list_get_address(recipient_list, i);
 
-    return ptr;
+                       if (INTERNET_ADDRESS_IS_MAILBOX(ia)) {
+                               net_client_smtp_msg_add_recipient(message, 
INTERNET_ADDRESS_MAILBOX(ia)->addr, dsn_mode);
+                       } else {
+                               add_recipients(message, INTERNET_ADDRESS_GROUP(ia)->members, request_dsn);
+                       }
+               }
+       }
 }
 
-static void
-add_recipients(smtp_message_t message,
-               InternetAddressList * recipient_list,
-               gboolean request_dsn)
+
+static gssize
+send_message_data_cb(gchar *buffer, gsize count, gpointer user_data, GError **error)
 {
-    const InternetAddress *ia;
-    int i;
-    
-    if (recipient_list == NULL)
-       return;
-    
-    for (i = 0; i < internet_address_list_length (recipient_list); i++) {
-        ia = internet_address_list_get_address (recipient_list, i);
+       ssize_t read_res;
+       MessageQueueItem *mqi = (MessageQueueItem *) user_data;
+
+       read_res = g_mime_stream_read(mqi->stream, buffer, count);
+       if ((mqi->message_size > 0) && (read_res > 0)) {
+               mqi->acc += read_res;
+               if (mqi->acc >= mqi->update) {
+                       float percent;
+                       SendThreadMessage *threadmsg;
+
+                       mqi->sent += mqi->acc;
+                       mqi->acc = 0;
+                       percent = (float) mqi->sent / (float) mqi->message_size;
+                       if (percent > 1.0F) {
+                               percent = 1.0F;
+                       }
+                       MSGSENDTHREAD(threadmsg, MSGSENDTHREADPROGRESS, "", NULL, NULL, percent);
+               }
+       }
+       return read_res;
+}
 
-       if (INTERNET_ADDRESS_IS_MAILBOX (ia)) {
-           smtp_recipient_t recipient;
 
-            recipient =
-                smtp_add_recipient(message,
-                                   INTERNET_ADDRESS_MAILBOX(ia)->addr);
-           if (request_dsn) {
-                smtp_dsn_set_notify(recipient,
-                                    Notify_SUCCESS | Notify_FAILURE |
-                                    Notify_DELAY);
+static gboolean
+check_cert(NetClient *client, GTlsCertificate *peer_cert, GTlsCertificateFlags errors, gpointer user_data)
+{
+       GByteArray *cert_der = NULL;
+       gboolean result = FALSE;
+
+       /* FIXME - this a hack, simulating the (OpenSSL based) input for libbalsa_is_cert_known().  If we 
switch completely to
+        * (GnuTLS based) GTlsCertificate/GTlsClientConnection, we can omit this... */
+       g_debug("%s: %p %p %u %p", __func__, client, peer_cert, errors, user_data);
+
+       /* create a OpenSSL X509 object from the certificate's DER data */
+       g_object_get(G_OBJECT(peer_cert), "certificate", &cert_der, NULL);
+       if (cert_der != NULL) {
+               X509 *ossl_cert;
+               const unsigned char *der_p;
+
+               der_p = (const unsigned char *) cert_der->data;
+               ossl_cert = d2i_X509(NULL, &der_p, cert_der->len);
+               g_byte_array_unref(cert_der);
+
+               if (ossl_cert != NULL) {
+                       long vfy_result;
+
+                       /* convert the GIO error flags into OpenSSL error flags */
+                       if ((errors & G_TLS_CERTIFICATE_UNKNOWN_CA) == G_TLS_CERTIFICATE_UNKNOWN_CA) {
+                               vfy_result = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
+                       } else if ((errors & G_TLS_CERTIFICATE_BAD_IDENTITY) == 
G_TLS_CERTIFICATE_BAD_IDENTITY) {
+                               vfy_result = X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
+                       } else if ((errors & G_TLS_CERTIFICATE_NOT_ACTIVATED) == 
G_TLS_CERTIFICATE_NOT_ACTIVATED) {
+                               vfy_result = X509_V_ERR_CERT_NOT_YET_VALID;
+                       } else if ((errors & G_TLS_CERTIFICATE_EXPIRED) == G_TLS_CERTIFICATE_EXPIRED) {
+                               vfy_result = X509_V_ERR_CERT_HAS_EXPIRED;
+                       } else if ((errors & G_TLS_CERTIFICATE_REVOKED) == G_TLS_CERTIFICATE_REVOKED) {
+                               vfy_result = X509_V_ERR_CERT_REVOKED;
+                       } else {
+                               vfy_result = X509_V_ERR_APPLICATION_VERIFICATION;
+                       }
+
+                       result = libbalsa_is_cert_known(ossl_cert, vfy_result);
+                       X509_free(ossl_cert);
+               }
+       }
 
-               /* XXX  - It would be cool if LibBalsaAddress could contain DSN options
-                  for a particular recipient.  For the time being, just use a switch */
-           }
-       } else {
-            add_recipients(message, INTERNET_ADDRESS_GROUP(ia)->members,
-                           request_dsn);
+       return result;
+}
+
+
+static gchar **
+get_auth(NetClient *client, gpointer user_data)
+{
+       LibBalsaServer *server = LIBBALSA_SERVER(user_data);
+       gchar **result = NULL;
+
+       g_debug("%s: %p %p: encrypted = %d", __func__, client, user_data, net_client_is_encrypted(client));
+       /* Note: if the usr name is empty, we assume anonymous access */
+       if ((server->try_anonymous == 0U) && (server->user != NULL) && (server->user[0] != '\0')) {
+               result = g_new0(gchar *, 3U);
+               result[0] = g_strdup(server->user);
+               if ((server->passwd != NULL) && (server->passwd[0] != '\0')) {
+                       result[1] = g_strdup(server->passwd);
+               } else {
+                       result[1] = libbalsa_server_get_password(server, NULL);
+               }
        }
-    }
+       return result;
 }
 
+
 /* libbalsa_process_queue:
    treats given mailbox as a set of messages to send. Loads them up and
    launches sending thread/routine.
@@ -631,303 +622,126 @@ add_recipients(smtp_message_t message,
  */
 static gboolean
 lbs_process_queue(LibBalsaMailbox * outbox, LibBalsaFccboxFinder finder,
-                 LibBalsaSmtpServer * smtp_server, gboolean debug,
-                  GtkWindow * parent)
+       LibBalsaSmtpServer * smtp_server, gboolean debug,
+       GtkWindow * parent)
 {
-    LibBalsaServer *server = LIBBALSA_SERVER(smtp_server);
-    MessageQueueItem *new_message;
-    SendMessageInfo *send_message_info;
-    LibBalsaMessage *msg;
-    smtp_session_t session;
-    smtp_message_t message, bcc_message;
-    const gchar *phrase, *mailbox, *subject;
-    long estimate;
-    guint msgno;
-    gchar *host_with_port;
-    GThread *send_mail;
-
-    g_mutex_lock(&send_messages_lock);
-
-    if (!libbalsa_mailbox_open(outbox, NULL)) {
-       g_mutex_unlock(&send_messages_lock);
-       return FALSE;
-    }
-    if (!libbalsa_mailbox_total_messages(outbox)) {
-       libbalsa_mailbox_close(outbox, TRUE);
-       g_mutex_unlock(&send_messages_lock);
-       return TRUE;
-    }
-    /* We create here the progress bar */
-    ensure_send_progress_dialog(parent);
-
-    /* Create the libESMTP session.  Loop over the out box and add the
-       messages to the session. */
-
-    /* FIXME - check for failure returns in the smtp_xxx() calls */
-    host_with_port = strchr(server->host, ':') ?
-        g_strdup(server->host) : g_strconcat(server->host, ":smtp", NULL);
-    session = smtp_create_session ();
-    smtp_set_server (session, host_with_port);
-    g_free(host_with_port);
+       LibBalsaServer *server = LIBBALSA_SERVER(smtp_server);
+       SendMessageInfo *send_message_info;
+       NetClientSmtp *session;
+       guint msgno;
 
-    /* Tell libESMTP how to use the SMTP STARTTLS extension.  */
-    smtp_starttls_enable (session, (enum starttls_option) server->tls_mode);
+       g_mutex_lock(&send_messages_lock);
 
-    /* Now tell libESMTP it can use the SMTP AUTH extension.  */
-    smtp_auth_set_context(session,
-                          libbalsa_smtp_server_get_authctx(smtp_server));
- 
-    /* At present Balsa can't handle one recipient only out of many
-       failing.  Make libESMTP require all specified recipients to
-       succeed before transferring a message.  */
-    smtp_option_require_all_recipients (session, 1);
-
-    for (msgno = libbalsa_mailbox_total_messages(outbox);
-        msgno > 0; msgno--) {
-       const gchar *smtp_server_name;
-        LibBalsaMsgCreateResult created;
-
-        /* Skip this message if it either FLAGGED or DELETED: */
-        if (!libbalsa_mailbox_msgno_has_flags
-            (outbox, msgno, 0,
-             (LIBBALSA_MESSAGE_FLAG_FLAGGED |
-              LIBBALSA_MESSAGE_FLAG_DELETED)))
-            continue;
-
-       msg = libbalsa_mailbox_get_message(outbox, msgno);
-        if (!msg) /* error? */
-            continue;
-        libbalsa_message_body_ref(msg, TRUE, TRUE);
-        smtp_server_name =
-            libbalsa_message_get_user_header(msg, "X-Balsa-SmtpServer");
-        if (!smtp_server_name)
-            smtp_server_name = libbalsa_smtp_server_get_name(NULL);
-        if (strcmp(smtp_server_name,
-                   libbalsa_smtp_server_get_name(smtp_server)) != 0) {
-            libbalsa_message_body_unref(msg);
-            g_object_unref(msg);
-            continue;
-        }
-        msg->request_dsn =
-               (atoi(libbalsa_message_get_user_header(msg, "X-Balsa-DSN")) != 0);
-
-       new_message = msg_queue_item_new(finder);
-        created = libbalsa_fill_msg_queue_item_from_queu(msg, new_message);
-        libbalsa_message_body_unref(msg);
+       if (!libbalsa_mailbox_open(outbox, NULL)) {
+               g_mutex_unlock(&send_messages_lock);
+               return FALSE;
+       }
 
-       if (created != LIBBALSA_MESSAGE_CREATE_OK) {
-           msg_queue_item_destroy(new_message);
+       /* create the SMTP session */
+       if (server->security == NET_CLIENT_CRYPT_ENCRYPTED) {
+               session = net_client_smtp_new(server->host, 465U, server->security);
        } else {
-            gboolean has_open_recipients;
-            guint n_bcc_recipients;
-           const InternetAddress *ia;
-
-           libbalsa_message_change_flags(msg,
-                                          LIBBALSA_MESSAGE_FLAG_FLAGGED, 0);
-           /*
-              The message needs to be filtered and the newlines converted to
-              \r\n because internally the lines foolishly terminate with the
-              Unix \n despite RFC 2822 calling for \r\n.  Furthermore RFC 822
-              states that bare \n and \r are acceptable in messages and that
-              individually they do not constitute a line termination.  This
-              requirement cannot be reconciled with storing messages with Unix
-              line terminations.  RFC 2822 relieves this situation slightly by
-              prohibiting bare \r and \n.
-
-              The following code cannot therefore work correctly in all
-              situations.  Furthermore it is very inefficient since it must
-              search for the \n.
-            */
-           {
-               GMimeStream *mem_stream;
-               GMimeStream *filter_stream;
-               GMimeFilter *filter;
-
-               mem_stream = new_message->stream;
-               filter_stream =
-                   g_mime_stream_filter_new(mem_stream);
-               filter =
-                   g_mime_filter_crlf_new( TRUE,
-                                           FALSE);
-               g_mime_stream_filter_add(GMIME_STREAM_FILTER(filter_stream),
-                                        filter);
-               g_object_unref(G_OBJECT(filter));
-               mem_stream = g_mime_stream_mem_new();
-               g_mime_stream_write_to_stream(filter_stream, mem_stream);
-               g_object_unref(filter_stream);
-               g_mime_stream_reset(mem_stream);
-               g_object_unref(new_message->stream);
-               new_message->stream = mem_stream;
-           }
-
-            /* If the message has To: or Cc: recipients, and the Bcc:
-             * recipient list is present and contains exactly one
-             * address, add an additional copy of the message to the
-             * session for that one recipient, in which the Bcc: header
-             * is preserved. */
-            has_open_recipients =
-                libbalsa_address_n_mailboxes_in_list(msg->headers->
-                                                     to_list) > 0
-                || libbalsa_address_n_mailboxes_in_list(msg->headers->
-                                                        cc_list) > 0;
-            n_bcc_recipients =
-                libbalsa_address_n_mailboxes_in_list(msg->headers->
-                                                     bcc_list);
-            if (has_open_recipients && n_bcc_recipients == 1)
-               bcc_message = smtp_add_message (session);
-           else
-               bcc_message = NULL;
-            new_message->refcount = bcc_message ? 2 : 1;
-
-           /* Add this after the Bcc: copy. */
-           message = smtp_add_message (session);
-
-           if (msg->request_dsn) {
-               smtp_dsn_set_ret(message, Ret_HDRS);
-               smtp_dsn_set_envid(message, msg->message_id);
-           }
-
-            /* The main copy must not contain a Bcc: header, unless the
-             * message has no To: recipients and no Cc: recipients, and
-             * exactly one Bcc: recipient: */
-            if ((has_open_recipients && n_bcc_recipients > 0)
-                || n_bcc_recipients > 1)
-               smtp_set_header_option (message, "Bcc", Hdr_PROHIBIT, 1);
-
-           smtp_message_set_application_data (message, new_message);
-           smtp_set_messagecb (message, libbalsa_message_cb, new_message);
-           if (bcc_message) {
-               smtp_message_set_application_data (bcc_message, new_message);
-               smtp_set_messagecb (bcc_message,
-                                   libbalsa_message_cb, new_message);
-           }
-
-#define LIBESMTP_ADDS_HEADERS
-#ifdef LIBESMTP_ADDS_HEADERS
-           /* XXX - The following calls to smtp_set_header() probably
-                    aren't necessary since they should already be in the
-                    message. */
-
-           smtp_set_header (message, "Date", &msg->headers->date);
-           if (bcc_message)
-               smtp_set_header (bcc_message, "Date", &msg->headers->date);
-
-           /* RFC 2822 does not require a message to have a subject.
-                      I assume this is NULL if not present */
-           subject = LIBBALSA_MESSAGE_GET_SUBJECT(msg);
-           if (subject) {
-               smtp_set_header (message, "Subject", subject);
-               if (bcc_message)
-                   smtp_set_header (bcc_message, "Subject", subject);
-           }
-
-           /* Add the sender info */
-            if (msg->headers->from
-               && (ia = internet_address_list_get_address (msg->headers->from, 0))) {
-               phrase = ia->name;
-               while (ia && INTERNET_ADDRESS_IS_GROUP (ia))
-                   ia = internet_address_list_get_address (INTERNET_ADDRESS_GROUP (ia)->members, 0);
-               mailbox = ia ? INTERNET_ADDRESS_MAILBOX (ia)->addr : "";
-            } else
-                phrase = mailbox = "";
-           smtp_set_reverse_path (message, mailbox);
-           smtp_set_header (message, "From", phrase, mailbox);
-           if (bcc_message) {
-               smtp_set_reverse_path (bcc_message, mailbox);
-               smtp_set_header (bcc_message, "From", phrase, mailbox);
-           }
-
-           if (msg->headers->reply_to
-               && (ia = internet_address_list_get_address (msg->headers->reply_to, 0))) {
-               phrase = ia->name;
-               while (ia && INTERNET_ADDRESS_IS_GROUP (ia))
-                   ia = internet_address_list_get_address (INTERNET_ADDRESS_GROUP (ia)->members, 0);
-               mailbox = ia ? INTERNET_ADDRESS_MAILBOX (ia)->addr : "";
-               smtp_set_header (message, "Reply-To", phrase, mailbox);
-               if (bcc_message)
-                   smtp_set_header (bcc_message, "Reply-To", phrase, mailbox);
-           }
-
-           if (msg->headers->dispnotify_to
-               && (ia = internet_address_list_get_address (msg->headers->dispnotify_to, 0))) {
-               phrase = ia->name;
-               while (ia && INTERNET_ADDRESS_IS_GROUP (ia))
-                   ia = internet_address_list_get_address (INTERNET_ADDRESS_GROUP (ia)->members, 0);
-               mailbox = ia ? INTERNET_ADDRESS_MAILBOX (ia)->addr : "";
-               smtp_set_header (message, "Disposition-Notification-To",
-                                phrase, mailbox);
-               if (bcc_message)
-                   smtp_set_header (bcc_message, "Disposition-Notification-To",
-                                    phrase, mailbox);
-           }
-#endif
-
-           /* Now need to add the recipients to the message.  The main
-              copy of the message gets the To and Cc recipient list, and
-              the Bcc recipient list, when it has more than one address.
-              The bcc copy gets the single Bcc recipient.  */
-
-            add_recipients(message, msg->headers->to_list, msg->request_dsn);
-            add_recipients(message, msg->headers->cc_list, msg->request_dsn);
-
-            add_recipients(bcc_message ? bcc_message : message, 
-                           msg->headers->bcc_list, msg->request_dsn);
-
-           /* Prohibit status headers. */
-           smtp_set_header_option(message, "Status", Hdr_PROHIBIT, 1);
-           smtp_set_header_option(message, "X-Status", Hdr_PROHIBIT, 1);
-            /* ... and all X-Balsa-* headers. */
-            smtp_set_header_option(message, "X-Balsa-", Hdr_PROHIBIT, 1);
+               // FIXME - submission (587) is the standard, but most isp's use 25...
+               session = net_client_smtp_new(server->host, 587U, server->security);
+       }
+       // FIXME - set user cert and connect cert-pass signal if we have a user cert
+       g_signal_connect(G_OBJECT(session), "cert-check", G_CALLBACK(check_cert), session);     // FIXME!!
+       g_signal_connect(G_OBJECT(session), "auth", G_CALLBACK(get_auth), smtp_server);
+
+       send_message_info = send_message_info_new(outbox, session, 
libbalsa_smtp_server_get_name(smtp_server));
+
+       for (msgno = libbalsa_mailbox_total_messages(outbox); msgno > 0U; msgno--) {
+               MessageQueueItem *new_message;
+               LibBalsaMessage *msg;
+               const gchar *smtp_server_name;
+               LibBalsaMsgCreateResult created;
+
+               /* Skip this message if it either FLAGGED or DELETED: */
+               if (!libbalsa_mailbox_msgno_has_flags(outbox, msgno, 0, (LIBBALSA_MESSAGE_FLAG_FLAGGED | 
LIBBALSA_MESSAGE_FLAG_DELETED)))
+                       continue;
+
+               msg = libbalsa_mailbox_get_message(outbox, msgno);
+               if (!msg) /* error? */
+                       continue;
+               libbalsa_message_body_ref(msg, TRUE, TRUE);
+               smtp_server_name = libbalsa_message_get_user_header(msg, "X-Balsa-SmtpServer");
+               if (!smtp_server_name)
+                       smtp_server_name = libbalsa_smtp_server_get_name(NULL);
+               if (strcmp(smtp_server_name, libbalsa_smtp_server_get_name(smtp_server)) != 0) {
+                       libbalsa_message_body_unref(msg);
+                       g_object_unref(msg);
+                       continue;
+               }
+               msg->request_dsn = (atoi(libbalsa_message_get_user_header(msg, "X-Balsa-DSN")) != 0);
 
+               new_message = msg_queue_item_new(finder);
+               created = libbalsa_fill_msg_queue_item_from_queu(msg, new_message);
+               libbalsa_message_body_unref(msg);
 
-           /* Estimate the size of the message.  This need not be exact
-              but it's better to err on the large side since some message
-              headers may be altered during the transfer. */
-           new_message->message_size = g_mime_stream_length(new_message->stream);
+               if (created != LIBBALSA_MESSAGE_CREATE_OK) {
+                       msg_queue_item_destroy(new_message);
+               } else {
+                       const InternetAddress *ia;
+                       const gchar *mailbox;
+
+                       libbalsa_message_change_flags(msg, LIBBALSA_MESSAGE_FLAG_FLAGGED, 0);
+
+                       send_message_info->items = g_list_prepend(send_message_info->items, new_message);
+                       new_message->smtp_msg = net_client_smtp_msg_new(send_message_data_cb, new_message);
+
+                       if (msg->request_dsn) {
+                               net_client_smtp_msg_set_dsn_opts(new_message->smtp_msg, msg->message_id, 
FALSE);
+                       }
+
+                       /* Add the sender info */
+                       if (msg->headers->from && (ia = internet_address_list_get_address(msg->headers->from, 
0))) {
+                               while (ia && INTERNET_ADDRESS_IS_GROUP(ia)) {
+                                       ia = internet_address_list_get_address 
(INTERNET_ADDRESS_GROUP(ia)->members, 0);
+                               }
+                               mailbox = ia ? INTERNET_ADDRESS_MAILBOX(ia)->addr : "";
+                       } else {
+                               mailbox = "";
+                       }
+                       net_client_smtp_msg_set_sender(new_message->smtp_msg, mailbox);
+
+                       /* Now need to add the recipients to the message. */
+                       add_recipients(new_message->smtp_msg, msg->headers->to_list, msg->request_dsn);
+                       add_recipients(new_message->smtp_msg, msg->headers->cc_list, msg->request_dsn);
+                       add_recipients(new_message->smtp_msg, msg->headers->bcc_list, msg->request_dsn);
+
+                       /* Estimate the size of the message.  This need not be exact but it's better to err 
on the large side since some
+                        * message headers may be altered during the transfer. */
+                       new_message->message_size = g_mime_stream_length(new_message->stream);
+
+                       /* Set up counters for the progress bar.  Update is the byte count when the progress 
bar should be updated.  This is
+                        * capped around 5k so that the progress bar moves about once per second on a slow 
line.  On small messages it is
+                        * smaller to allow smooth progress of the bar. */
+                       new_message->update = new_message->message_size / 20;
+                       if (new_message->update < 100) {
+                               new_message->update = 100;
+                       } else if (new_message->update > 5 * 1024) {
+                               new_message->update = 5 * 1024;
+                       }
+                       new_message->sent = 0;
+                       new_message->acc = 0;
+               }
+               g_object_unref(msg);
+       }
 
-           if (new_message->message_size > 0) {
-               estimate = new_message->message_size;
-               estimate += 1024 - (estimate % 1024);
-               smtp_size_set_estimate (message, estimate);
-               if (bcc_message)
-                   smtp_size_set_estimate (bcc_message, estimate);
-           }
+       /* launch the thread for sending the messages only if we collected any */
+       if (send_message_info->items != NULL) {
+               GThread *send_mail;
 
-           /* Set up counters for the progress bar.  Update is the byte
-              count when the progress bar should be updated.  This is
-              capped around 5k so that the progress bar moves about once
-              per second on a slow line.  On small messages it is smaller
-              to allow smooth progress of the bar. */
-           new_message->update = new_message->message_size / 20;
-           if (new_message->update < 100)
-               new_message->update = 100;
-           else if (new_message->update > 5 * 1024)
-               new_message->update = 5 * 1024;
-           new_message->sent = 0;
-           new_message->acc = 0;
+               ensure_send_progress_dialog(parent);
+               sending_threads++;
+               send_mail = g_thread_new("balsa_send_message_real", (GThreadFunc) balsa_send_message_real, 
send_message_info);
+               g_thread_unref(send_mail);
+       } else {
+               send_message_info_destroy(send_message_info);
        }
-       g_object_unref(msg);
-    }
 
-   /* At this point the session is ready to be sent.  As I've written the
-      code, a new smtp session is created for every call here.  Therefore
-      a new thread is always required to dispatch it.
-    */
-
-    send_message_info=send_message_info_new(outbox, session, debug);
-
-    sending_threads++;
-    send_mail =
-       g_thread_new("balsa_send_message_real",
-                                (GThreadFunc) balsa_send_message_real,
-                                        send_message_info);
-    /* Detach so we don't need to g_thread_join
-     * This means that all resources will be
-     * reclaimed as soon as the thread exits
-     */
-    g_thread_unref(send_mail);
-    g_mutex_unlock(&send_messages_lock);
-    return TRUE;
+       g_mutex_unlock(&send_messages_lock);
+       return TRUE;
 }
 
 gboolean
@@ -947,391 +761,6 @@ libbalsa_process_queue(LibBalsaMailbox * outbox,
     return TRUE;
 }
 
-static void
-disp_recipient_status(smtp_recipient_t recipient,
-                      const char *mailbox, void *arg)
-{
-  const smtp_status_t *status = smtp_recipient_status (recipient);
-
-  if(status->code != 0 && status->code != 250) {
-      libbalsa_information(
-                           LIBBALSA_INFORMATION_ERROR,
-                           _("Could not send the message to %s:\n"
-                             "%d: %s\n"
-                             "Message left in your outbox.\n"), 
-                           mailbox, status->code, status->text);
-      (*(int*)arg)++;
-  }
-}
-
-static void
-handle_successful_send(smtp_message_t message, void *be_verbose)
-{
-    MessageQueueItem *mqi;
-    const smtp_status_t *status;
-
-    g_mutex_lock(&send_messages_lock);
-    /* Get the app data and decrement the reference count.  Only delete
-       structures if refcount reaches zero */
-    mqi = smtp_message_get_application_data (message);
-    if (mqi != NULL)
-      mqi->refcount--;
-
-    if(mqi != NULL && mqi->orig != NULL && mqi->orig->mailbox && !mqi->error)
-       libbalsa_message_change_flags(mqi->orig, 0,
-                                      LIBBALSA_MESSAGE_FLAG_FLAGGED);
-    else printf("mqi: %p mqi->orig: %p mqi->orig->mailbox: %p\n",
-                  mqi, mqi ? mqi->orig : NULL, 
-                  mqi&&mqi->orig ? mqi->orig->mailbox : NULL);
-    status = smtp_message_transfer_status (message);
-    if (status->code / 100 == 2) {
-       if (mqi != NULL && mqi->orig != NULL && mqi->refcount <= 0 &&
-            mqi->orig->mailbox && !mqi->error) {
-            gboolean remove = TRUE;
-            const gchar *fccurl =
-                libbalsa_message_get_user_header(mqi->orig, "X-Balsa-Fcc");
-
-           if (fccurl) {
-                LibBalsaMailbox *fccbox = mqi->finder(fccurl);
-                GError *err = NULL;
-                libbalsa_message_change_flags(mqi->orig, 0,
-                                              LIBBALSA_MESSAGE_FLAG_NEW |
-                                              LIBBALSA_MESSAGE_FLAG_FLAGGED);
-               libbalsa_mailbox_sync_storage(mqi->orig->mailbox, FALSE);
-                remove = libbalsa_message_copy(mqi->orig, fccbox, &err);
-                if(!remove) {
-                    libbalsa_information
-                        (LIBBALSA_INFORMATION_ERROR, 
-                         _("Saving sent message to %s failed: %s"),
-                         fccbox->url, err ? err->message : "?");
-                    g_clear_error(&err);
-                }
-            }
-            /* If copy failed, mark the message again as flagged -
-               otherwise it will get resent again. And again, and
-               again... */
-            libbalsa_message_change_flags(mqi->orig, remove ?
-                                          LIBBALSA_MESSAGE_FLAG_DELETED :
-                                          LIBBALSA_MESSAGE_FLAG_FLAGGED, 0);
-       }
-    } else {
-        /* Record the error, so that the message will not be deleted
-         * later... */
-        mqi->error = TRUE;
-        /* ...and mark it as:
-         *   - flagged, so it will not be sent again until the error
-         *     is fixed and the user manually clears the flag;
-         *   - undeleted, in case it was already deleted. */
-        if (mqi->orig && mqi->orig->mailbox)
-            libbalsa_message_change_flags(mqi->orig,
-                                          LIBBALSA_MESSAGE_FLAG_FLAGGED,
-                                          LIBBALSA_MESSAGE_FLAG_DELETED);
-       /* Check whether it was a problem with transfer. */
-        if(*(gboolean*)be_verbose) {
-            int cnt = 0;
-            if(status->code != 250 && status->code != 0) {
-                libbalsa_information(
-                                     LIBBALSA_INFORMATION_ERROR,
-                                     _("Relaying refused:\n"
-                                       "%d: %s\n"
-                                       "Message left in your outbox.\n"), 
-                                     status->code, status->text);
-                cnt++;
-            }
-            smtp_enumerate_recipients (message, disp_recipient_status, &cnt);
-            if(cnt==0) { /* other error, maybe sender or message size? */
-                status = smtp_reverse_path_status(message);
-                if(status->code != 250 && status->code != 0) {
-                    libbalsa_information
-                        (LIBBALSA_INFORMATION_ERROR,
-                         _("Relaying refused:\n"
-                           "%d: %s\n"
-                           "Message left in your outbox.\n"), 
-                         status->code, status->text);
-                } else
-                    libbalsa_information
-                        (LIBBALSA_INFORMATION_ERROR,
-                         _("Message submission problem, placing it into your outbox.\n"
-                           "System will attempt to resubmit the message until you delete it."));
-                
-            }
-        }
-    }
-    if (mqi != NULL && mqi->refcount <= 0)
-        msg_queue_item_destroy(mqi);
-    g_mutex_unlock(&send_messages_lock);
-}
-
-static void
-libbalsa_smtp_event_cb (smtp_session_t session, int event_no, void *arg, ...)
-{
-    SendThreadMessage *threadmsg;
-    MessageQueueItem *mqi;
-    char buf[1024];
-    va_list ap;
-    const char *mailbox;
-    smtp_message_t message;
-    smtp_recipient_t recipient;
-    const smtp_status_t *status;
-    int len;
-    float percent;
-
-    va_start (ap, arg);
-    switch (event_no) {
-    case SMTP_EV_CONNECT:
-       MSGSENDTHREAD(threadmsg, MSGSENDTHREADPROGRESS,
-                     _("Connected to MTA"),
-                     NULL, NULL, 0);
-        break;
-    case SMTP_EV_MAILSTATUS:
-        mailbox = va_arg (ap, const char *);
-        message = va_arg (ap, smtp_message_t);
-       status = smtp_reverse_path_status (message);
-
-        /* status code, mailbox */
-        snprintf (buf, sizeof buf, _("From: %d <%s>"), status->code, mailbox);
-       MSGSENDTHREAD(threadmsg, MSGSENDTHREADPROGRESS, buf, NULL, NULL, 0);
-
-        /* mailbox, status code, status text */
-        snprintf (buf, sizeof buf, _("From %s: %d %s"), 
-                 mailbox, status->code, status->text);
-        g_strchomp(buf);
-       libbalsa_information(LIBBALSA_INFORMATION_DEBUG, "%s", buf);
-        break;
-    case SMTP_EV_RCPTSTATUS:
-        mailbox = va_arg (ap, const char *);
-        recipient = va_arg (ap, smtp_recipient_t);
-       status = smtp_recipient_status (recipient);
-
-        /* status code, mailbox */
-        snprintf (buf, sizeof buf, _("To: %d <%s>"),  status->code, mailbox);
-       MSGSENDTHREAD(threadmsg, MSGSENDTHREADPROGRESS, buf, NULL, NULL, 0);
-
-        /* mailbox, status code, status text */
-        snprintf (buf, sizeof buf, _("To %s: %d %s"),
-                 mailbox, status->code, status->text);
-        g_strchomp(buf);
-       libbalsa_information(LIBBALSA_INFORMATION_DEBUG, "%s", buf);
-        break;
-    case SMTP_EV_MESSAGEDATA:
-        message = va_arg (ap, smtp_message_t);
-        len = va_arg (ap, int);
-        mqi = smtp_message_get_application_data (message);
-        if (mqi != NULL && mqi->message_size > 0) {
-           mqi->acc += len;
-           if (mqi->acc >= mqi->update) {
-               mqi->sent += mqi->acc;
-               mqi->acc = 0;
-
-               percent = (float) mqi->sent / (float) mqi->message_size;
-               if(percent>1) percent = 1;
-               MSGSENDTHREAD(threadmsg, MSGSENDTHREADPROGRESS, "", NULL, NULL,
-                             percent);
-           }
-       }
-        break;
-    case SMTP_EV_MESSAGESENT:
-        message = va_arg (ap, smtp_message_t);
-        status = smtp_message_transfer_status (message);
-       snprintf (buf, sizeof buf, "%d %s", status->code, status->text);
-        g_strchomp(buf);
-       MSGSENDTHREAD(threadmsg, MSGSENDTHREADPROGRESS, buf, NULL, NULL, 0);
-       libbalsa_information(LIBBALSA_INFORMATION_DEBUG, "%s", buf);
-        /* Reset 'mqi->sent' for the next message (i.e. bcc copy) */
-        mqi = smtp_message_get_application_data (message);
-        if (mqi != NULL) {
-           mqi->sent = 0;
-           mqi->acc = 0;
-        }
-        break;
-    case SMTP_EV_DISCONNECT:
-       MSGSENDTHREAD(threadmsg, MSGSENDTHREADPROGRESS,
-                     _("Disconnected"),
-                     NULL, NULL, 0);
-        break;
-
-        /* SMTP_TLS related things. Observe that we need to have SSL
-        * enabled in balsa to properly interpret libesmtp
-        * messages. */
-    case SMTP_EV_INVALID_PEER_CERTIFICATE: {
-        long vfy_result;
-       SSL  *ssl;
-       X509 *cert;
-        int *ok;
-        vfy_result = va_arg(ap, long); ok = va_arg(ap, int*);
-       ssl = va_arg(ap, SSL*);
-       cert = SSL_get_peer_certificate(ssl);
-       if(cert) {
-           *ok = libbalsa_is_cert_known(cert, vfy_result);
-           X509_free(cert);
-       }
-        break;
-    }
-    case SMTP_EV_NO_PEER_CERTIFICATE:
-    case SMTP_EV_WRONG_PEER_CERTIFICATE:
-#if LIBESMTP_1_0_3_AVAILABLE
-    case SMTP_EV_NO_CLIENT_CERTIFICATE:
-#endif
-    {
-       int *ok;
-       printf("SMTP-TLS event_no=%d\n", event_no);
-       ok = va_arg(ap, int*);
-       *ok = 1;
-       break;
-    }
-    }
-    va_end (ap);
-}
-
-#else /* ESMTP */
-
-/* CHBM: non-esmtp version */
-
-/* libbalsa_process_queue:
-   treats given mailbox as a set of messages to send. Loads them up and
-   launches sending thread/routine.
-   NOTE that we do not close outbox after reading. send_real/thread message 
-   handler does that.
-*/
-gboolean 
-libbalsa_process_queue(LibBalsaMailbox* outbox, LibBalsaFccboxFinder finder,
-                       gboolean debug, GtkWindow * parent)
-{
-    MessageQueueItem *mqi = NULL, *new_message;
-    SendMessageInfo *send_message_info;
-    LibBalsaMessage *msg;
-    guint msgno;
-
-    /* We do messages in queue now only if where are not sending them already */
-
-    send_lock();
-
-#ifdef BALSA_USE_THREADS
-    if (sending_threads>0) {
-       send_unlock();
-       return TRUE;
-    }
-    sending_threads++;
-#endif
-
-    ensure_send_progress_dialog(parent);
-    if (!libbalsa_mailbox_open(outbox, NULL)) {
-#ifdef BALSA_USE_THREADS
-       sending_threads--;
-       send_unlock();
-#endif
-       return FALSE;
-    }
-    for (msgno = libbalsa_mailbox_total_messages(outbox);
-        msgno > 0; msgno--) {
-        LibBalsaMsgCreateResult created;
-
-        if (libbalsa_mailbox_msgno_has_flags(outbox, msgno, 
-                                             (LIBBALSA_MESSAGE_FLAG_FLAGGED |
-                                              LIBBALSA_MESSAGE_FLAG_DELETED),
-                                             0))
-            continue;
-
-       msg = libbalsa_mailbox_get_message(outbox, msgno);
-        if (!msg) /* error? */
-            continue;
-        libbalsa_message_body_ref(msg, TRUE, TRUE); /* FIXME: do we need
-                                                      * all headers? */
-       new_message = msg_queue_item_new(finder);
-        created = libbalsa_fill_msg_queue_item_from_queu(msg, new_message);
-        libbalsa_message_body_unref(msg);
-       
-       if (created != LIBBALSA_MESSAGE_CREATE_OK) {
-           msg_queue_item_destroy(new_message);
-       } else {
-           libbalsa_message_change_flags(msg,
-                                          LIBBALSA_MESSAGE_FLAG_FLAGGED, 0);
-           if (mqi)
-               mqi->next_message = new_message;
-           else
-               message_queue = new_message;
-           mqi = new_message;
-       }
-    }
-
-    send_message_info=send_message_info_new(outbox, finder, debug);
-    
-    balsa_send_message_real(send_message_info);
-
-    send_unlock();
-    return TRUE;
-}
-
-static void
-handle_successful_send(MessageQueueItem *mqi, LibBalsaFccboxFinder finder)
-{
-    if (mqi->orig->mailbox) {
-        gboolean remove = TRUE;
-        const gchar *fccurl =
-            libbalsa_message_get_user_header(mqi->orig, "X-Balsa-Fcc");
-
-        libbalsa_message_change_flags(mqi->orig, 0,
-                                      LIBBALSA_MESSAGE_FLAG_NEW |
-                                      LIBBALSA_MESSAGE_FLAG_FLAGGED);
-       libbalsa_mailbox_sync_storage(mqi->orig->mailbox, FALSE);
-
-        if (mqi->orig->mailbox && fccurl) {
-            LibBalsaMailbox *fccbox = mqi->finder(fccurl);
-            remove =
-                libbalsa_message_copy(mqi->orig, fccbox, NULL)>=0;
-        }
-        /* If copy failed, mark the message again as flagged -
-           otherwise it will get resent again. And again, and
-           again... */
-        libbalsa_message_change_flags(mqi->orig, remove ?
-                                      LIBBALSA_MESSAGE_FLAG_DELETED :
-                                      LIBBALSA_MESSAGE_FLAG_FLAGGED, 0);
-    }
-    mqi->status = MQI_SENT;
-}
-
-/* get_msg2send: 
-   returns first waiting message on the message_queue.
-*/
-static MessageQueueItem* get_msg2send()
-{
-    MessageQueueItem* res = message_queue;
-    send_lock();
-
-    while(res && res->status != MQI_WAITING)
-       res = res->next_message;
-
-    send_unlock();
-    return res;
-}
-
-#endif /* ESMTP */
-
-#if ENABLE_ESMTP
-static void
-monitor_cb (const char *buf, int buflen, int writing, void *arg)
-{
-  FILE *fp = arg;
-
-  if (writing == SMTP_CB_HEADERS)
-    {
-      fputs ("H: ", fp);
-      if (fwrite (buf, 1, buflen, fp) != (size_t) buflen)
-        /* FIXME */ return;
-      return;
-    }
-
- fputs (writing ? "C: " : "S: ", fp);
- if (writing && g_ascii_strncasecmp(buf, "auth plain", 10) == 0) {
-     fputs("AUTH (details hidden)\n", fp);
-     return;
- }
- if (fwrite (buf, 1, buflen, fp) != (size_t) buflen)
-   /* FIXME */ return;
- if (buf[buflen - 1] != '\n')
-   putc ('\n', fp);
-}
-
 /* balsa_send_message_real:
    does the actual message sending. 
    This function may be called as a thread and should therefore do
@@ -1339,9 +768,6 @@ monitor_cb (const char *buf, int buflen, int writing, void *arg)
    Also, structure info should be freed before exiting.
 */
 
-/* [BCS] radically different since it uses the libESMTP interface.
- */
-
 static gboolean
 balsa_send_message_real_idle_cb(LibBalsaMailbox * outbox)
 {
@@ -1351,215 +777,103 @@ balsa_send_message_real_idle_cb(LibBalsaMailbox * outbox)
     return FALSE;
 }
 
-static guint
-balsa_send_message_real(SendMessageInfo* info)
+static gboolean
+balsa_send_message_real(SendMessageInfo *info)
 {
-    gboolean session_started;
-    SendThreadMessage *threadmsg;
-
-    /* The event callback is used to write messages to the the progress
-       dialog shown when transferring a message to the SMTP server. 
-       This callback is only used in MT build, we do not show any
-       feedback in non-MT version.
-    */
-    smtp_set_eventcb (info->session, libbalsa_smtp_event_cb, NULL);
-
-    /* Add a protocol monitor when debugging is enabled. */
-    if(info->debug)
-        smtp_set_monitorcb (info->session, monitor_cb, stderr, 1);
-
-    /* Kick off the connection with the MTA.  When this returns, all
-       messages with valid recipients have been sent. */
-    if ( !(session_started = smtp_start_session (info->session)) ){
-        char buf[256];
-        int smtp_err = smtp_errno();
-        switch(smtp_err) {
-        case -ECONNREFUSED:
-            libbalsa_information
-                (LIBBALSA_INFORMATION_ERROR,
-                 _("SMTP server refused connection.\n"
-                   "Check your internet connection."));
-        case -EHOSTUNREACH:
-            libbalsa_information
-                (LIBBALSA_INFORMATION_ERROR,
-                 _("SMTP server cannot be reached.\n"
-                   "Check your internet connection."));
-            break;
-        case SMTP_ERR_NOTHING_TO_DO: /* silence this one? */
-            break;
-        case SMTP_ERR_EAI_AGAIN:
-            /* this is really problem with DNS but it is nowadays
-               caused by general connection problems. */
-            libbalsa_information(LIBBALSA_INFORMATION_MESSAGE,
-                                 _("Message left in Outbox (try again later)"));
-            break;
-        default:
-            libbalsa_information (LIBBALSA_INFORMATION_ERROR,
-                                  _("SMTP server problem (%d): %s\n"
-                                    "Message is left in outbox."),
-                                  smtp_errno(),
-                                  smtp_strerror (smtp_errno (), 
-                                                 buf, sizeof buf));
-        }
-    } 
-    /* We give back all the resources used and delete the sent messages */
-    /* Quite a bit of status info has been gathered about messages and
-       their recipients.  The following will do a libbalsa_message_delete()
-       on the messages with a 2xx status recorded against them.  However
-       its possible for individual recipients to fail too.  Need a way to
-       report it all.  */
-    smtp_enumerate_messages (info->session, handle_successful_send, 
-                             &session_started);
+       gboolean result;
+       GError *error = NULL;
+       SendThreadMessage *threadmsg;
+       gchar *greeting = NULL;
+
+       g_debug("%s: starting", __func__);
+
+       /* connect the SMTP server */
+       result = net_client_smtp_connect(info->session, &greeting, &error);
+       g_debug("%s: connect = %d [%p]: '%s'", __func__, result, info->items, greeting);
+       if (result) {
+               GList *this_msg;
+               gchar *msg;
+
+               msg = g_strdup_printf(_("Connected to MTA %s: %s"), info->mta_name, greeting);
+               MSGSENDTHREAD(threadmsg, MSGSENDTHREADPROGRESS, msg, NULL, NULL, 0);
+               g_free(msg);
+               for (this_msg = info->items; this_msg != NULL; this_msg = this_msg->next) {
+                       MessageQueueItem *mqi = (MessageQueueItem *) this_msg->data;
+                       gboolean send_res;
+
+                       g_debug("%s: mqi = %p", __func__, mqi);
+                       /* send the message */
+                       send_res = net_client_smtp_send_msg(info->session, mqi->smtp_msg, &error);
+
+                       g_mutex_lock(&send_messages_lock);
+                       if ((mqi->orig != NULL) && (mqi->orig->mailbox != NULL)) {
+                               libbalsa_message_change_flags(mqi->orig, 0, LIBBALSA_MESSAGE_FLAG_FLAGGED);
+                       } else {
+                               g_message("mqi: %p mqi->orig: %p mqi->orig->mailbox: %p\n", mqi, mqi ? 
mqi->orig : NULL,
+                                                 mqi && mqi->orig ? mqi->orig->mailbox : NULL);
+                       }
+
+                       if (send_res) {
+                               /* sending message successful */
+                               if ((mqi->orig != NULL) && (mqi->orig->mailbox != NULL)) {
+                                       gboolean remove = TRUE;
+                                       const gchar *fccurl = libbalsa_message_get_user_header(mqi->orig, 
"X-Balsa-Fcc");
+
+                                       if (fccurl != NULL) {
+                                               LibBalsaMailbox *fccbox = mqi->finder(fccurl);
+                                               GError *err = NULL;
+
+                                               libbalsa_message_change_flags(mqi->orig, 0, 
LIBBALSA_MESSAGE_FLAG_NEW | LIBBALSA_MESSAGE_FLAG_FLAGGED);
+                                               libbalsa_mailbox_sync_storage(mqi->orig->mailbox, FALSE);
+                                               remove = libbalsa_message_copy(mqi->orig, fccbox, &err);
+                                               if (!remove) {
+                                                       libbalsa_information(LIBBALSA_INFORMATION_ERROR, 
_("Saving sent message to %s failed: %s"),
+                                                                                                fccbox->url, 
err ? err->message : "?");
+                                                       g_clear_error(&err);
+                                               }
+                                       }
+                                       /* If copy failed, mark the message again as flagged - otherwise it 
will get resent again. And again, and
+                                        * again... */
+                                       libbalsa_message_change_flags(mqi->orig,
+                                                                                                 remove ? 
LIBBALSA_MESSAGE_FLAG_DELETED : LIBBALSA_MESSAGE_FLAG_FLAGGED, 0);
+                               }
+                       } else {
+                               /* sending message failed - mark it as:
+                                *   - flagged, so it will not be sent again until the error is fixed and the 
user manually clears the flag;
+                                *   - undeleted, in case it was already deleted. */
+                               if ((mqi->orig != NULL) && (mqi->orig->mailbox != NULL)) {
+                                       libbalsa_message_change_flags(mqi->orig, 
LIBBALSA_MESSAGE_FLAG_FLAGGED, LIBBALSA_MESSAGE_FLAG_DELETED);
+                               }
+                               libbalsa_information(LIBBALSA_INFORMATION_ERROR, _("Sending message failed: 
%s\nMessage left in your outbox."),
+                                                                        error->message);
+                               g_clear_error(&error);
+                       }
+
+                       /* free data */
+                       msg_queue_item_destroy(mqi);
+                       g_mutex_unlock(&send_messages_lock);
+               }
+       } else {
+               libbalsa_information(LIBBALSA_INFORMATION_ERROR, _("Connecting MTA %s (%s) failed: %s"),
+                                                        info->mta_name, 
net_client_get_host(NET_CLIENT(info->session)), error->message);
+               g_error_free(error);
+       }
+       g_free(greeting);
 
     /* close outbox in an idle callback, as it might affect the display */
-    g_idle_add((GSourceFunc) balsa_send_message_real_idle_cb,
-               g_object_ref(info->outbox));
-    /*
-     * gdk_flush();
-     * gdk_threads_leave();
-     */
-
-    g_mutex_lock(&send_messages_lock);
-    MSGSENDTHREAD(threadmsg, MSGSENDTHREADFINISHED, "", NULL, NULL, 0);
-    sending_threads--;
-    g_mutex_unlock(&send_messages_lock);
-        
-    smtp_destroy_session (info->session);
-    send_message_info_destroy(info);   
-    /* threadmsg is not leaked: */
-    /* cppcheck-suppress memleak */
-    return TRUE;
-}
+    g_idle_add((GSourceFunc) balsa_send_message_real_idle_cb, g_object_ref(info->outbox));
 
-#else /* ESMTP */
-static void
-sendmail_add_recipients(GPtrArray *args, InternetAddressList* recipient_list)
-{
-    const InternetAddress *ia;
-    int i;
-    
-    if (recipient_list == NULL)
-       return;
-    
-    for (i = 0; i < internet_address_list_length(recipient_list); i++) {
-        ia = internet_address_list_get_address(recipient_list, i);
+       /* clean up */
+    send_message_info_destroy(info);
 
-       if (INTERNET_ADDRESS_IS_MAILBOX(ia))
-           g_ptr_array_add(args, INTERNET_ADDRESS_MAILBOX(ia)->addr);
-       else
-           sendmail_add_recipients(args, INTERNET_ADDRESS_GROUP(ia)->members);
-    }
-}
-
-/* balsa_send_message_real:
-   does the actual message sending. 
-   This function may be called as a thread and should therefore do
-   proper gdk_threads_{enter/leave} stuff around GTK,libbalsa or
-   libmutt calls. Also, structure info should be freed before exiting.
-*/
-
-static guint
-balsa_send_message_real(SendMessageInfo* info)
-{
-    MessageQueueItem *mqi, *next_message;
-#ifdef BALSA_USE_THREADS
-    SendThreadMessage *threadmsg;
-    send_lock();
-    if (!message_queue) {
+       g_mutex_lock(&send_messages_lock);
+    MSGSENDTHREAD(threadmsg, MSGSENDTHREADFINISHED, "", NULL, NULL, 0);
        sending_threads--;
-       send_unlock();
-       MSGSENDTHREAD(threadmsg, MSGSENDTHREADFINISHED, "", NULL, NULL, 0);
-       send_message_info_destroy(info);        
-       return TRUE;
-    }
-    send_unlock();
-#else
-    if(!message_queue){
-       send_message_info_destroy(info);        
-       return TRUE;
-    }  
-#endif
-
-
-    while ( (mqi = get_msg2send()) != NULL) {
-       GPtrArray *args = g_ptr_array_new();
-       LibBalsaMessage *msg = LIBBALSA_MESSAGE(mqi->orig);
-       InternetAddress *ia;
-       gchar *cmd;
-       FILE *sendmail;
-       GMimeStream *out;
-
-       g_ptr_array_add(args, SENDMAIL);
-
-        /* Determine the sender info */
-        if (msg->headers->from
-            && (ia=internet_address_list_get_address(msg->headers->from, 0))) {
-            while (ia && INTERNET_ADDRESS_IS_GROUP (ia))
-                ia = internet_address_list_get_address
-                    (INTERNET_ADDRESS_GROUP (ia)->members, 0);
-
-            if (ia) {
-                g_ptr_array_add(args, "-f");
-                g_ptr_array_add(args, INTERNET_ADDRESS_MAILBOX (ia)->addr);
-            }
-        } 
-
-       g_ptr_array_add(args, "--");
-        
-        sendmail_add_recipients(args, msg->headers->to_list);
-        sendmail_add_recipients(args, msg->headers->cc_list);
-        sendmail_add_recipients(args, msg->headers->bcc_list);
-
-       g_ptr_array_add(args, NULL);
-       cmd = g_strjoinv(" ", (gchar**)args->pdata);
-       g_ptr_array_free(args, FALSE);
-       if ( (sendmail=popen(cmd, "w")) == NULL) {
-           /* Error while sending */
-           mqi->status = MQI_FAILED;
-       } else {
-           out = g_mime_stream_file_new(sendmail);
-           g_mime_stream_file_set_owner(GMIME_STREAM_FILE(out), FALSE);
-           if (g_mime_stream_write_to_stream(mqi->stream, out) == -1)
-               mqi->status = MQI_FAILED;
-           g_object_unref(out);
-           if (pclose(sendmail) != 0)
-               mqi->status = MQI_FAILED;
-           if (mqi->status != MQI_FAILED)
-               mqi->status = MQI_SENT;
-       }
-       g_free(cmd);
-    }
-
-    /* We give back all the resources used and delete the sent messages */
-    
-    send_lock();
-    mqi = message_queue;
-    
-    while (mqi != NULL) {
-       if (mqi->status == MQI_SENT) 
-           handle_successful_send(mqi, info->finder);
-       next_message = mqi->next_message;
-       msg_queue_item_destroy(mqi);
-       mqi = next_message;
-    }
-    
-    gdk_threads_enter();
-    libbalsa_mailbox_close(info->outbox, TRUE);
-    gdk_threads_leave();
+       g_mutex_unlock(&send_messages_lock);
 
-    message_queue = NULL;
-#ifdef BALSA_USE_THREADS
-    sending_threads--;
-    MSGSENDTHREAD(threadmsg, MSGSENDTHREADFINISHED, "", NULL, NULL, 0);
-#endif
-    send_message_info_destroy(info);   
-    send_unlock();
-    return TRUE;
+       return result;
 }
 
-#endif /* ESMTP */
-
-
 static void
 message_add_references(const LibBalsaMessage * message, GMimeMessage * msg)
 {
@@ -1953,6 +1267,18 @@ libbalsa_message_postpone(LibBalsaMessage * message,
 }
 
 
+static inline gchar
+base32_char(guint8 val)
+{
+       val &= 0x1f;
+       if (val <= 25) {
+               return val + 'A';
+       } else {
+               return val + '2' - 26;
+       }
+}
+
+
 /* Create a message-id and set it on the mime message.
  */
 static void
@@ -1970,6 +1296,8 @@ libbalsa_set_message_id(GMimeMessage * mime_message)
     guint8 buffer[32];
     gsize buflen;
     gchar *message_id;
+    guint8 *src;
+    gchar *dst;
 
     g_mutex_lock(&mutex);
     if (rand == NULL) {
@@ -1994,15 +1322,28 @@ libbalsa_set_message_id(GMimeMessage * mime_message)
     g_hmac_unref(msg_id_hash);
     g_mutex_unlock(&mutex);
 
-    /* create a msg id string
-     * Note: RFC 5322, sect. 3.6.4 explicitly allows the form
-     *    dot-atom-text "@" dot-atom-text
-     * where dot-atom-text may include all base64 (RFC 1421) chars,
-     * including '+' and '/' */
-    message_id = g_base64_encode(buffer, buflen);
-    memmove(message_id + 23, message_id + 22, strlen(message_id) - 23);
-    message_id[22] = '@';
-
+    /* create a msg id string as base32-encoded string from the first
+     * 30 bytes of the hashed result, and separate the groups by '.'
+     * or '@' */
+    message_id = dst = g_malloc0(54U); /* = (32 / 5) * 9 */
+    src = buffer;
+       while (buflen >= 5U) {
+               *dst++ = base32_char(src[0] >> 3);
+               *dst++ = base32_char((src[0] << 2) + (src[1] >> 6));
+               *dst++ = base32_char(src[1] >> 1);
+               *dst++ = base32_char((src[1] << 4) + (src[2] >> 4));
+               *dst++ = base32_char((src[2] << 1) + (src[3] >> 7));
+               *dst++ = base32_char(src[3] >> 2);
+               *dst++ = base32_char((src[3] << 3) + (src[4] >> 5));
+               *dst++ = base32_char(src[4]);
+               src = &src[5];
+               buflen -= 5U;
+               if (dst == &message_id[(54U / 2U) - 1U]) {
+                       *dst++ = '@';
+               } else if (buflen >= 5U) {
+                       *dst++ = '.';
+               }
+       }
     g_mime_message_set_message_id(mime_message, message_id);
     g_free(message_id);
 }
@@ -2027,34 +1368,50 @@ libbalsa_create_msg(LibBalsaMessage * message, gboolean flow, GError ** error)
 }
 
 static LibBalsaMsgCreateResult
-libbalsa_fill_msg_queue_item_from_queu(LibBalsaMessage * message,
-                                       MessageQueueItem *mqi)
+libbalsa_fill_msg_queue_item_from_queu(LibBalsaMessage * message, MessageQueueItem *mqi)
 {
-    mqi->orig = message;
-    g_object_ref(mqi->orig);
-    if (message->mime_msg) {
-        g_mime_object_remove_header(GMIME_OBJECT(message->mime_msg),
-                                    "Status");
-        g_mime_object_remove_header(GMIME_OBJECT(message->mime_msg),
-                                    "X-Status");
-        g_mime_object_remove_header(GMIME_OBJECT(message->mime_msg),
-                                    "X-Balsa-Fcc");
-        g_mime_object_remove_header(GMIME_OBJECT(message->mime_msg),
-                                    "X-Balsa-SmtpServer");
-        g_mime_object_remove_header(GMIME_OBJECT(message->mime_msg),
-                                    "X-Balsa-DSN");
-       mqi->stream = g_mime_stream_mem_new();
-        libbalsa_mailbox_lock_store(message->mailbox);
-       g_mime_object_write_to_stream(GMIME_OBJECT(message->mime_msg),
-                                      mqi->stream);
-        libbalsa_mailbox_unlock_store(message->mailbox);
-       g_mime_stream_reset(mqi->stream);
-    } else
-       mqi->stream = libbalsa_message_stream(message);
-    if (mqi->stream == NULL)
-       return LIBBALSA_MESSAGE_CREATE_ERROR;
-  
-    return LIBBALSA_MESSAGE_CREATE_OK;
+       GMimeStream *msg_stream;
+       LibBalsaMsgCreateResult result = LIBBALSA_MESSAGE_CREATE_ERROR;
+
+       mqi->orig = message;
+       if (message->mime_msg != NULL) {
+               msg_stream = g_mime_stream_mem_new();
+               libbalsa_mailbox_lock_store(message->mailbox);
+               g_mime_object_write_to_stream(GMIME_OBJECT(message->mime_msg), msg_stream);
+               libbalsa_mailbox_unlock_store(message->mailbox);
+               g_mime_stream_reset(msg_stream);
+       } else {
+               msg_stream = libbalsa_message_stream(message);
+       }
+
+       if (msg_stream != NULL) {
+               GMimeStream *filter_stream;
+               GMimeFilter *filter;
+
+               filter_stream = g_mime_stream_filter_new(msg_stream);
+
+               /* filter out unwanted headers */
+               filter = g_mime_filter_header_new();
+               g_mime_stream_filter_add(GMIME_STREAM_FILTER(filter_stream), filter);
+               g_object_unref(G_OBJECT(filter));
+
+               /* add CRLF, encode dot */
+               filter = g_mime_filter_crlf_new(TRUE, TRUE);
+               g_mime_stream_filter_add(GMIME_STREAM_FILTER(filter_stream), filter);
+               g_object_unref(G_OBJECT(filter));
+
+               /* write to a new stream */
+               mqi->stream = g_mime_stream_mem_new();
+               g_mime_stream_write_to_stream(filter_stream, mqi->stream);
+               g_object_unref(G_OBJECT(filter_stream));
+               g_mime_stream_reset(mqi->stream);
+               g_object_unref(G_OBJECT(msg_stream));
+
+               g_object_ref(G_OBJECT(mqi->orig));
+               result = LIBBALSA_MESSAGE_CREATE_OK;
+       }
+
+       return result;
 }
 
 
diff --git a/libbalsa/send.h b/libbalsa/send.h
index a6f383a..c7ce3dd 100644
--- a/libbalsa/send.h
+++ b/libbalsa/send.h
@@ -50,9 +50,6 @@ gboolean libbalsa_message_postpone(LibBalsaMessage * message,
                                   gboolean flow, 
                                   GError **error);
 
-
-#if ENABLE_ESMTP
-
 LibBalsaMsgCreateResult libbalsa_message_queue(LibBalsaMessage* message, 
                                               LibBalsaMailbox* outbox,
                                                LibBalsaMailbox* fccbox,
@@ -75,24 +72,6 @@ gboolean libbalsa_process_queue(LibBalsaMailbox * outbox,
                                 GSList * smtp_servers,
                                 GtkWindow * parent,
                                 gboolean debug);
-#else
-
-LibBalsaMsgCreateResult libbalsa_message_queue(LibBalsaMessage* message, 
-                                              LibBalsaMailbox* outbox,
-                                               LibBalsaMailbox* fccbox,
-                                              gboolean flow,
-                                              GError ** error);
-LibBalsaMsgCreateResult libbalsa_message_send(LibBalsaMessage* message,
-                                             LibBalsaMailbox* outbox,  
-                                             LibBalsaMailbox* fccbox,
-                                              LibBalsaFccboxFinder finder, 
-                                             gboolean flow, gboolean debug,
-                                             GError ** error);
-gboolean libbalsa_process_queue(LibBalsaMailbox* outbox, 
-                                LibBalsaFccboxFinder finder,
-                                gboolean debug);
-
-#endif
 
 extern GMutex send_messages_lock;
 extern int send_thread_pipes[2];
diff --git a/libbalsa/server.c b/libbalsa/server.c
index f8dc278..132c75c 100644
--- a/libbalsa/server.c
+++ b/libbalsa/server.c
@@ -182,6 +182,7 @@ libbalsa_server_init(LibBalsaServer * server)
     server->remember_passwd = TRUE;
     server->use_ssl         = FALSE;
     server->tls_mode        = LIBBALSA_TLS_ENABLED;
+    server->security           = NET_CLIENT_CRYPT_STARTTLS;
 }
 
 /* leave object in sane state (NULLified fields) */
@@ -324,9 +325,14 @@ libbalsa_server_load_config(LibBalsaServer * server)
         }
     }       
     server->use_ssl = libbalsa_conf_get_bool("SSL=false");
-    d=0;
+    d = FALSE;
     server->tls_mode = libbalsa_conf_get_int_with_default("TLSMode", &d);
     if(d) server->tls_mode = LIBBALSA_TLS_ENABLED;
+    d = FALSE;
+    server->security = libbalsa_conf_get_int_with_default("Security", &d);
+    if (d) {
+       server->security = NET_CLIENT_CRYPT_STARTTLS;
+    }
     server->user = libbalsa_conf_private_get_string("Username");
     if (!server->user)
        server->user = g_strdup(getenv("USER"));
@@ -466,6 +472,7 @@ libbalsa_server_save_config(LibBalsaServer * server)
     }
     libbalsa_conf_set_bool("SSL", server->use_ssl);
     libbalsa_conf_set_int("TLSMode", server->tls_mode);
+    libbalsa_conf_set_int("Security", server->security);
 }
 
 void
diff --git a/libbalsa/server.h b/libbalsa/server.h
index 32fde52..81090e1 100644
--- a/libbalsa/server.h
+++ b/libbalsa/server.h
@@ -27,6 +27,7 @@
 
 #include "imap/libimap.h"
 #include "libbalsa.h"
+#include "net-client.h"
 
 #if defined(HAVE_LIBSECRET)
 #include <libsecret/secret.h>
@@ -75,6 +76,7 @@ struct _LibBalsaServer {
     gchar *host;
     gchar *user;
     gchar *passwd;
+    NetClientCryptMode security;
     /* We include SSL support in UI unconditionally to preserve config
      * between SSL and non-SSL builds. We just fail if SSL is requested
      * in non-SSL build. */
diff --git a/libbalsa/smtp-server.c b/libbalsa/smtp-server.c
index 04bcf10..c91e81a 100644
--- a/libbalsa/smtp-server.c
+++ b/libbalsa/smtp-server.c
@@ -21,7 +21,6 @@
 # include "config.h"
 #endif                          /* HAVE_CONFIG_H */
 
-#if ENABLE_ESMTP
 /*
  * LibBalsaSmtpServer is a subclass of LibBalsaServer.
  */
@@ -34,7 +33,7 @@
 #include "libbalsa-conf.h"
 #include "misc.h"
 #include <glib/gi18n.h>
-#include <libesmtp.h>
+#include "net-client.h"
 
 #if HAVE_MACOSX_DESKTOP
 #  include "macosx-helpers.h"
@@ -46,10 +45,7 @@ struct _LibBalsaSmtpServer {
     LibBalsaServer server;
 
     gchar *name;
-    auth_context_t authctx;
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
     gchar *cert_passphrase;
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
     guint big_message; /* size of partial messages; in kB */
 };
 
@@ -70,11 +66,8 @@ libbalsa_smtp_server_finalize(GObject * object)
 
     smtp_server = LIBBALSA_SMTP_SERVER(object);
 
-    auth_destroy_context(smtp_server->authctx);
     g_free(smtp_server->name);
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
     g_free(smtp_server->cert_passphrase);
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
 
     G_OBJECT_CLASS(parent_class)->finalize(object);
 }
@@ -91,70 +84,10 @@ libbalsa_smtp_server_class_init(LibBalsaSmtpServerClass * klass)
     object_class->finalize = libbalsa_smtp_server_finalize;
 }
 
-/* Callback to get user/password info from SMTP server preferences.
-   This is adequate for simple username / password requests but does
-   not adequately cope with all SASL mechanisms.  */
-static int
-authinteract(auth_client_request_t request, char **result, int fields,
-             void *arg)
-{
-    LibBalsaServer *server = LIBBALSA_SERVER(arg);
-    int i;
-
-    for (i = 0; i < fields; i++) {
-        if (request[i].flags & AUTH_PASS) {
-           /* We need to return a const pointer, this is why we
-              ignore the result from get_password, and take and
-              advantage of the fact that this function sets the
-              passwd field of the server. */
-           if(!server->passwd)
-               g_free(libbalsa_server_get_password(server, NULL));
-            result[i] = server->passwd;
-       }
-        else if (request[i].flags & AUTH_USER)
-            result[i] = (server->user
-                         && *server->user) ? server->user : NULL;
-
-        /* Fail the AUTH exchange if something was requested
-           but not supplied. */
-        if (result[i] == NULL)
-            return 0;
-    }
-
-    return 1;
-}
-
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
-static int
-tlsinteract(char *buf, int buflen, int rwflag, void *arg)
-{
-    LibBalsaSmtpServer *smtp_server = LIBBALSA_SMTP_SERVER(arg);
-    char *pw;
-    int len;
-
-    pw = smtp_server->cert_passphrase;
-    len = strlen(pw);
-    if (len + 1 > buflen)
-        return 0;
-    strcpy(buf, pw);
-    return len;
-}
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
-
 static void
 libbalsa_smtp_server_init(LibBalsaSmtpServer * smtp_server)
 {
     LIBBALSA_SERVER(smtp_server)->protocol = "smtp";
-    smtp_server->authctx = auth_create_context();
-    auth_set_mechanism_flags(smtp_server->authctx, AUTH_PLUGIN_PLAIN, 0);
-    auth_set_interact_cb(smtp_server->authctx, authinteract, smtp_server);
-
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
-    /* Use our callback for X.509 certificate passwords.  If STARTTLS is
-       not in use or disabled in configure, the following is harmless. */
-    smtp_server->cert_passphrase = NULL;
-    smtp_starttls_set_password_cb(tlsinteract, smtp_server);
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
 }
 
 /* Class boilerplate */
@@ -219,7 +152,6 @@ libbalsa_smtp_server_new_from_config(const gchar * name)
 
     libbalsa_server_load_config(LIBBALSA_SERVER(smtp_server));
 
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
     smtp_server->cert_passphrase =
         libbalsa_conf_private_get_string("CertificatePassphrase");
     if (smtp_server->cert_passphrase) {
@@ -227,7 +159,6 @@ libbalsa_smtp_server_new_from_config(const gchar * name)
         g_free(smtp_server->cert_passphrase);
         smtp_server->cert_passphrase = tmp;
     }
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
 
     smtp_server->big_message = libbalsa_conf_get_int("BigMessage=0");
 
@@ -239,13 +170,11 @@ libbalsa_smtp_server_save_config(LibBalsaSmtpServer * smtp_server)
 {
     libbalsa_server_save_config(LIBBALSA_SERVER(smtp_server));
 
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
     if (smtp_server->cert_passphrase) {
         gchar *tmp = libbalsa_rot(smtp_server->cert_passphrase);
         libbalsa_conf_private_set_string("CertificatePassphrase", tmp);
         g_free(tmp);
     }
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
     libbalsa_conf_set_int("BigMessage", smtp_server->big_message);
 }
 
@@ -263,7 +192,6 @@ libbalsa_smtp_server_get_name(LibBalsaSmtpServer * smtp_server)
     return smtp_server ? smtp_server->name : _("Default");
 }
 
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
 void
 libbalsa_smtp_server_set_cert_passphrase(LibBalsaSmtpServer * smtp_server,
                                          const gchar * passphrase)
@@ -277,13 +205,6 @@ libbalsa_smtp_server_get_cert_passphrase(LibBalsaSmtpServer * smtp_server)
 {
     return smtp_server->cert_passphrase;
 }
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
-
-auth_context_t
-libbalsa_smtp_server_get_authctx(LibBalsaSmtpServer * smtp_server)
-{
-    return smtp_server->authctx;
-}
 
 guint
 libbalsa_smtp_server_get_big_message(LibBalsaSmtpServer * smtp_server)
@@ -333,10 +254,8 @@ struct smtp_server_dialog_info {
     GtkWidget *host;
     GtkWidget *user;
     GtkWidget *pass;
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
     GtkWidget *tlsm;
     GtkWidget *cert;
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
     GtkWidget *split_button;
     GtkWidget *big_message;
 };
@@ -369,7 +288,6 @@ smtp_server_add_widget(GtkWidget * grid, gint row, const gchar * text,
     gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
 }
 
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
 static
 GtkWidget *
 smtp_server_tls_widget(LibBalsaSmtpServer * smtp_server)
@@ -377,30 +295,15 @@ smtp_server_tls_widget(LibBalsaSmtpServer * smtp_server)
     LibBalsaServer *server = LIBBALSA_SERVER(smtp_server);
     GtkWidget *combo_box = gtk_combo_box_text_new();
 
-    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box),
-                                   _("Never"));
-    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box),
-                                   _("If Possible"));
-    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box),
-                                   _("Required"));
+    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), _("SMTP over SSL (SMTPS)"));
+    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), _("TLS required"));
+    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), _("TLS if possible (not recommended)"));
+    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), _("None (not recommended)"));
 
-    switch (server->tls_mode) {
-    case Starttls_DISABLED:
-        gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0);
-        break;
-    case Starttls_ENABLED:
-        gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 1);
-        break;
-    case Starttls_REQUIRED:
-        gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 2);
-        break;
-    default:
-        break;
-    }
+    gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), (gint) server->security - 1);
 
     return combo_box;
 }
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
 
 static void
 smtp_server_response(GtkDialog * dialog, gint response,
@@ -436,24 +339,10 @@ smtp_server_response(GtkDialog * dialog, gint response,
         libbalsa_server_set_password(server,
                                      gtk_entry_get_text(GTK_ENTRY
                                                         (sdi->pass)));
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
-        switch (gtk_combo_box_get_active(GTK_COMBO_BOX(sdi->tlsm))) {
-        case 0:
-            server->tls_mode = LIBBALSA_TLS_DISABLED;
-            break;
-        case 1:
-            server->tls_mode = LIBBALSA_TLS_ENABLED;
-            break;
-        case 2:
-            server->tls_mode = LIBBALSA_TLS_REQUIRED;
-            break;
-        default:
-            break;
-        }
+        server->security = (NetClientCryptMode) (gtk_combo_box_get_active(GTK_COMBO_BOX(sdi->tlsm)) + 1);
         libbalsa_smtp_server_set_cert_passphrase(sdi->smtp_server,
                                                  gtk_entry_get_text
                                                  (GTK_ENTRY(sdi->cert)));
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
         if (gtk_toggle_button_get_active
             (GTK_TOGGLE_BUTTON(sdi->split_button)))
             /* big_message is stored in kB, but the widget is in MB. */
@@ -590,8 +479,7 @@ libbalsa_smtp_server_dialog(LibBalsaSmtpServer * smtp_server,
     g_signal_connect(sdi->pass, "changed", G_CALLBACK(smtp_server_changed),
                      sdi);
 
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
-    smtp_server_add_widget(grid, ++row, _("Use _TLS:"), sdi->tlsm =
+    smtp_server_add_widget(grid, ++row, _("Se_curity:"), sdi->tlsm =
                            smtp_server_tls_widget(smtp_server));
     g_signal_connect(sdi->tlsm, "changed", G_CALLBACK(smtp_server_changed),
                      sdi);
@@ -604,7 +492,6 @@ libbalsa_smtp_server_dialog(LibBalsaSmtpServer * smtp_server,
                            smtp_server->cert_passphrase);
     g_signal_connect(sdi->cert, "changed", G_CALLBACK(smtp_server_changed),
                      sdi);
-#endif                          /* HAVE_SMTP_TLS_CLIENT_CERTIFICATE */
 
     ++row;
     sdi->split_button =
@@ -636,5 +523,3 @@ libbalsa_smtp_server_dialog(LibBalsaSmtpServer * smtp_server,
 
     gtk_widget_show_all(dialog);
 }
-
-#endif                          /* ENABLE_ESMTP */
diff --git a/libbalsa/smtp-server.h b/libbalsa/smtp-server.h
index 24ba219..3f757f3 100644
--- a/libbalsa/smtp-server.h
+++ b/libbalsa/smtp-server.h
@@ -17,12 +17,10 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
-#if ENABLE_ESMTP
 #ifndef __SMTP_SERVER_H__
 #define __SMTP_SERVER_H__
 
 #include <gtk/gtk.h>
-#include <auth-client.h>
 #include "libbalsa.h"
 
 #define LIBBALSA_TYPE_SMTP_SERVER                              \
@@ -53,8 +51,6 @@ void libbalsa_smtp_server_set_cert_passphrase(LibBalsaSmtpServer *
                                               const gchar * passphrase);
 const gchar *libbalsa_smtp_server_get_cert_passphrase(LibBalsaSmtpServer *
                                                       smtp_server);
-auth_context_t libbalsa_smtp_server_get_authctx(LibBalsaSmtpServer *
-                                                smtp_server);
 guint libbalsa_smtp_server_get_big_message(LibBalsaSmtpServer *
                                            smtp_server);
 void libbalsa_smtp_server_add_to_list(LibBalsaSmtpServer * smtp_server,
@@ -68,4 +64,3 @@ void libbalsa_smtp_server_dialog(LibBalsaSmtpServer * smtp_server,
                                  LibBalsaSmtpServerUpdate update);
 
 #endif                          /* __SMTP_SERVER_H__ */
-#endif                          /* ENABLE_ESMTP */
diff --git a/libinit_balsa/Makefile.am b/libinit_balsa/Makefile.am
index aa5a1ce..6bdd6d4 100644
--- a/libinit_balsa/Makefile.am
+++ b/libinit_balsa/Makefile.am
@@ -18,6 +18,7 @@ libinit_balsa_a_SOURCES =             \
 
 AM_CPPFLAGS = -I$(top_builddir) -I$(top_srcdir) \
        -I$(top_srcdir)/libbalsa \
+       -I${top_srcdir}/libnetclient \
        -I$(top_srcdir)/src \
         $(BALSA_CFLAGS)
 
diff --git a/libinit_balsa/assistant_page_user.c b/libinit_balsa/assistant_page_user.c
index 0526f70..d987d55 100644
--- a/libinit_balsa/assistant_page_user.c
+++ b/libinit_balsa/assistant_page_user.c
@@ -76,9 +76,7 @@ balsa_druid_page_user_init(BalsaDruidPageUser * user,
     user->emaster.donemask = 0;
     user->ed0.master = &(user->emaster);
     user->ed1.master = &(user->emaster);
-#if ENABLE_ESMTP
     user->ed2.master = &(user->emaster);
-#endif
     user->ed3.master = &(user->emaster);
     user->ed4.master = &(user->emaster);
     label = GTK_LABEL(gtk_label_new(_(header2)));
@@ -119,11 +117,9 @@ balsa_druid_page_user_init(BalsaDruidPageUser * user,
     gtk_entry_set_visibility(GTK_ENTRY(user->passwd), FALSE);
     /* separator line here */
 
-#if ENABLE_ESMTP
     preset = "localhost:25";
     balsa_init_add_grid_entry(grid, row++, _("_SMTP Server:"), preset,
                                &(user->ed2), druid, page, &(user->smtp));
-#endif
 
     /* 2.1 */
     balsa_init_add_grid_entry(grid, row++, _("Your real _name:"),
@@ -242,9 +238,7 @@ balsa_druid_page_user_next(GtkAssistant * druid, GtkWidget * page,
     gchar *uhoh;
     LibBalsaIdentity *ident;
     InternetAddress *ia;
-#if ENABLE_ESMTP
     LibBalsaSmtpServer *smtp_server;
-#endif /* ENABLE_ESMTP */
     
 #if 0
     printf("USER next ENTER %p %p\n", page, user->page);
@@ -294,7 +288,6 @@ balsa_druid_page_user_next(GtkAssistant * druid, GtkWidget * page,
     libbalsa_identity_set_address (ident, ia);
 
     /* outgoing mail */
-#if ENABLE_ESMTP
     if (balsa_app.smtp_servers == NULL) {
        smtp_server = libbalsa_smtp_server_new();
         libbalsa_smtp_server_set_name(smtp_server,
@@ -306,7 +299,6 @@ balsa_druid_page_user_next(GtkAssistant * druid, GtkWidget * page,
     libbalsa_server_set_host(LIBBALSA_SERVER(smtp_server),
                              gtk_entry_get_text(GTK_ENTRY(user->smtp)),
                              FALSE);
-#endif
 
     g_free(balsa_app.local_mail_directory);
     balsa_app.local_mail_directory =
diff --git a/libinit_balsa/assistant_page_user.h b/libinit_balsa/assistant_page_user.h
index d864c13..0e0ed02 100644
--- a/libinit_balsa/assistant_page_user.h
+++ b/libinit_balsa/assistant_page_user.h
@@ -42,9 +42,7 @@ G_BEGIN_DECLS
         GtkWidget *login;
         GtkWidget *passwd;
         GtkWidget *remember_passwd;
-#if ENABLE_ESMTP
         GtkWidget *smtp;
-#endif
         GtkWidget *name;
         GtkWidget *email;
         GtkWidget *localmaildir;
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 2d6836b..add337b 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -62,6 +62,8 @@ libinit_balsa/assistant_page_user.c
 libinit_balsa/assistant_page_user.c
 libinit_balsa/assistant_page_welcome.c
 libinit_balsa/assistant_page_welcome.c
+libnetclient/net-client-smtp.c
+libnetclient/net-client.c
 sounds/balsa.soundlist.in
 src/ab-main.c
 src/ab-window.c
diff --git a/src/Makefile.am b/src/Makefile.am
index a4390cf..8e2df58 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -112,6 +112,7 @@ AM_CPPFLAGS = \
        -DGNOMELOCALEDIR=\""$(datadir)/locale"\" \
        -I$(top_builddir) -I$(top_srcdir) \
        -I$(top_srcdir)/libbalsa \
+       -I${top_srcdir}/libnetclient \
        $(BALSA_DEFS)
 
 AM_CFLAGS = $(BALSA_CFLAGS)
@@ -120,6 +121,7 @@ balsa_LDADD = \
        $(top_builddir)/libinit_balsa/libinit_balsa.a \
        $(top_builddir)/libbalsa/libbalsa.a     \
        $(top_builddir)/libbalsa/imap/libimap.a \
+       $(top_builddir)/libnetclient/libnetclient.a \
        $(balsa_IDL_OBJS) \
        $(INTLLIBS) \
         $(BALSA_LIBS)
diff --git a/src/balsa-app.c b/src/balsa-app.c
index 3f68461..2c9f3ea 100644
--- a/src/balsa-app.c
+++ b/src/balsa-app.c
@@ -251,17 +251,6 @@ ask_password(LibBalsaServer *server, LibBalsaMailbox *mbox)
        return password;
 }
 
-#if ENABLE_ESMTP
-static void
-authapi_exit (void)
-{
-    g_slist_foreach(balsa_app.smtp_servers, (GFunc) g_object_unref, NULL);
-    g_slist_free(balsa_app.smtp_servers);
-    balsa_app.smtp_servers = NULL;
-    auth_client_exit ();
-}
-#endif /* ESMTP */
-
 void
 balsa_app_init(void)
 {
@@ -272,16 +261,7 @@ balsa_app_init(void)
     balsa_app.identities = NULL;
     balsa_app.current_ident = NULL;
     balsa_app.local_mail_directory = NULL;
-
-#if ENABLE_ESMTP
     balsa_app.smtp_servers = NULL;
-
-    /* Do what's needed at application level to allow libESMTP
-       to use authentication.  */
-    auth_client_init ();
-    atexit (authapi_exit);
-#endif
-
     balsa_app.inbox = NULL;
     balsa_app.inbox_input = NULL;
     balsa_app.outbox = NULL;
diff --git a/src/balsa-app.h b/src/balsa-app.h
index 004498a..6afe16a 100644
--- a/src/balsa-app.h
+++ b/src/balsa-app.h
@@ -34,11 +34,6 @@
 /* misc.h for LibBalsaCodeset */
 #include "misc.h"
 
-#if ENABLE_ESMTP
-#include <libesmtp.h>                  /* part of libESMTP */
-#include <auth-client.h>               /* part of libESMTP */
-#endif
-
 /* Work around nonprivileged installs so we can find icons */
 #ifdef BALSA_LOCAL_INSTALL
 #define gnome_pixmap_file( s ) g_strconcat( BALSA_RESOURCE_PREFIX, "/pixmaps/", s, NULL ) 
@@ -144,9 +139,7 @@ extern struct BalsaApplication {
 
     gchar *local_mail_directory;
 
-#if ENABLE_ESMTP
     GSList *smtp_servers;
-#endif
 
     /* folder scanning */
     guint local_scan_depth;
diff --git a/src/balsa-message.c b/src/balsa-message.c
index 3c2af39..2661807 100644
--- a/src/balsa-message.c
+++ b/src/balsa-message.c
@@ -2472,18 +2472,11 @@ handle_mdn_request(GtkWindow *parent, LibBalsaMessage *message)
        GError * error = NULL;
        LibBalsaMsgCreateResult result;
 
-#if ENABLE_ESMTP
         result = libbalsa_message_send(mdn, balsa_app.outbox, NULL,
                                       balsa_find_sentbox_by_url,
                                       mdn_ident->smtp_server,
                                        parent,
                                       TRUE, balsa_app.debug, &error);
-#else
-        result = libbalsa_message_send(mdn, balsa_app.outbox, NULL,
-                                      balsa_find_sentbox_by_url,
-                                       parent,
-                                      TRUE, balsa_app.debug, &error);
-#endif
        if (result != LIBBALSA_MESSAGE_CREATE_OK)
            libbalsa_information(LIBBALSA_INFORMATION_ERROR,
                                 _("Sending the disposition notification failed: %s"),
@@ -2618,7 +2611,6 @@ mdn_dialog_response(GtkWidget * dialog, gint response, gpointer user_data)
     g_return_if_fail(mdn_ident != NULL);
 
     if (response == GTK_RESPONSE_YES) {
-#if ENABLE_ESMTP
         result =
             libbalsa_message_send(send_msg, balsa_app.outbox, NULL,
                                   balsa_find_sentbox_by_url,
@@ -2626,13 +2618,6 @@ mdn_dialog_response(GtkWidget * dialog, gint response, gpointer user_data)
                                   gtk_window_get_transient_for
                                   ((GtkWindow *) dialog),
                                   TRUE, balsa_app.debug, &error);
-#else
-        result = libbalsa_message_send(send_msg, balsa_app.outbox, NULL,
-                                      balsa_find_sentbox_by_url,
-                                       gtk_window_get_transient_for
-                                       ((GtkWindow *) dialog),
-                                      TRUE, balsa_app.debug, &error);
-#endif
         if (result != LIBBALSA_MESSAGE_CREATE_OK)
             libbalsa_information(LIBBALSA_INFORMATION_ERROR,
                                  _("Sending the disposition notification failed: %s"),
diff --git a/src/balsa-mime-widget-message.c b/src/balsa-mime-widget-message.c
index d423815..5eaea9b 100644
--- a/src/balsa-mime-widget-message.c
+++ b/src/balsa-mime-widget-message.c
@@ -363,18 +363,12 @@ extbody_send_mail(GtkWidget * button, LibBalsaMessageBody * mime_body)
     else
        body->charset = g_strdup("US-ASCII");
     libbalsa_message_append_part(message, body);
-#if ENABLE_ESMTP
     result = libbalsa_message_send(message, balsa_app.outbox, NULL,
                                   balsa_find_sentbox_by_url,
                                   balsa_app.current_ident->smtp_server,
                                    GTK_WINDOW(gtk_widget_get_toplevel
                                               (button)),
                                   FALSE, balsa_app.debug, &err);
-#else
-    result = libbalsa_message_send(message, balsa_app.outbox, NULL,
-                                  balsa_find_sentbox_by_url,
-                                  FALSE, balsa_app.debug, &err);
-#endif
     if (result != LIBBALSA_MESSAGE_CREATE_OK)
        libbalsa_information(LIBBALSA_INFORMATION_ERROR,
                             _("Sending the external body request failed: %s"),
diff --git a/src/balsa-mime-widget-vcalendar.c b/src/balsa-mime-widget-vcalendar.c
index 2c05f09..e0b651f 100644
--- a/src/balsa-mime-widget-vcalendar.c
+++ b/src/balsa-mime-widget-vcalendar.c
@@ -322,20 +322,12 @@ vevent_reply(GObject * button, GtkWidget * box)
     params[2] = NULL;
     message->parameters = g_list_prepend(message->parameters, params);
 
-#if ENABLE_ESMTP
     result = libbalsa_message_send(message, balsa_app.outbox, NULL,
                                   balsa_find_sentbox_by_url,
                                   ident->smtp_server,
                                    GTK_WINDOW(gtk_widget_get_toplevel
                                               ((GtkWidget *) button)),
                                   FALSE, balsa_app.debug, &error);
-#else
-    result = libbalsa_message_send(message, balsa_app.outbox, NULL,
-                                  balsa_find_sentbox_by_url,
-                                   GTK_WINDOW(gtk_widget_get_toplevel
-                                              ((GtkWidget *) button)),
-                                  FALSE, balsa_app.debug, &error);
-#endif
     if (result != LIBBALSA_MESSAGE_CREATE_OK)
        libbalsa_information(LIBBALSA_INFORMATION_ERROR,
                             _("Sending the iTIP calendar reply failed: %s"),
diff --git a/src/main-window.c b/src/main-window.c
index 8a6bd7d..a950ad1 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -897,9 +897,7 @@ identities_activated(GSimpleAction * action,
     libbalsa_identity_config_dialog(window,
                                     &balsa_app.identities,
                                     &balsa_app.current_ident,
-#if ENABLE_ESMTP
                                     balsa_app.smtp_servers,
-#endif /* ENABLE_ESMTP */
                                     (void(*)(gpointer))
                                     balsa_identities_changed);
 }
@@ -1046,9 +1044,7 @@ send_queued_mail_activated(GSimpleAction * action,
                            gpointer        user_data)
 {
     libbalsa_process_queue(balsa_app.outbox, balsa_find_sentbox_by_url,
-#if ENABLE_ESMTP
                            balsa_app.smtp_servers,
-#endif /* ENABLE_ESMTP */
                            (GtkWindow *) balsa_app.main_window,
                           balsa_app.debug);
 }
diff --git a/src/pref-manager.c b/src/pref-manager.c
index d0833f3..3e5731d 100644
--- a/src/pref-manager.c
+++ b/src/pref-manager.c
@@ -39,12 +39,8 @@
 #  include "macosx-helpers.h"
 #endif
 
-#if ENABLE_ESMTP
-#include <libesmtp.h>
-#include <string.h>
 #include "smtp-server.h"
 #include "libbalsa-conf.h"
-#endif                          /* ENABLE_ESMTP */
 
 #include <glib/gi18n.h>
 
@@ -74,14 +70,10 @@ typedef struct _PropertyUI {
     GtkWidget *address_books;
 
     GtkWidget *mail_servers;
-#if ENABLE_ESMTP
     GtkWidget *smtp_servers;
     GtkWidget *smtp_server_edit_button;
     GtkWidget *smtp_server_del_button;
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
     GtkWidget *smtp_certificate_passphrase;
-#endif
-#endif                          /* ENABLE_ESMTP */
     GtkWidget *mail_directory;
     GtkWidget *encoding_menu;
     GtkWidget *check_mail_auto;
@@ -200,9 +192,7 @@ static GtkWidget *mailserver_subpage(void);
 
 static GtkWidget *remote_mailbox_servers_group(GtkWidget * page);
 static GtkWidget *local_mail_group(GtkWidget * page);
-#if ENABLE_ESMTP
 static GtkWidget *outgoing_mail_group(GtkWidget * page);
-#endif                          /* ENABLE_ESMTP */
 
 static GtkWidget *incoming_subpage(void);
 
@@ -326,11 +316,9 @@ static void balsa_help_pbox_display(void);
 static void set_prefs(void);
 static void apply_prefs(GtkDialog * dialog);
 void update_mail_servers(void); /* public; in pref-manager.h */
-#if ENABLE_ESMTP
 static void smtp_server_update(LibBalsaSmtpServer *, GtkResponseType,
                               const gchar *);
 static void update_smtp_servers(void);
-#endif                          /* ENABLE_ESMTP */
 
     /* callbacks */
 static void response_cb(GtkDialog * dialog, gint response, gpointer data);
@@ -343,13 +331,11 @@ static void pop3_add_cb(void);
 static void add_menu_cb(GtkWidget * menu, GtkWidget * widget);
 static void server_del_cb(GtkTreeView * tree_view);
 
-#if ENABLE_ESMTP
 static void smtp_server_edit_cb(GtkTreeView * tree_view);
 static void smtp_server_add_cb(void);
 static void smtp_server_del_cb(GtkTreeView * tree_view);
 static void smtp_server_changed (GtkTreeSelection * selection,
                                 gpointer user_data);
-#endif                          /* ENABLE_ESMTP */
 
 static void address_book_edit_cb(GtkTreeView * tree_view);
 static void address_book_delete_cb(GtkTreeView * tree_view);
@@ -1628,9 +1614,7 @@ mailserver_subpage()
 
     pm_page_add(page, remote_mailbox_servers_group(page), TRUE);
     pm_page_add(page, local_mail_group(page), FALSE);
-#if ENABLE_ESMTP
     pm_page_add(page, outgoing_mail_group(page), TRUE);
-#endif                          /* ENABLE_ESMTP */
 
     return page;
 }
@@ -1722,7 +1706,6 @@ local_mail_group(GtkWidget * page)
     return group;
 }
 
-#if ENABLE_ESMTP
 static GtkWidget *
 outgoing_mail_group(GtkWidget * page)
 {
@@ -1785,7 +1768,6 @@ outgoing_mail_group(GtkWidget * page)
 
     return group;
 }
-#endif                          /* ENABLE_ESMTP */
 
 static GtkWidget *
 create_mail_options_page(GtkTreeStore * store)
@@ -2917,9 +2899,6 @@ server_edit_cb(GtkTreeView * tree_view)
     balsa_mailbox_node_show_prop_dialog(mbnode);
 }
 
-#if ENABLE_ESMTP
-/* SMTP server callbacks */
-
 /* Clear and populate the list. */
 static void
 update_smtp_servers(void)
@@ -3072,7 +3051,6 @@ smtp_server_changed(GtkTreeSelection * selection, gpointer user_data)
                              && gtk_tree_model_iter_n_children(model,
                                                                NULL) > 1);
 }
-#endif                          /* ENABLE_ESMTP */
 
 /* Address book callbacks */
 
diff --git a/src/save-restore.c b/src/save-restore.c
index 58d1841..dc04e72 100644
--- a/src/save-restore.c
+++ b/src/save-restore.c
@@ -39,9 +39,7 @@
 #include "libbalsa-conf.h"
 #include "threads.h"
 
-#if ENABLE_ESMTP
 #include "smtp-server.h"
-#endif                          /* ENABLE_ESMTP */
 
 #define FOLDER_SECTION_PREFIX "folder-"
 #define MAILBOX_SECTION_PREFIX "mailbox-"
@@ -486,7 +484,6 @@ config_warning_idle(const gchar * text)
     return FALSE;
 }
 
-#if ENABLE_ESMTP
 static gboolean
 config_load_smtp_server(const gchar * key, const gchar * value, gpointer data)
 {
@@ -502,7 +499,6 @@ config_load_smtp_server(const gchar * key, const gchar * value, gpointer data)
 
     return FALSE;
 }
-#endif                          /* ENABLE_ESMTP */
 
 static gboolean
 load_gtk_print_setting(const gchar * key, const gchar * value, gpointer data)
@@ -579,12 +575,11 @@ config_global_load(void)
     static gboolean new_user = FALSE;
 
     config_address_books_load();
-#if ENABLE_ESMTP
+
     /* Load SMTP servers before identities. */
     libbalsa_conf_foreach_group(SMTP_SERVER_SECTION_PREFIX,
                                config_load_smtp_server,
                                &balsa_app.smtp_servers);
-#endif                          /* ENABLE_ESMTP */
 
     /* We must load filters before mailboxes, because they refer to the filters list */
     config_filters_load();
@@ -935,7 +930,6 @@ config_global_load(void)
     /* Sending options ... */
     libbalsa_conf_push_group("Sending");
 
-#if ENABLE_ESMTP
     /* ... SMTP servers */
     if (!balsa_app.smtp_servers) {
        /* Transition code */
@@ -969,7 +963,6 @@ config_global_load(void)
         /* default set to "Use TLS if possible" */
        server->tls_mode = libbalsa_conf_get_int("ESMTPTLSMode=1");
 
-#if HAVE_SMTP_TLS_CLIENT_CERTIFICATE
        passphrase =
            libbalsa_conf_private_get_string("ESMTPCertificatePassphrase");
        if (passphrase) {
@@ -977,9 +970,8 @@ config_global_load(void)
             g_free(passphrase);
            libbalsa_smtp_server_set_cert_passphrase(smtp_server, tmp);
        }
-#endif
     }
-#endif                          /* ENABLE_ESMTP */
+
     /* ... outgoing mail */
     balsa_app.wordwrap = libbalsa_conf_get_bool("WordWrap=false");
     balsa_app.wraplength = libbalsa_conf_get_int("WrapLength=72");
@@ -1173,9 +1165,7 @@ gint
 config_save(void)
 {
     gint i;
-#if ENABLE_ESMTP
     GSList *list;
-#endif                          /* ENABLE_ESMTP */
 
     config_address_books_save();
     config_identities_save();
@@ -1404,7 +1394,6 @@ config_save(void)
     libbalsa_conf_pop_group();
 
     /* Sending options ... */
-#if ENABLE_ESMTP
     for (list = balsa_app.smtp_servers; list; list = list->next) {
         LibBalsaSmtpServer *smtp_server = LIBBALSA_SMTP_SERVER(list->data);
         gchar *group;
@@ -1417,7 +1406,6 @@ config_save(void)
         libbalsa_smtp_server_save_config(smtp_server);
         libbalsa_conf_pop_group();
     }
-#endif                          /* ENABLE_ESMTP */
 
     libbalsa_conf_remove_group("Sending");
     libbalsa_conf_private_remove_group("Sending");
@@ -1596,7 +1584,6 @@ config_address_books_save(void)
                    (GFunc) config_address_book_save, NULL);
 }
 
-#if ENABLE_ESMTP
 static LibBalsaSmtpServer *
 find_smtp_server_by_name(const gchar * name)
 {
@@ -1618,26 +1605,21 @@ find_smtp_server_by_name(const gchar * name)
     g_return_val_if_fail(balsa_app.smtp_servers, NULL);
     return LIBBALSA_SMTP_SERVER(balsa_app.smtp_servers->data);
 }
-#endif                          /* ESMTP */
 
 static gboolean
 config_identity_load(const gchar * key, const gchar * value, gpointer data)
 {
     const gchar *default_ident = data;
     LibBalsaIdentity *ident;
-#if ENABLE_ESMTP
     gchar *smtp_server_name;
-#endif                          /* ENABLE_ESMTP */
 
     libbalsa_conf_push_group(key);
     ident = libbalsa_identity_new_config(value);
-#if ENABLE_ESMTP
     smtp_server_name = libbalsa_conf_get_string("SmtpServer");
     libbalsa_identity_set_smtp_server(ident,
                                       find_smtp_server_by_name
                                       (smtp_server_name));
     g_free(smtp_server_name);
-#endif                          /* ENABLE_ESMTP */
     libbalsa_conf_pop_group();
     balsa_app.identities = g_list_prepend(balsa_app.identities, ident);
     if (g_ascii_strcasecmp(default_ident, ident->identity_name) == 0)
diff --git a/src/sendmsg-window.c b/src/sendmsg-window.c
index 0a86df1..87ff391 100644
--- a/src/sendmsg-window.c
+++ b/src/sendmsg-window.c
@@ -5241,7 +5241,6 @@ send_message_handler(BalsaSendmsg * bsmsg, gboolean queue_only)
                                message->gpg_mode);
 #endif
 
-#if ENABLE_ESMTP
     if(queue_only)
        result = libbalsa_message_queue(message, balsa_app.outbox, fcc,
                                        bsmsg->ident->smtp_server,
@@ -5252,16 +5251,6 @@ send_message_handler(BalsaSendmsg * bsmsg, gboolean queue_only)
                                       bsmsg->ident->smtp_server,
                                        GTK_WINDOW(bsmsg->window),
                                        bsmsg->flow, balsa_app.debug, &error);
-#else
-    if(queue_only)
-       result = libbalsa_message_queue(message, balsa_app.outbox, fcc,
-                                       bsmsg->flow, &error);
-    else
-        result = libbalsa_message_send(message, balsa_app.outbox, fcc,
-                                       balsa_find_sentbox_by_url,
-                                       GTK_WINDOW(bsmsg->window),
-                                      bsmsg->flow, balsa_app.debug, &error);
-#endif
     if (result == LIBBALSA_MESSAGE_CREATE_OK) {
        if (bsmsg->parent_message && bsmsg->parent_message->mailbox
             && !bsmsg->parent_message->mailbox->readonly)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]