[evolution] Give MailSession a permanent home.



commit a06e4484b8df804124b5bcf88d94dec5acfba270
Author: Matthew Barnes <mbarnes redhat com>
Date:   Wed Oct 6 23:38:52 2010 -0400

    Give MailSession a permanent home.
    
    Global variables in shared libraries are a bad idea.  EMailBackend now
    owns the MailSession instance, which is actually now EMailSession.
    
    Move the blocking utility functions in mail-tools.c to e-mail-session.c
    and add asynchronous variants.  Same approach as Camel.
    
    Replace EMailReader.get_shell_backend() with EMailReader.get_backend(),
    which returns an EMailBackend.  Easier access to the EMailSession.

 capplet/settings/mail-account-view.c               |   14 +-
 capplet/settings/mail-account-view.h               |    2 +-
 capplet/settings/mail-capplet-shell.c              |   16 +-
 capplet/settings/mail-view.c                       |    7 +-
 capplet/settings/mail-view.h                       |    2 +
 filter/e-filter-int.c                              |   17 +-
 filter/e-filter-int.h                              |    4 +-
 filter/e-filter-option.c                           |    9 +-
 filter/e-filter-option.h                           |    2 +-
 mail/Makefile.am                                   |    4 +-
 mail/e-mail-backend.c                              |  165 ++-
 mail/e-mail-backend.h                              |    2 +
 mail/e-mail-browser.c                              |  103 +-
 mail/e-mail-browser.h                              |    5 +-
 mail/e-mail-folder-pane.c                          |    4 +-
 mail/e-mail-local.c                                |    9 +-
 mail/e-mail-local.h                                |    4 +-
 mail/e-mail-migrate.c                              |    9 +-
 mail/e-mail-notebook-view.c                        |   28 +-
 mail/e-mail-paned-view.c                           |   30 +-
 mail/e-mail-reader-utils.c                         |   60 +-
 mail/e-mail-reader.c                               |  184 ++-
 mail/e-mail-reader.h                               |   12 +-
 mail/e-mail-session.c                              | 1376 ++++++++++++++++++++
 mail/e-mail-session.h                              |  123 ++
 mail/e-mail-sidebar.c                              |    7 +-
 mail/e-mail-sidebar.h                              |    2 +-
 mail/e-mail-store.c                                |   44 +-
 mail/e-mail-store.h                                |   13 +-
 mail/em-account-editor.c                           |  157 ++-
 mail/em-account-editor.h                           |    4 +
 mail/em-composer-utils.c                           |   61 +-
 mail/em-composer-utils.h                           |    7 +-
 mail/em-filter-context.c                           |  356 +++---
 mail/em-filter-context.h                           |   61 +-
 mail/em-filter-folder-element.c                    |  249 +++--
 mail/em-filter-folder-element.h                    |   59 +-
 mail/em-filter-rule.c                              |    4 +-
 mail/em-filter-rule.h                              |   20 +-
 mail/em-filter-source-element.c                    |    7 +-
 mail/em-filter-source-element.h                    |    2 +-
 mail/em-folder-properties.c                        |   11 +-
 mail/em-folder-selection-button.c                  |  122 ++-
 mail/em-folder-selection-button.h                  |    9 +-
 mail/em-folder-selector.c                          |    2 +-
 mail/em-folder-selector.h                          |    1 -
 mail/em-folder-tree-model.c                        |   91 ++-
 mail/em-folder-tree-model.h                        |    7 +
 mail/em-folder-tree.c                              |  118 ++-
 mail/em-folder-tree.h                              |    5 +-
 mail/em-folder-utils.c                             |   60 +-
 mail/em-folder-utils.h                             |    5 +-
 mail/em-utils.c                                    |   36 +-
 mail/em-utils.h                                    |    7 +-
 mail/em-vfolder-context.c                          |  208 +++-
 mail/em-vfolder-context.h                          |   46 +-
 mail/em-vfolder-editor.c                           |   58 +-
 mail/em-vfolder-rule.c                             |  234 +++--
 mail/em-vfolder-rule.h                             |   55 +-
 mail/importers/elm-importer.c                      |   14 +-
 mail/importers/evolution-mbox-importer.c           |   28 +-
 mail/importers/mail-importer.c                     |   37 +-
 mail/importers/mail-importer.h                     |   20 +-
 mail/importers/pine-importer.c                     |   15 +-
 mail/mail-autofilter.c                             |   72 +-
 mail/mail-autofilter.h                             |   37 +-
 mail/mail-config.c                                 |   54 +-
 mail/mail-config.h                                 |    4 +-
 mail/mail-folder-cache.c                           |    3 +-
 mail/mail-folder-cache.h                           |    1 +
 mail/mail-ops.c                                    |  212 ++--
 mail/mail-ops.h                                    |   68 +-
 mail/mail-send-recv.c                              |  100 +-
 mail/mail-send-recv.h                              |   13 +-
 mail/mail-session.c                                |  998 --------------
 mail/mail-session.h                                |   57 -
 mail/mail-tools.c                                  |  141 +--
 mail/mail-tools.h                                  |   19 -
 mail/mail-vfolder.c                                |  131 ++-
 mail/mail-vfolder.h                                |   33 +-
 mail/message-list.c                                |  109 +-
 mail/message-list.h                                |    6 +-
 modules/mail/e-mail-attachment-handler.c           |   95 +-
 modules/mail/e-mail-junk-hook.c                    |   15 +-
 modules/mail/e-mail-shell-backend.c                |   32 +-
 modules/mail/e-mail-shell-content.c                |   30 +-
 modules/mail/e-mail-shell-settings.c               |   22 +-
 modules/mail/e-mail-shell-settings.h               |    4 +-
 modules/mail/e-mail-shell-sidebar.c                |   10 +-
 modules/mail/e-mail-shell-view-actions.c           |  107 ++-
 modules/mail/e-mail-shell-view-private.c           |   34 +-
 modules/mail/e-mail-shell-view-private.h           |    2 +-
 modules/mail/e-mail-shell-view.c                   |   15 +-
 modules/mail/em-account-prefs.c                    |  163 ++-
 modules/mail/em-account-prefs.h                    |    6 +-
 modules/mail/em-mailer-prefs.c                     |   25 +-
 modules/mail/em-mailer-prefs.h                     |    3 +
 modules/startup-wizard/evolution-startup-wizard.c  |   15 +-
 plugins/dbx-import/dbx-importer.c                  |   15 +-
 plugins/groupwise-features/install-shared.c        |   10 +-
 plugins/groupwise-features/proxy-login.c           |   19 +-
 plugins/groupwise-features/proxy.c                 |   13 +-
 plugins/groupwise-features/share-folder-common.c   |   22 +-
 .../mailing-list-actions/mailing-list-actions.c    |    7 +-
 plugins/mark-all-read/mark-all-read.c              |    6 +-
 plugins/pst-import/pst-importer.c                  |   51 +-
 plugins/templates/templates.c                      |    2 +-
 107 files changed, 4597 insertions(+), 2617 deletions(-)
---
diff --git a/capplet/settings/mail-account-view.c b/capplet/settings/mail-account-view.c
index 9cf9d47..8001e8f 100644
--- a/capplet/settings/mail-account-view.c
+++ b/capplet/settings/mail-account-view.c
@@ -33,7 +33,7 @@
 #include "mail-view.h"
 #include "e-util/e-config.h"
 #include "mail/mail-config.h"
-#include "mail/mail-session.h"
+#include "mail/e-mail-session.h"
 #include "mail-guess-servers.h"
 
 struct _MailAccountViewPrivate {
@@ -1019,7 +1019,8 @@ next_page (GtkWidget *entry, MailAccountView *mav)
 }
 
 static void
-mail_account_view_construct (MailAccountView *view)
+mail_account_view_construct (MailAccountView *view,
+                             EMailSession *session)
 {
 	gint i;
 	EShell *shell;
@@ -1041,7 +1042,9 @@ mail_account_view_construct (MailAccountView *view)
 	gtk_widget_show (view->pages[0]->box);
 	view->current_page = 0;
 	gtk_box_pack_start ((GtkBox *)view, view->scroll, TRUE, TRUE, 0);
-	view->edit = em_account_editor_new_for_pages (view->original, EMAE_PAGES, "org.gnome.evolution.mail.config.accountWizard", view->wpages);
+	view->edit = em_account_editor_new_for_pages (
+		view->original, EMAE_PAGES, session,
+		"org.gnome.evolution.mail.config.accountWizard", view->wpages);
 	gtk_widget_hide (e_config_create_widget (E_CONFIG (view->edit->config)));
 	view->edit->emae_check_servers = emae_check_servers;
 	if (!view->original) {
@@ -1077,13 +1080,14 @@ mail_account_view_construct (MailAccountView *view)
 }
 
 MailAccountView *
-mail_account_view_new (EAccount *account)
+mail_account_view_new (EAccount *account,
+                       EMailSession *session)
 {
 	MailAccountView *view = g_object_new (MAIL_ACCOUNT_VIEW_TYPE, NULL);
 	view->type = MAIL_VIEW_ACCOUNT;
 	view->uri = "account://";
 	view->original = account;
-	mail_account_view_construct (view);
+	mail_account_view_construct (view, session);
 
 	return view;
 }
diff --git a/capplet/settings/mail-account-view.h b/capplet/settings/mail-account-view.h
index 5b728bc..ac03b1a 100644
--- a/capplet/settings/mail-account-view.h
+++ b/capplet/settings/mail-account-view.h
@@ -87,7 +87,7 @@ typedef struct _MailAccountViewClass {
 } MailAccountViewClass;
 
 GType mail_account_view_get_type (void);
-MailAccountView *mail_account_view_new (EAccount *account);
+MailAccountView *mail_account_view_new (EAccount *account, EMailSession *session);
 GtkWidget * mail_account_view_get_tab_widget (MailAccountView *mcv);
 void mail_account_view_activate (MailAccountView *mcv, GtkWidget *tree, GtkWidget *folder_tree, GtkWidget *check_mail, GtkWidget *sort_by, gboolean act);
 #endif
diff --git a/capplet/settings/mail-capplet-shell.c b/capplet/settings/mail-capplet-shell.c
index 63f8625..9153edb 100644
--- a/capplet/settings/mail-capplet-shell.c
+++ b/capplet/settings/mail-capplet-shell.c
@@ -38,7 +38,6 @@
 #include "mail-decoration.h"
 #include <mail/em-utils.h>
 #include <mail/em-composer-utils.h>
-#include <mail/mail-session.h>
 #include <mail/mail-mt.h>
 #include <mail/e-mail-store.h>
 
@@ -68,6 +67,7 @@ static guint mail_capplet_shell_signals[LAST_SIGNAL];
 
 struct  _MailCappletShellPrivate {
 
+	EMailSession *session;
 	GtkWidget *box;
 
 	GtkWidget * top_bar;
@@ -237,17 +237,23 @@ mail_capplet_shell_construct (MailCappletShell *shell, gint socket_id, gboolean
 
 	}
 
+	if (camel_init (e_get_user_data_dir (), TRUE) != 0)
+		exit (0);
+
+	camel_provider_init ();
+
+	shell->priv->session = e_mail_session_new ();
+
 	shell->view = mail_view_new ();
+	shell->view->session = shell->priv->session;
 	gtk_widget_show ((GtkWidget *)shell->view);
 	tmp = gtk_vbox_new (FALSE, 0);
 	gtk_box_pack_end ((GtkBox *)priv->box, (GtkWidget *)shell->view, TRUE, TRUE, 2);
 
-	/* This also initializes Camel, so it needs to happen early. */
-	mail_session_start ();
-	mail_config_init ();
+	mail_config_init (CAMEL_SESSION (shell->priv->session));
 	mail_msg_init ();
 	custom_dir = g_build_filename (e_get_user_data_dir (), "mail", NULL);
-	e_mail_store_init (custom_dir);
+	e_mail_store_init (shell->priv->session, custom_dir);
 	g_free (custom_dir);
 
 	if (just_druid) {
diff --git a/capplet/settings/mail-view.c b/capplet/settings/mail-view.c
index a294a8e..cb2010f 100644
--- a/capplet/settings/mail-view.c
+++ b/capplet/settings/mail-view.c
@@ -438,11 +438,14 @@ mail_view_switch_to_settings (MailView* mv, MailViewChild *mpv)
 }
 
 static MailViewChild *
-mail_view_add_account (MailView *mv, gpointer data, gboolean block)
+mail_view_add_account (MailView *mv,
+                       gpointer data,
+                       gboolean block)
 {
-	MailAccountView *msv  = mail_account_view_new (data);
+	MailAccountView *msv;
 	gint position = 0;
 
+	msv = mail_account_view_new (data, mv->session);
 	gtk_widget_show ((GtkWidget *)msv);
 	if (!block)
 		mv->priv->current_view = (MailViewChild *)msv;
diff --git a/capplet/settings/mail-view.h b/capplet/settings/mail-view.h
index a2a8df6..904338e 100644
--- a/capplet/settings/mail-view.h
+++ b/capplet/settings/mail-view.h
@@ -24,6 +24,7 @@
 #define _MAIL_VIEW_H_
 
 #include <shell/e-shell-view.h>
+#include <mail/e-mail-session.h>
 
 #include "anjal-mail-view.h"
 
@@ -48,6 +49,7 @@ typedef struct _MailViewPrivate MailViewPrivate;
 typedef struct _MailView {
 	AnjalMailView parent;
 
+	EMailSession *session;
 	GtkWidget *tree; /* Actual tree */
 	GtkWidget *folder_tree;
 	GtkWidget *slider;
diff --git a/filter/e-filter-int.c b/filter/e-filter-int.c
index 871e595..3fcfe87 100644
--- a/filter/e-filter-int.c
+++ b/filter/e-filter-int.c
@@ -77,7 +77,7 @@ filter_int_clone (EFilterElement *element)
 	EFilterInt *filter_int = E_FILTER_INT (element);
 	EFilterInt *clone;
 
-	clone = e_filter_int_new_type (
+	clone = (EFilterInt *) e_filter_int_new_type (
 		filter_int->type, filter_int->min, filter_int->max);
 	clone->val = filter_int->val;
 
@@ -200,33 +200,26 @@ e_filter_int_init (EFilterInt *filter_int)
 	filter_int->max = G_MAXINT;
 }
 
-/**
- * filter_int_new:
- *
- * Create a new EFilterInt object.
- *
- * Return value: A new #EFilterInt object.
- **/
-EFilterInt *
+EFilterElement *
 e_filter_int_new (void)
 {
 	return g_object_new (E_TYPE_FILTER_INT, NULL);
 }
 
-EFilterInt *
+EFilterElement *
 e_filter_int_new_type (const gchar *type,
                        gint min,
                        gint max)
 {
 	EFilterInt *filter_int;
 
-	filter_int = e_filter_int_new ();
+	filter_int = g_object_new (E_TYPE_FILTER_INT, NULL);
 
 	filter_int->type = g_strdup (type);
 	filter_int->min = min;
 	filter_int->max = max;
 
-	return filter_int;
+	return E_FILTER_ELEMENT (filter_int);
 }
 
 void
diff --git a/filter/e-filter-int.h b/filter/e-filter-int.h
index c9f613c..be0aa4e 100644
--- a/filter/e-filter-int.h
+++ b/filter/e-filter-int.h
@@ -65,8 +65,8 @@ struct _EFilterIntClass {
 };
 
 GType		e_filter_int_get_type		(void);
-EFilterInt *	e_filter_int_new		(void);
-EFilterInt *	e_filter_int_new_type		(const gchar *type,
+EFilterElement *e_filter_int_new		(void);
+EFilterElement *e_filter_int_new_type		(const gchar *type,
 						 gint min,
 						 gint max);
 void		e_filter_int_set_value		(EFilterInt *f_int,
diff --git a/filter/e-filter-option.c b/filter/e-filter-option.c
index c0ce5b7..e8a4dcc 100644
--- a/filter/e-filter-option.c
+++ b/filter/e-filter-option.c
@@ -425,14 +425,7 @@ e_filter_option_init (EFilterOption *option)
 	option->dynamic_func = NULL;
 }
 
-/**
- * filter_option_new:
- *
- * Create a new EFilterOption object.
- *
- * Return value: A new #EFilterOption object.
- **/
-EFilterOption *
+EFilterElement *
 e_filter_option_new (void)
 {
 	return g_object_new (E_TYPE_FILTER_OPTION, NULL);
diff --git a/filter/e-filter-option.h b/filter/e-filter-option.h
index c42efe2..45d426b 100644
--- a/filter/e-filter-option.h
+++ b/filter/e-filter-option.h
@@ -75,7 +75,7 @@ struct _EFilterOptionClass {
 };
 
 GType		e_filter_option_get_type	(void);
-EFilterOption *	e_filter_option_new		(void);
+EFilterElement *e_filter_option_new		(void);
 void		e_filter_option_set_current	(EFilterOption *option,
 						 const gchar *name);
 const gchar *	e_filter_option_get_current	(EFilterOption *option);
diff --git a/mail/Makefile.am b/mail/Makefile.am
index 79db282..f30de3e 100644
--- a/mail/Makefile.am
+++ b/mail/Makefile.am
@@ -54,6 +54,7 @@ mailinclude_HEADERS =					\
 	e-mail-migrate.h				\
 	e-mail-reader.h					\
 	e-mail-reader-utils.h				\
+	e-mail-session.h				\
 	e-mail-sidebar.h				\
 	e-mail-store.h					\
 	e-mail-tag-editor.h				\
@@ -92,7 +93,6 @@ mailinclude_HEADERS =					\
 	mail-mt.h					\
 	mail-ops.h					\
 	mail-send-recv.h				\
-	mail-session.h					\
 	mail-tools.h					\
 	mail-vfolder.h					\
 	message-list.h
@@ -123,6 +123,7 @@ libevolution_mail_la_SOURCES =				\
 	e-mail-migrate.c				\
 	e-mail-reader.c					\
 	e-mail-reader-utils.c				\
+	e-mail-session.c				\
 	e-mail-sidebar.c				\
 	e-mail-store.c					\
 	e-mail-tag-editor.c				\
@@ -161,7 +162,6 @@ libevolution_mail_la_SOURCES =				\
 	mail-mt.c					\
 	mail-ops.c					\
 	mail-send-recv.c				\
-	mail-session.c					\
 	mail-tools.c					\
 	mail-vfolder.c					\
 	message-list.c
diff --git a/mail/e-mail-backend.c b/mail/e-mail-backend.c
index 4dab31c..c9c668d 100644
--- a/mail/e-mail-backend.c
+++ b/mail/e-mail-backend.c
@@ -22,9 +22,11 @@
  *
  */
 
-#include <string.h>
 #include "e-mail-backend.h"
 
+#include <string.h>
+#include <libedataserver/e-data-server-util.h>
+
 #include "e-util/e-account-utils.h"
 #include "e-util/e-alert-dialog.h"
 #include "e-util/e-binding.h"
@@ -35,6 +37,7 @@
 
 #include "mail/e-mail-local.h"
 #include "mail/e-mail-migrate.h"
+#include "mail/e-mail-session.h"
 #include "mail/e-mail-store.h"
 #include "mail/em-event.h"
 #include "mail/em-folder-tree-model.h"
@@ -42,7 +45,6 @@
 #include "mail/mail-autofilter.h"
 #include "mail/mail-folder-cache.h"
 #include "mail/mail-ops.h"
-#include "mail/mail-session.h"
 #include "mail/mail-vfolder.h"
 
 #define E_MAIL_BACKEND_GET_PRIVATE(obj) \
@@ -52,14 +54,22 @@
 #define QUIT_POLL_INTERVAL 1  /* seconds */
 
 struct _EMailBackendPrivate {
-	gint placeholder;  /* for future expansion */
+	EMailSession *session;
 };
 
-static gpointer parent_class;
+enum {
+	PROP_0,
+	PROP_SESSION
+};
 
 /* FIXME Kill this thing.  It's a horrible hack. */
 extern gint camel_application_is_exiting;
 
+G_DEFINE_ABSTRACT_TYPE (
+	EMailBackend,
+	e_mail_backend,
+	E_TYPE_SHELL_BACKEND)
+
 static const gchar *
 mail_shell_backend_get_data_dir (EShellBackend *backend)
 {
@@ -100,9 +110,11 @@ mail_backend_prepare_for_offline_cb (EShell *shell,
                                      EMailBackend *backend)
 {
 	GtkWindow *window;
+	EMailSession *session;
 	gboolean synchronize = FALSE;
 
 	window = e_shell_get_active_window (shell);
+	session = e_mail_backend_get_session (backend);
 
 	if (e_shell_get_network_available (shell))
 		synchronize = em_utils_prompt_user (
@@ -110,7 +122,8 @@ mail_backend_prepare_for_offline_cb (EShell *shell,
 
 	if (!synchronize) {
 		mail_cancel_all ();
-		camel_session_set_network_available (session, FALSE);
+		camel_session_set_network_available (
+			CAMEL_SESSION (session), FALSE);
 	}
 
 	e_mail_store_foreach (
@@ -135,7 +148,10 @@ mail_backend_prepare_for_online_cb (EShell *shell,
                                     EActivity *activity,
                                     EMailBackend *backend)
 {
-	camel_session_set_online (session, TRUE);
+	EMailSession *session;
+
+	session = e_mail_backend_get_session (backend);
+	camel_session_set_online (CAMEL_SESSION (session), TRUE);
 
 	e_mail_store_foreach (
 		(GHFunc) mail_store_prepare_for_online_cb, activity);
@@ -203,7 +219,7 @@ mail_backend_poll_to_quit (EActivity *activity)
 static void
 mail_backend_ready_to_quit (EActivity *activity)
 {
-	mail_session_shutdown ();
+	camel_shutdown ();
 	emu_free_mail_cache ();
 
 	/* Do this last.  It may terminate the process. */
@@ -304,18 +320,26 @@ mail_backend_quit_requested_cb (EShell *shell,
 static void
 mail_backend_folder_deleted_cb (MailFolderCache *folder_cache,
                                 CamelStore *store,
-                                const gchar *uri)
+                                const gchar *uri,
+                                EMailBackend *backend)
 {
-	mail_filter_delete_uri (store, uri);
+	EMailSession *session;
+
+	session = e_mail_backend_get_session (backend);
+	mail_filter_delete_uri (session, store, uri);
 }
 
 static void
 mail_backend_folder_renamed_cb (MailFolderCache *folder_cache,
                                 CamelStore *store,
                                 const gchar *old_uri,
-                                const gchar *new_uri)
+                                const gchar *new_uri,
+                                EMailBackend *backend)
 {
-	mail_filter_rename_uri (store, old_uri, new_uri);
+	EMailSession *session;
+
+	session = e_mail_backend_get_session (backend);
+	mail_filter_rename_uri (session, store, old_uri, new_uri);
 }
 
 static void
@@ -370,31 +394,86 @@ mail_backend_folder_changed_cb (MailFolderCache *folder_cache,
 }
 
 static gboolean
-mail_backend_idle_cb (EShellBackend *shell_backend)
+mail_backend_idle_cb (EMailBackend *backend)
 {
+	EMailSession *session;
+	EShellBackend *shell_backend;
 	const gchar *data_dir;
 
+	session = e_mail_backend_get_session (backend);
+
+	shell_backend = E_SHELL_BACKEND (backend);
 	data_dir = e_shell_backend_get_data_dir (shell_backend);
-	e_mail_store_init (data_dir);
+
+	e_mail_store_init (session, data_dir);
 
 	return FALSE;
 }
 
 static void
+mail_backend_get_property (GObject *object,
+                           guint property_id,
+                           GValue *value,
+                           GParamSpec *pspec)
+{
+	switch (property_id) {
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				e_mail_backend_get_session (
+				E_MAIL_BACKEND (object)));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
+mail_backend_dispose (GObject *object)
+{
+	EMailBackendPrivate *priv;
+
+	priv = E_MAIL_BACKEND_GET_PRIVATE (object);
+
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
+	/* Chain up to parent's dispose() method. */
+	G_OBJECT_CLASS (e_mail_backend_parent_class)->dispose (object);
+}
+
+static void
 mail_backend_constructed (GObject *object)
 {
+	EMailBackendPrivate *priv;
 	EShell *shell;
 	EShellBackend *shell_backend;
 	MailFolderCache *folder_cache;
+	EMFolderTreeModel *folder_tree_model;
+
+	priv = E_MAIL_BACKEND_GET_PRIVATE (object);
 
 	shell_backend = E_SHELL_BACKEND (object);
 	shell = e_shell_backend_get_shell (shell_backend);
 
-	/* This also initializes Camel, so it needs to happen early. */
-	mail_session_start ();
+	if (camel_init (e_get_user_data_dir (), TRUE) != 0)
+		exit (0);
 
-	e_binding_new (shell, "online", session, "online");
-	e_account_combo_box_set_session (session);  /* XXX Don't ask... */
+	camel_provider_init ();
+
+	priv->session = e_mail_session_new ();
+
+	e_binding_new (shell, "online", priv->session, "online");
+
+	/* FIXME This is an evil hack that needs to die.
+	 *       Give EAccountComboBox a CamelSession property. */
+	e_account_combo_box_set_session (CAMEL_SESSION (priv->session));
+
+	/* FIXME EMailBackend should own the default EMFolderTreeModel. */
+	folder_tree_model = em_folder_tree_model_get_default ();
+	em_folder_tree_model_set_session (folder_tree_model, priv->session);
 
 	folder_cache = mail_folder_cache_get_default ();
 
@@ -420,17 +499,19 @@ mail_backend_constructed (GObject *object)
 
 	g_signal_connect (
 		folder_cache, "folder-deleted",
-		G_CALLBACK (mail_backend_folder_deleted_cb), NULL);
+		G_CALLBACK (mail_backend_folder_deleted_cb),
+		shell_backend);
 
 	g_signal_connect (
 		folder_cache, "folder-renamed",
-		G_CALLBACK (mail_backend_folder_renamed_cb), NULL);
+		G_CALLBACK (mail_backend_folder_renamed_cb),
+		shell_backend);
 
 	g_signal_connect (
 		folder_cache, "folder-changed",
 		G_CALLBACK (mail_backend_folder_changed_cb), shell);
 
-	mail_config_init ();
+	mail_config_init (CAMEL_SESSION (priv->session));
 	mail_msg_init ();
 
 	/* Defer initializing CamelStores until after the main loop
@@ -439,54 +520,46 @@ mail_backend_constructed (GObject *object)
 }
 
 static void
-mail_backend_class_init (EMailBackendClass *class)
+e_mail_backend_class_init (EMailBackendClass *class)
 {
 	GObjectClass *object_class;
 	EShellBackendClass *shell_backend_class;
 
-	parent_class = g_type_class_peek_parent (class);
 	g_type_class_add_private (class, sizeof (EMailBackendPrivate));
 
 	object_class = G_OBJECT_CLASS (class);
+	object_class->get_property = mail_backend_get_property;
+	object_class->dispose = mail_backend_dispose;
 	object_class->constructed = mail_backend_constructed;
 
 	shell_backend_class = E_SHELL_BACKEND_CLASS (class);
 	shell_backend_class->migrate = e_mail_migrate;
 	shell_backend_class->get_data_dir = mail_shell_backend_get_data_dir;
 	shell_backend_class->get_config_dir = mail_shell_backend_get_config_dir;
+
+	g_object_class_install_property (
+		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			NULL,
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READABLE));
 }
 
 static void
-mail_backend_init (EMailBackend *backend)
+e_mail_backend_init (EMailBackend *backend)
 {
 	backend->priv = E_MAIL_BACKEND_GET_PRIVATE (backend);
 }
 
-GType
-e_mail_backend_get_type (void)
+EMailSession *
+e_mail_backend_get_session (EMailBackend *backend)
 {
-	static GType type = 0;
-
-	if (G_UNLIKELY (type == 0)) {
-		static const GTypeInfo type_info = {
-			sizeof (EMailBackendClass),
-			(GBaseInitFunc) NULL,
-			(GBaseFinalizeFunc) NULL,
-			(GClassInitFunc) mail_backend_class_init,
-			(GClassFinalizeFunc) NULL,
-			NULL,  /* class_data */
-			sizeof (EMailBackend),
-			0,     /* n_preallocs */
-			(GInstanceInitFunc) mail_backend_init,
-			NULL   /* value_table */
-		};
-
-		type = g_type_register_static (
-			E_TYPE_SHELL_BACKEND, "EMailBackend", &type_info,
-			G_TYPE_FLAG_ABSTRACT);
-	}
+	g_return_val_if_fail (E_IS_MAIL_BACKEND (backend), NULL);
 
-	return type;
+	return backend->priv->session;
 }
 
 gboolean
diff --git a/mail/e-mail-backend.h b/mail/e-mail-backend.h
index 0d2dc2d..18fdf88 100644
--- a/mail/e-mail-backend.h
+++ b/mail/e-mail-backend.h
@@ -26,6 +26,7 @@
 #ifndef E_MAIL_BACKEND_H
 #define E_MAIL_BACKEND_H
 
+#include <mail/e-mail-session.h>
 #include <shell/e-shell-backend.h>
 
 /* Standard GObject macros */
@@ -69,6 +70,7 @@ struct _EMailBackendClass {
 };
 
 GType		e_mail_backend_get_type		(void);
+EMailSession *	e_mail_backend_get_session	(EMailBackend *backend);
 gboolean	e_mail_backend_delete_junk_policy_decision
 						(EMailBackend *backend);
 gboolean	e_mail_backend_empty_trash_policy_decision
diff --git a/mail/e-mail-browser.c b/mail/e-mail-browser.c
index 3ffc290..d1fe7ca 100644
--- a/mail/e-mail-browser.c
+++ b/mail/e-mail-browser.c
@@ -48,9 +48,9 @@
 #define MAIL_BROWSER_GCONF_PREFIX "/apps/evolution/mail/mail_browser"
 
 struct _EMailBrowserPrivate {
+	EMailBackend *backend;
 	GtkUIManager *ui_manager;
 	EFocusTracker *focus_tracker;
-	EShellBackend *shell_backend;
 	GtkActionGroup *action_group;
 	EMFormatHTMLDisplay *formatter;
 
@@ -65,9 +65,9 @@ struct _EMailBrowserPrivate {
 
 enum {
 	PROP_0,
+	PROP_BACKEND,
 	PROP_FOCUS_TRACKER,
 	PROP_GROUP_BY_THREADS,
-	PROP_SHELL_BACKEND,
 	PROP_SHOW_DELETED,
 	PROP_UI_MANAGER
 };
@@ -339,12 +339,13 @@ mail_browser_status_message_cb (EMailBrowser *browser,
 }
 
 static void
-mail_browser_set_shell_backend (EMailBrowser *browser,
-                                EShellBackend *shell_backend)
+mail_browser_set_backend (EMailBrowser *browser,
+                          EMailBackend *backend)
 {
-	g_return_if_fail (browser->priv->shell_backend == NULL);
+	g_return_if_fail (E_IS_MAIL_BACKEND (backend));
+	g_return_if_fail (browser->priv->backend == NULL);
 
-	browser->priv->shell_backend = g_object_ref (shell_backend);
+	browser->priv->backend = g_object_ref (backend);
 }
 
 static void
@@ -354,18 +355,18 @@ mail_browser_set_property (GObject *object,
                            GParamSpec *pspec)
 {
 	switch (property_id) {
+		case PROP_BACKEND:
+			mail_browser_set_backend (
+				E_MAIL_BROWSER (object),
+				g_value_get_object (value));
+			return;
+
 		case PROP_GROUP_BY_THREADS:
 			e_mail_reader_set_group_by_threads (
 				E_MAIL_READER (object),
 				g_value_get_boolean (value));
 			return;
 
-		case PROP_SHELL_BACKEND:
-			mail_browser_set_shell_backend (
-				E_MAIL_BROWSER (object),
-				g_value_get_object (value));
-			return;
-
 		case PROP_SHOW_DELETED:
 			e_mail_browser_set_show_deleted (
 				E_MAIL_BROWSER (object),
@@ -383,6 +384,12 @@ mail_browser_get_property (GObject *object,
                            GParamSpec *pspec)
 {
 	switch (property_id) {
+		case PROP_BACKEND:
+			g_value_set_object (
+				value, e_mail_reader_get_backend (
+				E_MAIL_READER (object)));
+			return;
+
 		case PROP_FOCUS_TRACKER:
 			g_value_set_object (
 				value, e_mail_browser_get_focus_tracker (
@@ -395,12 +402,6 @@ mail_browser_get_property (GObject *object,
 				E_MAIL_READER (object)));
 			return;
 
-		case PROP_SHELL_BACKEND:
-			g_value_set_object (
-				value, e_mail_reader_get_shell_backend (
-				E_MAIL_READER (object)));
-			return;
-
 		case PROP_SHOW_DELETED:
 			g_value_set_boolean (
 				value, e_mail_browser_get_show_deleted (
@@ -424,6 +425,11 @@ mail_browser_dispose (GObject *object)
 
 	priv = E_MAIL_BROWSER_GET_PRIVATE (object);
 
+	if (priv->backend != NULL) {
+		g_object_unref (priv->backend);
+		priv->backend = NULL;
+	}
+
 	if (priv->ui_manager != NULL) {
 		g_object_unref (priv->ui_manager);
 		priv->ui_manager = NULL;
@@ -434,11 +440,6 @@ mail_browser_dispose (GObject *object)
 		priv->focus_tracker = NULL;
 	}
 
-	if (priv->shell_backend != NULL) {
-		g_object_unref (priv->shell_backend);
-		priv->shell_backend = NULL;
-	}
-
 	if (priv->action_group != NULL) {
 		g_object_unref (priv->action_group);
 		priv->action_group = NULL;
@@ -485,6 +486,7 @@ mail_browser_constructed (GObject *object)
 	EMailBrowserPrivate *priv;
 	EMFormatHTML *formatter;
 	EMailReader *reader;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	EShell *shell;
 	EFocusTracker *focus_tracker;
@@ -509,8 +511,9 @@ mail_browser_constructed (GObject *object)
 	priv = E_MAIL_BROWSER_GET_PRIVATE (object);
 
 	reader = E_MAIL_READER (object);
+	backend = e_mail_reader_get_backend (reader);
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 
 	ui_manager = e_ui_manager_new ();
@@ -527,7 +530,7 @@ mail_browser_constructed (GObject *object)
 	/* The message list is a widget, but it is not shown in the browser.
 	 * Unfortunately, the widget is inseparable from its model, and the
 	 * model is all we need. */
-	priv->message_list = message_list_new (shell_backend);
+	priv->message_list = message_list_new (backend);
 	g_object_ref_sink (priv->message_list);
 
 	g_signal_connect_swapped (
@@ -662,6 +665,16 @@ mail_browser_get_action_group (EMailReader *reader)
 	return priv->action_group;
 }
 
+static EMailBackend *
+mail_browser_get_backend (EMailReader *reader)
+{
+	EMailBrowserPrivate *priv;
+
+	priv = E_MAIL_BROWSER_GET_PRIVATE (reader);
+
+	return priv->backend;
+}
+
 static gboolean
 mail_browser_get_hide_deleted (EMailReader *reader)
 {
@@ -706,16 +719,6 @@ mail_browser_get_popup_menu (EMailReader *reader)
 	return GTK_MENU (widget);
 }
 
-static EShellBackend *
-mail_browser_get_shell_backend (EMailReader *reader)
-{
-	EMailBrowserPrivate *priv;
-
-	priv = E_MAIL_BROWSER_GET_PRIVATE (reader);
-
-	return priv->shell_backend;
-}
-
 static GtkWindow *
 mail_browser_get_window (EMailReader *reader)
 {
@@ -780,6 +783,17 @@ mail_browser_class_init (EMailBrowserClass *class)
 
 	g_object_class_install_property (
 		object_class,
+		PROP_BACKEND,
+		g_param_spec_object (
+			"backend",
+			"Mail Backend",
+			"The mail backend",
+			E_TYPE_MAIL_BACKEND,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
+
+	g_object_class_install_property (
+		object_class,
 		PROP_FOCUS_TRACKER,
 		g_param_spec_object (
 			"focus-tracker",
@@ -796,17 +810,6 @@ mail_browser_class_init (EMailBrowserClass *class)
 
 	g_object_class_install_property (
 		object_class,
-		PROP_SHELL_BACKEND,
-		g_param_spec_object (
-			"shell-backend",
-			"Shell Module",
-			"The mail shell backend",
-			E_TYPE_SHELL_BACKEND,
-			G_PARAM_READWRITE |
-			G_PARAM_CONSTRUCT_ONLY));
-
-	g_object_class_install_property (
-		object_class,
 		PROP_SHOW_DELETED,
 		g_param_spec_boolean (
 			"show-deleted",
@@ -820,11 +823,11 @@ static void
 mail_browser_interface_init (EMailReaderInterface *interface)
 {
 	interface->get_action_group = mail_browser_get_action_group;
+	interface->get_backend = mail_browser_get_backend;
 	interface->get_formatter = mail_browser_get_formatter;
 	interface->get_hide_deleted = mail_browser_get_hide_deleted;
 	interface->get_message_list = mail_browser_get_message_list;
 	interface->get_popup_menu = mail_browser_get_popup_menu;
-	interface->get_shell_backend = mail_browser_get_shell_backend;
 	interface->get_window = mail_browser_get_window;
 	interface->set_message = mail_browser_set_message;
 	interface->show_search_bar = mail_browser_show_search_bar;
@@ -884,13 +887,13 @@ e_mail_browser_get_type (void)
 }
 
 GtkWidget *
-e_mail_browser_new (EShellBackend *shell_backend)
+e_mail_browser_new (EMailBackend *backend)
 {
-	g_return_val_if_fail (E_IS_SHELL_BACKEND (shell_backend), NULL);
+	g_return_val_if_fail (E_IS_MAIL_BACKEND (backend), NULL);
 
 	return g_object_new (
 		E_TYPE_MAIL_BROWSER,
-		"shell-backend", shell_backend, NULL);
+		"backend", backend, NULL);
 }
 
 void
diff --git a/mail/e-mail-browser.h b/mail/e-mail-browser.h
index 4b1c3eb..c09c85b 100644
--- a/mail/e-mail-browser.h
+++ b/mail/e-mail-browser.h
@@ -22,9 +22,8 @@
 #ifndef E_MAIL_BROWSER_H
 #define E_MAIL_BROWSER_H
 
-#include <gtk/gtk.h>
+#include <mail/e-mail-backend.h>
 #include <misc/e-focus-tracker.h>
-#include <shell/e-shell-backend.h>
 
 /* Standard GObject macros */
 #define E_TYPE_MAIL_BROWSER \
@@ -61,7 +60,7 @@ struct _EMailBrowserClass {
 };
 
 GType		e_mail_browser_get_type		(void);
-GtkWidget *	e_mail_browser_new		(EShellBackend *shell_backend);
+GtkWidget *	e_mail_browser_new		(EMailBackend *backend);
 void		e_mail_browser_close		(EMailBrowser *browser);
 gboolean	e_mail_browser_get_show_deleted	(EMailBrowser *browser);
 void		e_mail_browser_set_show_deleted (EMailBrowser *browser,
diff --git a/mail/e-mail-folder-pane.c b/mail/e-mail-folder-pane.c
index 6d2be33..a8c2a3d 100644
--- a/mail/e-mail-folder-pane.c
+++ b/mail/e-mail-folder-pane.c
@@ -70,6 +70,7 @@ mail_paned_view_open_selected_mail (EMailPanedView *view)
 	EShell *shell;
 	EShellBackend *shell_backend;
 	EMailReader *reader;
+	EMailBackend *backend;
 	GPtrArray *uids;
 	gint i;
 	GtkWindow *window;
@@ -79,8 +80,9 @@ mail_paned_view_open_selected_mail (EMailPanedView *view)
 	guint n_views, ii;
 
 	reader = E_MAIL_READER (view);
+	backend = e_mail_reader_get_backend (reader);
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 
 	uids = e_mail_reader_get_selected_uids (reader);
diff --git a/mail/e-mail-local.c b/mail/e-mail-local.c
index 861ac8f..1e7e66d 100644
--- a/mail/e-mail-local.c
+++ b/mail/e-mail-local.c
@@ -23,8 +23,6 @@
 
 #include <glib/gi18n.h>
 
-#include "mail/mail-session.h"
-
 #define CHECK_LOCAL_FOLDER_TYPE(type) \
 	((type) < G_N_ELEMENTS (default_local_folders))
 
@@ -45,7 +43,8 @@ static struct {
 static CamelStore *local_store;
 
 void
-e_mail_local_init (const gchar *data_dir)
+e_mail_local_init (EMailSession *session,
+                   const gchar *data_dir)
 {
 	static gboolean initialized = FALSE;
 	CamelService *service;
@@ -55,6 +54,7 @@ e_mail_local_init (const gchar *data_dir)
 	GError *local_error = NULL;
 
 	g_return_if_fail (!initialized);
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
 	g_return_if_fail (data_dir != NULL);
 
 	url = camel_url_new ("mbox:", NULL);
@@ -64,7 +64,8 @@ e_mail_local_init (const gchar *data_dir)
 
 	temp = camel_url_to_string (url, 0);
 	service = camel_session_get_service (
-		session, temp, CAMEL_PROVIDER_STORE, &local_error);
+		CAMEL_SESSION (session), temp,
+		CAMEL_PROVIDER_STORE, &local_error);
 	g_free (temp);
 
 	if (local_error != NULL)
diff --git a/mail/e-mail-local.h b/mail/e-mail-local.h
index b5b7e8a..125c645 100644
--- a/mail/e-mail-local.h
+++ b/mail/e-mail-local.h
@@ -24,6 +24,7 @@
 
 #include <glib.h>
 #include <camel/camel.h>
+#include <mail/e-mail-session.h>
 
 G_BEGIN_DECLS
 
@@ -40,7 +41,8 @@ typedef enum {
 	E_MAIL_NUM_LOCAL_FOLDERS
 } EMailLocalFolder;
 
-void		e_mail_local_init		(const gchar *data_dir);
+void		e_mail_local_init		(EMailSession *session,
+						 const gchar *data_dir);
 CamelFolder *	e_mail_local_get_folder		(EMailLocalFolder type);
 const gchar *	e_mail_local_get_folder_uri	(EMailLocalFolder type);
 CamelStore *	e_mail_local_get_store		(void);
diff --git a/mail/e-mail-migrate.c b/mail/e-mail-migrate.c
index f5481bc..ff3b704 100644
--- a/mail/e-mail-migrate.c
+++ b/mail/e-mail-migrate.c
@@ -60,6 +60,7 @@
 #include "shell/e-shell-migrate.h"
 
 #include "e-mail-store.h"
+#include "e-mail-backend.h"
 #include "mail-config.h"
 #include "em-utils.h"
 
@@ -633,6 +634,8 @@ migrate_to_db (EShellBackend *shell_backend)
 {
 	EMMigrateSession *session;
 	EAccountList *accounts;
+	EMailBackend *mail_backend;
+	EMailSession *mail_session;
 	EIterator *iter;
 	gint i=0, len;
 	CamelStore *store = NULL;
@@ -642,6 +645,9 @@ migrate_to_db (EShellBackend *shell_backend)
 	if (!(accounts = e_get_account_list ()))
 		return;
 
+	mail_backend = E_MAIL_BACKEND (shell_backend);
+	mail_session = e_mail_backend_get_session (mail_backend);
+
 	iter = e_list_get_iterator ((EList *) accounts);
 	len = e_list_length ((EList *) accounts);
 
@@ -694,7 +700,8 @@ migrate_to_db (EShellBackend *shell_backend)
 		    && service->url[0]
 		    && strncmp(service->url, "mbox:", 5) != 0) {
 
-			e_mail_store_add_by_uri (service->url, name);
+			e_mail_store_add_by_uri (
+				mail_session, service->url, name);
 
 			store = (CamelStore *) camel_session_get_service (CAMEL_SESSION (session), service->url, CAMEL_PROVIDER_STORE, NULL);
 			info = camel_store_get_folder_info_sync (
diff --git a/mail/e-mail-notebook-view.c b/mail/e-mail-notebook-view.c
index 3d8ae70..9b0d360 100644
--- a/mail/e-mail-notebook-view.c
+++ b/mail/e-mail-notebook-view.c
@@ -735,6 +735,20 @@ mail_notebook_view_get_action_group (EMailReader *reader)
 	return E_SHELL_WINDOW_ACTION_GROUP_MAIL (shell_window);
 }
 
+static EMailBackend *
+mail_notebook_view_get_backend (EMailReader *reader)
+{
+	EMailView *view;
+	EShellView *shell_view;
+	EShellBackend *shell_backend;
+
+	view = E_MAIL_VIEW (reader);
+	shell_view = e_mail_view_get_shell_view (view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	return E_MAIL_BACKEND (shell_backend);
+}
+
 static EMFormatHTML *
 mail_notebook_view_get_formatter (EMailReader *reader)
 {
@@ -793,18 +807,6 @@ mail_notebook_view_get_popup_menu (EMailReader *reader)
 	return e_mail_reader_get_popup_menu (reader);
 }
 
-static EShellBackend *
-mail_notebook_view_get_shell_backend (EMailReader *reader)
-{
-	EMailView *view;
-	EShellView *shell_view;
-
-	view = E_MAIL_VIEW (reader);
-	shell_view = e_mail_view_get_shell_view (view);
-
-	return e_shell_view_get_shell_backend (shell_view);
-}
-
 static GtkWindow *
 mail_notebook_view_get_window (EMailReader *reader)
 {
@@ -1261,11 +1263,11 @@ static void
 e_mail_notebook_view_reader_init (EMailReaderInterface *interface)
 {
 	interface->get_action_group = mail_notebook_view_get_action_group;
+	interface->get_backend = mail_notebook_view_get_backend;
 	interface->get_formatter = mail_notebook_view_get_formatter;
 	interface->get_hide_deleted = mail_notebook_view_get_hide_deleted;
 	interface->get_message_list = mail_notebook_view_get_message_list;
 	interface->get_popup_menu = mail_notebook_view_get_popup_menu;
-	interface->get_shell_backend = mail_notebook_view_get_shell_backend;
 	interface->get_window = mail_notebook_view_get_window;
 	interface->set_folder = mail_notebook_view_set_folder;
 	interface->show_search_bar = mail_notebook_view_show_search_bar;
diff --git a/mail/e-mail-paned-view.c b/mail/e-mail-paned-view.c
index ccbd6a2..d63109d 100644
--- a/mail/e-mail-paned-view.c
+++ b/mail/e-mail-paned-view.c
@@ -344,6 +344,20 @@ mail_paned_view_get_action_group (EMailReader *reader)
 	return E_SHELL_WINDOW_ACTION_GROUP_MAIL (shell_window);
 }
 
+static EMailBackend *
+mail_paned_view_get_backend (EMailReader *reader)
+{
+	EMailView *view;
+	EShellView *shell_view;
+	EShellBackend *shell_backend;
+
+	view = E_MAIL_VIEW (reader);
+	shell_view = e_mail_view_get_shell_view (view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	return E_MAIL_BACKEND (shell_backend);
+}
+
 static EMFormatHTML *
 mail_paned_view_get_formatter (EMailReader *reader)
 {
@@ -389,18 +403,6 @@ mail_paned_view_get_popup_menu (EMailReader *reader)
 	return GTK_MENU (widget);
 }
 
-static EShellBackend *
-mail_paned_view_get_shell_backend (EMailReader *reader)
-{
-	EMailView *view;
-	EShellView *shell_view;
-
-	view = E_MAIL_VIEW (reader);
-	shell_view = e_mail_view_get_shell_view (view);
-
-	return e_shell_view_get_shell_backend (shell_view);
-}
-
 static GtkWindow *
 mail_paned_view_get_window (EMailReader *reader)
 {
@@ -582,7 +584,7 @@ mail_paned_view_constructed (GObject *object)
 
 	container = widget;
 
-	widget = message_list_new (shell_backend);
+	widget = message_list_new (E_MAIL_BACKEND (shell_backend));
 	gtk_container_add (GTK_CONTAINER (container), widget);
 	priv->message_list = g_object_ref (widget);
 	gtk_widget_show (widget);
@@ -894,11 +896,11 @@ static void
 e_mail_paned_view_reader_init (EMailReaderInterface *interface)
 {
 	interface->get_action_group = mail_paned_view_get_action_group;
+	interface->get_backend = mail_paned_view_get_backend;
 	interface->get_formatter = mail_paned_view_get_formatter;
 	interface->get_hide_deleted = mail_paned_view_get_hide_deleted;
 	interface->get_message_list = mail_paned_view_get_message_list;
 	interface->get_popup_menu = mail_paned_view_get_popup_menu;
-	interface->get_shell_backend = mail_paned_view_get_shell_backend;
 	interface->get_window = mail_paned_view_get_window;
 	interface->set_folder = mail_paned_view_set_folder;
 	interface->show_search_bar = mail_paned_view_show_search_bar;
diff --git a/mail/e-mail-reader-utils.c b/mail/e-mail-reader-utils.c
index 32dcee6..44351c2 100644
--- a/mail/e-mail-reader-utils.c
+++ b/mail/e-mail-reader-utils.c
@@ -32,6 +32,7 @@
 #include "filter/e-filter-rule.h"
 #include "misc/e-web-view.h"
 
+#include "mail/e-mail-backend.h"
 #include "mail/e-mail-browser.h"
 #include "mail/em-composer-utils.h"
 #include "mail/em-format-html-print.h"
@@ -64,6 +65,7 @@ gboolean
 e_mail_reader_confirm_delete (EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	EShellSettings *shell_settings;
 	CamelFolder *folder;
@@ -80,10 +82,11 @@ e_mail_reader_confirm_delete (EMailReader *reader)
 
 	g_return_val_if_fail (E_IS_MAIL_READER (reader), FALSE);
 
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	window = e_mail_reader_get_window (reader);
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 	shell_settings = e_shell_get_shell_settings (shell);
 
@@ -131,6 +134,8 @@ void
 e_mail_reader_mark_as_read (EMailReader *reader,
                             const gchar *uid)
 {
+	EMailBackend *backend;
+	EMailSession *session;
 	EMFormatHTML *formatter;
 	CamelFolder *folder;
 	guint32 mask, set;
@@ -140,15 +145,18 @@ e_mail_reader_mark_as_read (EMailReader *reader,
 	g_return_if_fail (uid != NULL);
 
 	folder = e_mail_reader_get_folder (reader);
+	backend = e_mail_reader_get_backend (reader);
 	formatter = e_mail_reader_get_formatter (reader);
 
+	session = e_mail_backend_get_session (backend);
+
 	flags = camel_folder_get_message_flags (folder, uid);
 
 	if (!(flags & CAMEL_MESSAGE_SEEN)) {
 		CamelMimeMessage *message;
 
 		message = EM_FORMAT (formatter)->message;
-		em_utils_handle_receipt (folder, uid, message);
+		em_utils_handle_receipt (session, folder, uid, message);
 	}
 
 	mask = CAMEL_MESSAGE_SEEN;
@@ -213,6 +221,7 @@ guint
 e_mail_reader_open_selected (EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	CamelFolder *folder;
 	GtkWindow *window;
@@ -223,14 +232,15 @@ e_mail_reader_open_selected (EMailReader *reader)
 
 	g_return_val_if_fail (E_IS_MAIL_READER (reader), 0);
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
-	shell = e_shell_backend_get_shell (shell_backend);
-
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 	window = e_mail_reader_get_window (reader);
 
+	shell_backend = E_SHELL_BACKEND (backend);
+	shell = e_shell_backend_get_shell (shell_backend);
+
 	if (!em_utils_ask_open_many (window, uids->len)) {
 		em_utils_uids_free (uids);
 		return 0;
@@ -288,7 +298,7 @@ e_mail_reader_open_selected (EMailReader *reader)
 		const gchar *uid = views->pdata[ii];
 		GtkWidget *browser;
 
-		browser = e_mail_browser_new (shell_backend);
+		browser = e_mail_browser_new (backend);
 		e_mail_reader_set_folder (
 			E_MAIL_READER (browser), folder, folder_uri);
 		e_mail_reader_set_message (E_MAIL_READER (browser), uid);
@@ -387,6 +397,7 @@ e_mail_reader_reply_to_message (EMailReader *reader,
                                 gint reply_mode)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	EMFormatHTML *formatter;
 	GtkWidget *message_list;
@@ -404,15 +415,16 @@ e_mail_reader_reply_to_message (EMailReader *reader,
 
 	g_return_if_fail (E_IS_MAIL_READER (reader));
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
+	backend = e_mail_reader_get_backend (reader);
+	folder = e_mail_reader_get_folder (reader);
+	formatter = e_mail_reader_get_formatter (reader);
+	message_list = e_mail_reader_get_message_list (reader);
+
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 
-	formatter = e_mail_reader_get_formatter (reader);
 	web_view = em_format_html_get_web_view (formatter);
 
-	folder = e_mail_reader_get_folder (reader);
-	message_list = e_mail_reader_get_message_list (reader);
-
 	uid = MESSAGE_LIST (message_list)->cursor_uid;
 	g_return_if_fail (uid != NULL);
 
@@ -502,14 +514,19 @@ mail_reader_create_filter_cb (CamelFolder *folder,
                               gpointer user_data)
 {
 	struct {
+		EMailSession *session;
 		const gchar *source;
 		gint type;
 	} *filter_data = user_data;
 
 	if (message != NULL)
 		filter_gui_add_from_message (
-			message, filter_data->source, filter_data->type);
+			filter_data->session,
+			message,
+			filter_data->source,
+			filter_data->type);
 
+	g_object_unref (filter_data->session);
 	g_free (filter_data);
 }
 
@@ -517,18 +534,24 @@ void
 e_mail_reader_create_filter_from_selected (EMailReader *reader,
                                            gint filter_type)
 {
+	EMailBackend *backend;
+	EMailSession *session;
 	CamelFolder *folder;
 	const gchar *filter_source;
 	const gchar *folder_uri;
 	GPtrArray *uids;
 
 	struct {
+		EMailSession *session;
 		const gchar *source;
 		gint type;
 	} *filter_data;
 
 	g_return_if_fail (E_IS_MAIL_READER (reader));
 
+	backend = e_mail_reader_get_backend (reader);
+	session = e_mail_backend_get_session (backend);
+
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
 
@@ -543,6 +566,7 @@ e_mail_reader_create_filter_from_selected (EMailReader *reader,
 
 	if (uids->len == 1) {
 		filter_data = g_malloc (sizeof (*filter_data));
+		filter_data->session = g_object_ref (session);
 		filter_data->source = filter_source;
 		filter_data->type = filter_type;
 
@@ -563,14 +587,17 @@ mail_reader_create_vfolder_cb (CamelFolder *folder,
                                gpointer user_data)
 {
 	struct {
+		EMailSession *session;
 		gchar *uri;
 		gint type;
 	} *vfolder_data = user_data;
 
 	if (message != NULL)
 		vfolder_gui_add_from_message (
-			message, vfolder_data->type, vfolder_data->uri);
+			vfolder_data->session, message,
+			vfolder_data->type, vfolder_data->uri);
 
+	g_object_unref (vfolder_data->session);
 	g_free (vfolder_data->uri);
 	g_free (vfolder_data);
 }
@@ -579,23 +606,30 @@ void
 e_mail_reader_create_vfolder_from_selected (EMailReader *reader,
                                             gint vfolder_type)
 {
+	EMailBackend *backend;
+	EMailSession *session;
 	CamelFolder *folder;
 	const gchar *folder_uri;
 	GPtrArray *uids;
 
 	struct {
+		EMailSession *session;
 		gchar *uri;
 		gint type;
 	} *vfolder_data;
 
 	g_return_if_fail (E_IS_MAIL_READER (reader));
 
+	backend = e_mail_reader_get_backend (reader);
+	session = e_mail_backend_get_session (backend);
+
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
 	if (uids->len == 1) {
 		vfolder_data = g_malloc (sizeof (*vfolder_data));
+		vfolder_data->session = g_object_ref (session);
 		vfolder_data->uri = g_strdup (folder_uri);
 		vfolder_data->type = vfolder_type;
 
diff --git a/mail/e-mail-reader.c b/mail/e-mail-reader.c
index 7f2f8dc..0e28d06 100644
--- a/mail/e-mail-reader.c
+++ b/mail/e-mail-reader.c
@@ -38,6 +38,7 @@
 #include "widgets/misc/e-popup-action.h"
 #include "widgets/misc/e-menu-tool-action.h"
 
+#include "mail/e-mail-backend.h"
 #include "mail/e-mail-browser.h"
 #include "mail/e-mail-display.h"
 #include "mail/e-mail-reader-utils.h"
@@ -130,6 +131,7 @@ action_mail_add_sender_cb (GtkAction *action,
                            EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	CamelMessageInfo *info = NULL;
 	CamelFolder *folder;
@@ -137,7 +139,7 @@ action_mail_add_sender_cb (GtkAction *action,
 	const gchar *address;
 
 	folder = e_mail_reader_get_folder (reader);
-	shell_backend = e_mail_reader_get_shell_backend (reader);
+	backend = e_mail_reader_get_backend (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
 	if (uids->len != 1)
@@ -153,9 +155,11 @@ action_mail_add_sender_cb (GtkAction *action,
 
 	/* XXX EBookShellBackend should be listening for this
 	 *     event.  Kind of kludgey, but works for now. */
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 	e_shell_event (shell, "contact-quick-add-email", (gpointer) address);
 	emu_remove_from_mail_cache_1 (address);
+
 exit:
 	if (info)
 		camel_folder_free_message_info (folder, info);
@@ -167,6 +171,7 @@ action_add_to_address_book_cb (GtkAction *action,
                                EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	EMFormatHTML *formatter;
 	CamelInternetAddress *cia;
@@ -177,8 +182,8 @@ action_add_to_address_book_cb (GtkAction *action,
 
 	/* This action is defined in EMailDisplay. */
 
+	backend = e_mail_reader_get_backend (reader);
 	formatter = e_mail_reader_get_formatter (reader);
-	shell_backend = e_mail_reader_get_shell_backend (reader);
 
 	web_view = em_format_html_get_web_view (formatter);
 
@@ -201,6 +206,7 @@ action_add_to_address_book_cb (GtkAction *action,
 
 	/* XXX EBookShellBackend should be listening for this
 	 *     event.  Kind of kludgey, but works for now. */
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 	e_shell_event (shell, "contact-quick-add-email", email);
 	emu_remove_from_mail_cache_1 (curl->path);
@@ -234,13 +240,20 @@ static void
 action_mail_check_for_junk_cb (GtkAction *action,
                                EMailReader *reader)
 {
+	EMailBackend *backend;
+	EMailSession *session;
 	CamelFolder *folder;
 	GPtrArray *uids;
 
 	folder = e_mail_reader_get_folder (reader);
+	backend = e_mail_reader_get_backend (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
-	mail_filter_junk (folder, uids);
+	session = e_mail_backend_get_session (backend);
+
+	mail_filter_folder (
+		session, folder, uids,
+		E_FILTER_SOURCE_JUNKTEST, FALSE);
 }
 
 static void
@@ -248,17 +261,22 @@ action_mail_copy_cb (GtkAction *action,
                      EMailReader *reader)
 {
 	CamelFolder *folder;
+	EMailBackend *backend;
+	EMailSession *session;
 	GtkWidget *folder_tree;
 	GtkWidget *dialog;
 	GtkWindow *window;
 	GPtrArray *uids;
 	const gchar *uri;
 
+	backend = e_mail_reader_get_backend (reader);
+	session = e_mail_backend_get_session (backend);
+
 	folder = e_mail_reader_get_folder (reader);
 	window = e_mail_reader_get_window (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
-	folder_tree = em_folder_tree_new ();
+	folder_tree = em_folder_tree_new (session);
 	emu_restore_folder_tree_state (EM_FOLDER_TREE (folder_tree));
 
 	em_folder_tree_set_excluded (
@@ -287,7 +305,8 @@ action_mail_copy_cb (GtkAction *action,
 
 	if (uri != NULL) {
 		mail_transfer_messages (
-			folder, uids, FALSE, uri, 0, NULL, NULL);
+			session, folder, uids,
+			FALSE, uri, 0, NULL, NULL);
 		uids = NULL;
 	}
 
@@ -347,13 +366,20 @@ static void
 action_mail_filters_apply_cb (GtkAction *action,
                               EMailReader *reader)
 {
+	EMailBackend *backend;
+	EMailSession *session;
 	CamelFolder *folder;
 	GPtrArray *uids;
 
 	folder = e_mail_reader_get_folder (reader);
+	backend = e_mail_reader_get_backend (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
-	mail_filter_on_demand (folder, uids);
+	session = e_mail_backend_get_session (backend);
+
+	mail_filter_folder (
+		session, folder, uids,
+		E_FILTER_SOURCE_DEMAND, FALSE);
 }
 
 static void
@@ -468,15 +494,14 @@ action_mail_forward_cb (GtkAction *action,
                         EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	CamelFolder *folder;
 	GtkWindow *window;
 	GPtrArray *uids;
 	const gchar *folder_uri;
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
-	shell = e_shell_backend_get_shell (shell_backend);
-
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
@@ -484,6 +509,9 @@ action_mail_forward_cb (GtkAction *action,
 
 	g_return_if_fail (uids != NULL);
 
+	shell_backend = E_SHELL_BACKEND (backend);
+	shell = e_shell_backend_get_shell (shell_backend);
+
 	if (em_utils_ask_open_many (window, uids->len))
 		em_utils_forward_messages (shell, folder, uids, folder_uri);
 	else
@@ -497,15 +525,14 @@ action_mail_forward_attached_cb (GtkAction *action,
                                  EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	CamelFolder *folder;
 	GtkWindow *window;
 	GPtrArray *uids;
 	const gchar *folder_uri;
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
-	shell = e_shell_backend_get_shell (shell_backend);
-
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
@@ -513,6 +540,9 @@ action_mail_forward_attached_cb (GtkAction *action,
 
 	g_return_if_fail (uids != NULL);
 
+	shell_backend = E_SHELL_BACKEND (backend);
+	shell = e_shell_backend_get_shell (shell_backend);
+
 	if (em_utils_ask_open_many (window, uids->len))
 		em_utils_forward_attached (shell, folder, uids, folder_uri);
 	else
@@ -526,15 +556,14 @@ action_mail_forward_inline_cb (GtkAction *action,
                                EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	CamelFolder *folder;
 	GtkWindow *window;
 	GPtrArray *uids;
 	const gchar *folder_uri;
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
-	shell = e_shell_backend_get_shell (shell_backend);
-
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
@@ -542,6 +571,9 @@ action_mail_forward_inline_cb (GtkAction *action,
 
 	g_return_if_fail (uids != NULL);
 
+	shell_backend = E_SHELL_BACKEND (backend);
+	shell = e_shell_backend_get_shell (shell_backend);
+
 	if (em_utils_ask_open_many (window, uids->len))
 		em_utils_forward_inline (shell, folder, uids, folder_uri);
 	else
@@ -555,15 +587,14 @@ action_mail_forward_quoted_cb (GtkAction *action,
                                EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	CamelFolder *folder;
 	GtkWindow *window;
 	GPtrArray *uids;
 	const gchar *folder_uri;
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
-	shell = e_shell_backend_get_shell (shell_backend);
-
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
@@ -571,6 +602,9 @@ action_mail_forward_quoted_cb (GtkAction *action,
 
 	g_return_if_fail (uids != NULL);
 
+	shell_backend = E_SHELL_BACKEND (backend);
+	shell = e_shell_backend_get_shell (shell_backend);
+
 	if (em_utils_ask_open_many (window, uids->len))
 		em_utils_forward_quoted (shell, folder, uids, folder_uri);
 	else
@@ -677,16 +711,18 @@ action_mail_message_edit_cb (GtkAction *action,
                              EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	CamelFolder *folder;
 	GPtrArray *uids;
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
-	shell = e_shell_backend_get_shell (shell_backend);
-
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
+	shell_backend = E_SHELL_BACKEND (backend);
+	shell = e_shell_backend_get_shell (shell_backend);
+
 	em_utils_edit_messages (shell, folder, uids, FALSE);
 }
 
@@ -695,14 +731,16 @@ action_mail_message_new_cb (GtkAction *action,
                             EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	const gchar *folder_uri;
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
-	shell = e_shell_backend_get_shell (shell_backend);
-
+	backend = e_mail_reader_get_backend (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
 
+	shell_backend = E_SHELL_BACKEND (backend);
+	shell = e_shell_backend_get_shell (shell_backend);
+
 	em_utils_compose_new_message (shell, folder_uri);
 }
 
@@ -718,17 +756,22 @@ action_mail_move_cb (GtkAction *action,
                      EMailReader *reader)
 {
 	CamelFolder *folder;
+	EMailBackend *backend;
+	EMailSession *session;
 	GtkWidget *folder_tree;
 	GtkWidget *dialog;
 	GtkWindow *window;
 	GPtrArray *uids;
 	const gchar *uri;
 
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 	window = e_mail_reader_get_window (reader);
 
-	folder_tree = em_folder_tree_new ();
+	session = e_mail_backend_get_session (backend);
+
+	folder_tree = em_folder_tree_new (session);
 	emu_restore_folder_tree_state (EM_FOLDER_TREE (folder_tree));
 
 	em_folder_tree_set_excluded (
@@ -757,7 +800,8 @@ action_mail_move_cb (GtkAction *action,
 
 	if (uri != NULL) {
 		mail_transfer_messages (
-			folder, uids, TRUE, uri, 0, NULL, NULL);
+			session, folder, uids,
+			TRUE, uri, 0, NULL, NULL);
 		uids = NULL;
 	}
 
@@ -770,7 +814,7 @@ exit:
 
 static void
 action_mail_folder_cb (GtkAction *action,
-                     EMailReader *reader)
+                       EMailReader *reader)
 {
 	g_signal_emit (reader, signals[SHOW_FOLDER], 0);
 }
@@ -953,20 +997,22 @@ action_mail_redirect_cb (GtkAction *action,
                          EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	GtkWidget *message_list;
 	CamelFolder *folder;
 	const gchar *uid;
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
-	shell = e_shell_backend_get_shell (shell_backend);
-
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	message_list = e_mail_reader_get_message_list (reader);
 
 	uid = MESSAGE_LIST (message_list)->cursor_uid;
 	g_return_if_fail (uid != NULL);
 
+	shell_backend = E_SHELL_BACKEND (backend);
+	shell = e_shell_backend_get_shell (shell_backend);
+
 	em_utils_redirect_message_by_uid (shell, folder, uid);
 	check_close_browser_reader (reader);
 }
@@ -1248,6 +1294,7 @@ action_mail_save_as_cb (GtkAction *action,
                         EMailReader *reader)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	CamelMessageInfo *info;
 	CamelFolder *folder;
@@ -1258,7 +1305,7 @@ action_mail_save_as_cb (GtkAction *action,
 	gchar *uri;
 
 	folder = e_mail_reader_get_folder (reader);
-	shell_backend = e_mail_reader_get_shell_backend (reader);
+	backend = e_mail_reader_get_backend (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
 	g_return_if_fail (uids->len > 0);
@@ -1288,7 +1335,9 @@ action_mail_save_as_cb (GtkAction *action,
 		suggestion = g_strconcat (basename, ".mbox", NULL);
 	}
 
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
+
 	file = e_shell_run_save_dialog (
 		shell, title, suggestion,
 		"*.mbox:application/mbox,message/rfc822", NULL, NULL);
@@ -1360,21 +1409,21 @@ static void
 action_mail_show_source_cb (GtkAction *action,
                             EMailReader *reader)
 {
+	EMailBackend *backend;
 	EMFormatHTML *formatter;
-	EShellBackend *shell_backend;
 	CamelFolder *folder;
 	GtkWidget *browser;
 	GPtrArray *uids;
 	const gchar *folder_uri;
 
+	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
-	shell_backend = e_mail_reader_get_shell_backend (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
 	g_return_if_fail (uids->len > 0);
 
-	browser = e_mail_browser_new (shell_backend);
+	browser = e_mail_browser_new (backend);
 	reader = E_MAIL_READER (browser);
 	formatter = e_mail_reader_get_formatter (reader);
 
@@ -1474,6 +1523,8 @@ static void
 action_search_folder_recipient_cb (GtkAction *action,
                                    EMailReader *reader)
 {
+	EMailBackend *backend;
+	EMailSession *session;
 	EMFormatHTML *formatter;
 	EWebView *web_view;
 	CamelURL *curl;
@@ -1493,15 +1544,19 @@ action_search_folder_recipient_cb (GtkAction *action,
 	curl = camel_url_new (uri, NULL);
 	g_return_if_fail (curl != NULL);
 
+	backend = e_mail_reader_get_backend (reader);
+	session = e_mail_backend_get_session (backend);
+
 	if (curl->path != NULL && *curl->path != '\0') {
 		CamelInternetAddress *inet_addr;
 
 		/* Ensure vfolder is running. */
-		vfolder_load_storage ();
+		vfolder_load_storage (session);
 
 		inet_addr = camel_internet_address_new ();
 		camel_address_decode (CAMEL_ADDRESS (inet_addr), curl->path);
-		vfolder_gui_add_from_address (inet_addr, AUTO_TO, folder_uri);
+		vfolder_gui_add_from_address (
+			session, inet_addr, AUTO_TO, folder_uri);
 		g_object_unref (inet_addr);
 	}
 
@@ -1512,6 +1567,8 @@ static void
 action_search_folder_sender_cb (GtkAction *action,
                                 EMailReader *reader)
 {
+	EMailBackend *backend;
+	EMailSession *session;
 	EMFormatHTML *formatter;
 	EWebView *web_view;
 	CamelURL *curl;
@@ -1531,15 +1588,19 @@ action_search_folder_sender_cb (GtkAction *action,
 	curl = camel_url_new (uri, NULL);
 	g_return_if_fail (curl != NULL);
 
+	backend = e_mail_reader_get_backend (reader);
+	session = e_mail_backend_get_session (backend);
+
 	if (curl->path != NULL && *curl->path != '\0') {
 		CamelInternetAddress *inet_addr;
 
 		/* Ensure vfolder is running. */
-		vfolder_load_storage ();
+		vfolder_load_storage (session);
 
 		inet_addr = camel_internet_address_new ();
 		camel_address_decode (CAMEL_ADDRESS (inet_addr), curl->path);
-		vfolder_gui_add_from_address (inet_addr, AUTO_FROM, folder_uri);
+		vfolder_gui_add_from_address (
+			session, inet_addr, AUTO_FROM, folder_uri);
 		g_object_unref (inet_addr);
 	}
 
@@ -2242,6 +2303,7 @@ mail_reader_message_loaded_cb (CamelFolder *folder,
 	EMailReaderPrivate *priv;
 	EMFormatHTML *formatter;
 	GtkWidget *message_list;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	EShellSettings *shell_settings;
 	EShell *shell;
@@ -2262,10 +2324,11 @@ mail_reader_message_loaded_cb (CamelFolder *folder,
 		return;
 	}
 
+	backend = e_mail_reader_get_backend (reader);
 	formatter = e_mail_reader_get_formatter (reader);
 	message_list = e_mail_reader_get_message_list (reader);
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 	shell_settings = e_shell_get_shell_settings (shell);
 
@@ -2579,6 +2642,7 @@ mail_reader_update_actions (EMailReader *reader,
                             guint32 state)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	EShellSettings *shell_settings;
 	GtkAction *action;
@@ -2607,7 +2671,9 @@ mail_reader_update_actions (EMailReader *reader,
 
 	priv = E_MAIL_READER_GET_PRIVATE (reader);
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
+	backend = e_mail_reader_get_backend (reader);
+
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 	shell_settings = e_shell_get_shell_settings (shell);
 
@@ -3071,6 +3137,7 @@ e_mail_reader_init (EMailReader *reader,
                     gboolean connect_signals)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellBackend *shell_backend;
 	EShellSettings *shell_settings;
 	EMFormatHTML *formatter;
@@ -3086,10 +3153,11 @@ e_mail_reader_init (EMailReader *reader,
 
 	g_return_if_fail (E_IS_MAIL_READER (reader));
 
+	backend = e_mail_reader_get_backend (reader);
 	formatter = e_mail_reader_get_formatter (reader);
 	message_list = e_mail_reader_get_message_list (reader);
-	shell_backend = e_mail_reader_get_shell_backend (reader);
 
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 	shell_settings = e_shell_get_shell_settings (shell);
 
@@ -3498,6 +3566,19 @@ e_mail_reader_get_action_group (EMailReader *reader)
 	return interface->get_action_group (reader);
 }
 
+EMailBackend *
+e_mail_reader_get_backend (EMailReader *reader)
+{
+	EMailReaderInterface *interface;
+
+	g_return_val_if_fail (E_IS_MAIL_READER (reader), NULL);
+
+	interface = E_MAIL_READER_GET_INTERFACE (reader);
+	g_return_val_if_fail (interface->get_backend != NULL, NULL);
+
+	return interface->get_backend (reader);
+}
+
 EMFormatHTML *
 e_mail_reader_get_formatter (EMailReader *reader)
 {
@@ -3563,19 +3644,6 @@ e_mail_reader_get_selected_uids (EMailReader *reader)
 	return interface->get_selected_uids (reader);
 }
 
-EShellBackend *
-e_mail_reader_get_shell_backend (EMailReader *reader)
-{
-	EMailReaderInterface *interface;
-
-	g_return_val_if_fail (E_IS_MAIL_READER (reader), NULL);
-
-	interface = E_MAIL_READER_GET_INTERFACE (reader);
-	g_return_val_if_fail (interface->get_shell_backend != NULL, NULL);
-
-	return interface->get_shell_backend (reader);
-}
-
 GtkWindow *
 e_mail_reader_get_window (EMailReader *reader)
 {
@@ -3645,12 +3713,18 @@ void
 e_mail_reader_set_folder_uri (EMailReader *reader,
                               const gchar *folder_uri)
 {
+	EMailBackend *backend;
+	EMailSession *session;
+
 	g_return_if_fail (E_IS_MAIL_READER (reader));
 	g_return_if_fail (folder_uri != NULL);
 
+	backend = e_mail_reader_get_backend (reader);
+	session = e_mail_backend_get_session (backend);
+
 	/* Fetch the CamelFolder asynchronously. */
 	mail_get_folder (
-		folder_uri, 0, mail_reader_got_folder_cb,
+		session, folder_uri, 0, mail_reader_got_folder_cb,
 		reader, mail_msg_fast_ordered_push);
 }
 
diff --git a/mail/e-mail-reader.h b/mail/e-mail-reader.h
index b93b411..c0c954c 100644
--- a/mail/e-mail-reader.h
+++ b/mail/e-mail-reader.h
@@ -29,8 +29,8 @@
 
 #include <gtk/gtk.h>
 #include <camel/camel.h>
+#include <mail/e-mail-backend.h>
 #include <mail/em-format-html.h>
-#include <shell/e-shell-backend.h>
 
 /* Standard GObject macros */
 #define E_TYPE_MAIL_READER \
@@ -84,12 +84,12 @@ struct _EMailReaderInterface {
 
 	GtkActionGroup *
 			(*get_action_group)	(EMailReader *reader);
+	EMailBackend *	(*get_backend)		(EMailReader *reader);
 	EMFormatHTML *	(*get_formatter)	(EMailReader *reader);
 	gboolean	(*get_hide_deleted)	(EMailReader *reader);
 	GtkWidget *	(*get_message_list)	(EMailReader *reader);
 	GtkMenu *	(*get_popup_menu)	(EMailReader *reader);
 	GPtrArray *	(*get_selected_uids)	(EMailReader *reader);
-	EShellBackend *	(*get_shell_backend)	(EMailReader *reader);
 	GtkWindow *	(*get_window)		(EMailReader *reader);
 
 	CamelFolder *	(*get_folder)		(EMailReader *reader);
@@ -120,13 +120,14 @@ GtkAction *	e_mail_reader_get_action	(EMailReader *reader,
 						 const gchar *action_name);
 GtkActionGroup *
 		e_mail_reader_get_action_group	(EMailReader *reader);
+EMailBackend *	e_mail_reader_get_backend	(EMailReader *reader);
 EMFormatHTML *	e_mail_reader_get_formatter	(EMailReader *reader);
 gboolean	e_mail_reader_get_hide_deleted	(EMailReader *reader);
 GtkWidget *	e_mail_reader_get_message_list	(EMailReader *reader);
-guint		e_mail_reader_open_selected_mail (EMailReader *reader);
+guint		e_mail_reader_open_selected_mail
+						(EMailReader *reader);
 GtkMenu *	e_mail_reader_get_popup_menu	(EMailReader *reader);
 GPtrArray *	e_mail_reader_get_selected_uids	(EMailReader *reader);
-EShellBackend *	e_mail_reader_get_shell_backend	(EMailReader *reader);
 GtkWindow *	e_mail_reader_get_window	(EMailReader *reader);
 CamelFolder *	e_mail_reader_get_folder	(EMailReader *reader);
 const gchar *	e_mail_reader_get_folder_uri	(EMailReader *reader);
@@ -149,7 +150,8 @@ void		e_mail_reader_create_charset_menu
 void		e_mail_reader_show_search_bar	(EMailReader *reader);
 gboolean	e_mail_reader_get_enable_show_folder
 						(EMailReader *reader);
-void		e_mail_reader_enable_show_folder (EMailReader *reader);
+void		e_mail_reader_enable_show_folder
+						(EMailReader *reader);
 
 G_END_DECLS
 
diff --git a/mail/e-mail-session.c b/mail/e-mail-session.c
new file mode 100644
index 0000000..14213bf
--- /dev/null
+++ b/mail/e-mail-session.c
@@ -0,0 +1,1376 @@
+/*
+ * e-mail-session.c
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ *
+ * Authors:
+ *   Jonathon Jongsma <jonathon jongsma collabora co uk>
+ *
+ * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
+ * Copyright (C) 2009 Intel Corporation
+ *
+ */
+
+/* mail-session.c: handles the session information and resource manipulation */
+
+#include <config.h>
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <glib/gstdio.h>
+
+#include <gtk/gtk.h>
+
+#include <gconf/gconf-client.h>
+
+#ifdef HAVE_CANBERRA
+#include <canberra-gtk.h>
+#endif
+
+#include <libedataserverui/e-passwords.h>
+#include <libedataserver/e-flag.h>
+
+#include "e-util/e-util.h"
+#include "e-util/e-alert-dialog.h"
+#include "e-util/e-util-private.h"
+
+#include "e-mail-local.h"
+#include "e-mail-session.h"
+#include "em-composer-utils.h"
+#include "em-filter-context.h"
+#include "em-filter-rule.h"
+#include "em-utils.h"
+#include "mail-config.h"
+#include "mail-folder-cache.h"
+#include "mail-mt.h"
+#include "mail-ops.h"
+#include "mail-send-recv.h"
+#include "mail-tools.h"
+
+#define E_MAIL_SESSION_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE \
+	((obj), E_TYPE_MAIL_SESSION, EMailSessionPrivate))
+
+static guint session_check_junk_notify_id;
+static guint session_gconf_proxy_id;
+
+typedef struct _AsyncContext AsyncContext;
+
+struct _EMailSessionPrivate {
+	FILE *filter_logfile;
+	GList *junk_plugins;
+};
+
+struct _AsyncContext {
+	/* arguments */
+	CamelStoreGetFolderFlags flags;
+	gchar *uri;
+
+	/* results */
+	CamelFolder *folder;
+};
+
+static gchar *mail_data_dir;
+static gchar *mail_config_dir;
+
+static MailMsgInfo ms_thread_info_dummy = { sizeof (MailMsg) };
+
+G_DEFINE_TYPE (
+	EMailSession,
+	e_mail_session,
+	CAMEL_TYPE_SESSION)
+
+/* Support for CamelSession.alert_user() *************************************/
+
+static gpointer user_message_dialog;
+static GQueue user_message_queue = { NULL, NULL, 0 };
+
+struct _user_message_msg {
+	MailMsg base;
+
+	CamelSessionAlertType type;
+	gchar *prompt;
+	EFlag *done;
+
+	guint allow_cancel:1;
+	guint result:1;
+	guint ismain:1;
+};
+
+static void user_message_exec (struct _user_message_msg *m);
+
+static void
+user_message_response_free (GtkDialog *dialog,
+                            gint button,
+                            struct _user_message_msg *m)
+{
+	gtk_widget_destroy ((GtkWidget *) dialog);
+
+	user_message_dialog = NULL;
+
+	/* check for pendings */
+	if (!g_queue_is_empty (&user_message_queue)) {
+		m = g_queue_pop_head (&user_message_queue);
+		user_message_exec (m);
+		mail_msg_unref (m);
+	}
+}
+
+/* clicked, send back the reply */
+static void
+user_message_response (GtkDialog *dialog,
+                       gint button,
+                       struct _user_message_msg *m)
+{
+	/* if !allow_cancel, then we've already replied */
+	if (m->allow_cancel) {
+		m->result = button == GTK_RESPONSE_OK;
+		e_flag_set (m->done);
+	}
+
+	user_message_response_free (dialog, button, m);
+}
+
+static void
+user_message_exec (struct _user_message_msg *m)
+{
+	GtkWindow *parent;
+	const gchar *error_type;
+
+	if (!m->ismain && user_message_dialog != NULL) {
+		g_queue_push_tail (&user_message_queue, mail_msg_ref (m));
+		return;
+	}
+
+	switch (m->type) {
+		case CAMEL_SESSION_ALERT_INFO:
+			error_type = m->allow_cancel ?
+				"mail:session-message-info-cancel" :
+				"mail:session-message-info";
+			break;
+		case CAMEL_SESSION_ALERT_WARNING:
+			error_type = m->allow_cancel ?
+				"mail:session-message-warning-cancel" :
+				"mail:session-message-warning";
+			break;
+		case CAMEL_SESSION_ALERT_ERROR:
+			error_type = m->allow_cancel ?
+				"mail:session-message-error-cancel" :
+				"mail:session-message-error";
+			break;
+		default:
+			error_type = NULL;
+			g_return_if_reached ();
+	}
+
+	/* Pull in the active window from the shell to get a parent window */
+	parent = e_shell_get_active_window (e_shell_get_default ());
+	user_message_dialog = e_alert_dialog_new_for_args (
+		parent, error_type, m->prompt, NULL);
+	g_object_set (
+		user_message_dialog, "allow_shrink", TRUE,
+		"allow_grow", TRUE, NULL);
+
+	/* Use the number of dialog buttons as a heuristic for whether to
+	 * emit a status bar message or present the dialog immediately, the
+	 * thought being if there's more than one button then something is
+	 * probably blocked until the user responds. */
+	if (e_alert_dialog_count_buttons (user_message_dialog) > 1) {
+		if (m->ismain) {
+			gint response;
+
+			response = gtk_dialog_run (user_message_dialog);
+			user_message_response (
+				user_message_dialog, response, m);
+		} else {
+			g_signal_connect (
+				user_message_dialog, "response",
+				G_CALLBACK (user_message_response), m);
+			gtk_widget_show (user_message_dialog);
+		}
+	} else {
+		g_signal_connect (
+			user_message_dialog, "response",
+			G_CALLBACK (user_message_response_free), m);
+		g_object_set_data (
+			user_message_dialog, "response-handled",
+			GINT_TO_POINTER (TRUE));
+		em_utils_show_error_silent (user_message_dialog);
+	}
+}
+
+static void
+user_message_free (struct _user_message_msg *m)
+{
+	g_free (m->prompt);
+	e_flag_free (m->done);
+}
+
+static MailMsgInfo user_message_info = {
+	sizeof (struct _user_message_msg),
+	(MailMsgDescFunc) NULL,
+	(MailMsgExecFunc) user_message_exec,
+	(MailMsgDoneFunc) NULL,
+	(MailMsgFreeFunc) user_message_free
+};
+
+/* Support for CamelSession.get_filter_driver () *****************************/
+
+static CamelFolder *
+get_folder (CamelFilterDriver *d,
+            const gchar *uri,
+            gpointer user_data,
+            GError **error)
+{
+	EMailSession *session = E_MAIL_SESSION (user_data);
+
+	/* FIXME Not passing a GCancellable here. */
+	/* FIXME Need a camel_filter_driver_get_session(). */
+	return e_mail_session_uri_to_folder_sync (
+		session, uri, 0, NULL, error);
+}
+
+static gboolean
+session_play_sound_cb (const gchar *filename)
+{
+#ifdef HAVE_CANBERRA
+	if (filename != NULL && *filename != '\0')
+		ca_context_play (
+			ca_gtk_context_get (), 0,
+			CA_PROP_MEDIA_FILENAME, filename,
+			NULL);
+	else
+#endif
+		gdk_beep ();
+
+	return FALSE;
+}
+
+static void
+session_play_sound (CamelFilterDriver *driver,
+                    const gchar *filename,
+                    gpointer user_data)
+{
+	g_idle_add_full (
+		G_PRIORITY_DEFAULT_IDLE,
+		(GSourceFunc) session_play_sound_cb,
+		g_strdup (filename), (GDestroyNotify) g_free);
+}
+
+static void
+session_system_beep (CamelFilterDriver *driver,
+                     gpointer user_data)
+{
+	g_idle_add ((GSourceFunc) session_play_sound_cb, NULL);
+}
+
+static CamelFilterDriver *
+main_get_filter_driver (CamelSession *session,
+                        const gchar *type,
+                        GError **error)
+{
+	EMailSession *ms = E_MAIL_SESSION (session);
+	CamelFilterDriver *driver;
+	EFilterRule *rule = NULL;
+	const gchar *config_dir;
+	gchar *user, *system;
+	GConfClient *gconf;
+	ERuleContext *fc;
+
+	gconf = mail_config_get_gconf_client ();
+
+	config_dir = mail_session_get_config_dir ();
+	user = g_build_filename (config_dir, "filters.xml", NULL);
+	system = g_build_filename (EVOLUTION_PRIVDATADIR, "filtertypes.xml", NULL);
+	fc = (ERuleContext *) em_filter_context_new (ms);
+	e_rule_context_load (fc, system, user);
+	g_free (system);
+	g_free (user);
+
+	driver = camel_filter_driver_new (session);
+	camel_filter_driver_set_folder_func (driver, get_folder, session);
+
+	if (gconf_client_get_bool (gconf, "/apps/evolution/mail/filters/log", NULL)) {
+		if (ms->priv->filter_logfile == NULL) {
+			gchar *filename;
+
+			filename = gconf_client_get_string (gconf, "/apps/evolution/mail/filters/logfile", NULL);
+			if (filename) {
+				ms->priv->filter_logfile = g_fopen (filename, "a+");
+				g_free (filename);
+			}
+		}
+
+		if (ms->priv->filter_logfile)
+			camel_filter_driver_set_logfile (driver, ms->priv->filter_logfile);
+	}
+
+	camel_filter_driver_set_shell_func (driver, mail_execute_shell_command, NULL);
+	camel_filter_driver_set_play_sound_func (driver, session_play_sound, NULL);
+	camel_filter_driver_set_system_beep_func (driver, session_system_beep, NULL);
+
+	if ((!strcmp (type, E_FILTER_SOURCE_INCOMING) || !strcmp (type, E_FILTER_SOURCE_JUNKTEST))
+	    && camel_session_get_check_junk (session)) {
+		/* implicit junk check as 1st rule */
+		camel_filter_driver_add_rule (driver, "Junk check", "(junk-test)", "(begin (set-system-flag \"junk\"))");
+	}
+
+	if (strcmp (type, E_FILTER_SOURCE_JUNKTEST) != 0) {
+		GString *fsearch, *faction;
+
+		fsearch = g_string_new ("");
+		faction = g_string_new ("");
+
+		if (!strcmp (type, E_FILTER_SOURCE_DEMAND))
+			type = E_FILTER_SOURCE_INCOMING;
+
+		/* add the user-defined rules next */
+		while ((rule = e_rule_context_next_rule (fc, rule, type))) {
+			g_string_truncate (fsearch, 0);
+			g_string_truncate (faction, 0);
+
+			/* skip disabled rules */
+			if (!rule->enabled)
+				continue;
+
+			e_filter_rule_build_code (rule, fsearch);
+			em_filter_rule_build_action ((EMFilterRule *) rule, faction);
+			camel_filter_driver_add_rule (driver, rule->name, fsearch->str, faction->str);
+		}
+
+		g_string_free (fsearch, TRUE);
+		g_string_free (faction, TRUE);
+	}
+
+	g_object_unref (fc);
+
+	return driver;
+}
+
+/* Support for CamelSession.forward_to () ************************************/
+
+static guint preparing_flush = 0;
+
+static gboolean
+forward_to_flush_outbox_cb (EMailSession *session)
+{
+	g_return_val_if_fail (preparing_flush != 0, FALSE);
+
+	preparing_flush = 0;
+	mail_send (session);
+
+	return FALSE;
+}
+
+static void
+ms_forward_to_cb (CamelFolder *folder,
+                  CamelMimeMessage *msg,
+                  CamelMessageInfo *info,
+                  gint queued,
+                  const gchar *appended_uid,
+                  gpointer data)
+{
+	EMailSession *session = E_MAIL_SESSION (data);
+
+	camel_message_info_free (info);
+
+	/* do not call mail send immediately, just pile them all in the outbox */
+	if (preparing_flush ||
+	    gconf_client_get_bool (mail_config_get_gconf_client (), "/apps/evolution/mail/filters/flush-outbox", NULL)) {
+		if (preparing_flush)
+			g_source_remove (preparing_flush);
+
+		preparing_flush = g_timeout_add_seconds (
+			60, (GSourceFunc)
+			forward_to_flush_outbox_cb, session);
+	}
+}
+
+/* Support for SOCKS proxy ***************************************************/
+
+#define DIR_PROXY "/system/proxy"
+#define MODE_PROXY "/system/proxy/mode"
+#define KEY_SOCKS_HOST "/system/proxy/socks_host"
+#define KEY_SOCKS_PORT "/system/proxy/socks_port"
+
+static void
+set_socks_proxy_from_gconf (CamelSession *session)
+{
+	GConfClient *client;
+	gchar *mode, *host;
+	gint port;
+
+	client = mail_config_get_gconf_client ();
+
+	mode = gconf_client_get_string (client, MODE_PROXY, NULL);
+	if (!g_strcmp0(mode, "manual")) {
+		host = gconf_client_get_string (client, KEY_SOCKS_HOST, NULL); /* NULL-GError */
+		port = gconf_client_get_int (client, KEY_SOCKS_PORT, NULL); /* NULL-GError */
+		camel_session_set_socks_proxy (session, host, port);
+		g_free (host);
+	}
+	g_free (mode);
+}
+
+static void
+proxy_gconf_notify_cb (GConfClient* client,
+                       guint cnxn_id,
+                       GConfEntry *entry,
+                       gpointer user_data)
+{
+	CamelSession *session = CAMEL_SESSION (user_data);
+	const gchar *key;
+
+	key = gconf_entry_get_key (entry);
+
+	if (strcmp (entry->key, KEY_SOCKS_HOST) == 0
+	    || strcmp (entry->key, KEY_SOCKS_PORT) == 0)
+		set_socks_proxy_from_gconf (session);
+}
+
+static void
+set_socks_proxy_gconf_watch (CamelSession *session)
+{
+	GConfClient *client;
+
+	client = mail_config_get_gconf_client ();
+
+	gconf_client_add_dir (
+		client, DIR_PROXY,
+		GCONF_CLIENT_PRELOAD_ONELEVEL, NULL); /* NULL-GError */
+	session_gconf_proxy_id = gconf_client_notify_add (
+		client, DIR_PROXY, proxy_gconf_notify_cb,
+		session, NULL, NULL); /* NULL-GError */
+}
+
+static void
+init_socks_proxy (CamelSession *session)
+{
+	g_return_if_fail (CAMEL_IS_SESSION (session));
+
+	set_socks_proxy_gconf_watch (session);
+	set_socks_proxy_from_gconf (session);
+}
+
+/*****************************************************************************/
+
+static void
+async_context_free (AsyncContext *context)
+{
+	if (context->folder != NULL)
+		g_object_unref (context->folder);
+
+	g_free (context->uri);
+
+	g_slice_free (AsyncContext, context);
+}
+
+static gchar *
+mail_session_make_key (CamelService *service,
+                       const gchar *item)
+{
+	gchar *key;
+
+	if (service != NULL)
+		key = camel_url_to_string (
+			service->url,
+			CAMEL_URL_HIDE_PASSWORD |
+			CAMEL_URL_HIDE_PARAMS);
+	else
+		key = g_strdup (item);
+
+	return key;
+}
+
+static void
+mail_session_check_junk_notify (GConfClient *gconf,
+                                guint id,
+                                GConfEntry *entry,
+                                CamelSession *session)
+{
+	gchar *key;
+
+	g_return_if_fail (gconf_entry_get_key (entry) != NULL);
+	g_return_if_fail (gconf_entry_get_value (entry) != NULL);
+
+	key = strrchr (gconf_entry_get_key (entry), '/');
+	if (key) {
+		key++;
+		if (strcmp (key, "check_incoming") == 0)
+			camel_session_set_check_junk (
+				session, gconf_value_get_bool (
+				gconf_entry_get_value (entry)));
+	}
+}
+
+static void
+mail_session_finalize (GObject *object)
+{
+	GConfClient *client;
+
+	client = mail_config_get_gconf_client ();
+
+	if (session_check_junk_notify_id != 0) {
+		gconf_client_notify_remove (client, session_check_junk_notify_id);
+		session_check_junk_notify_id = 0;
+	}
+
+	if (session_gconf_proxy_id != 0) {
+		gconf_client_notify_remove (client, session_gconf_proxy_id);
+		session_gconf_proxy_id = 0;
+	}
+
+	g_free (mail_data_dir);
+	g_free (mail_config_dir);
+
+	/* Chain up to parent's finalize() method. */
+	G_OBJECT_CLASS (e_mail_session_parent_class)->finalize (object);
+}
+
+static gchar *
+mail_session_get_password (CamelSession *session,
+                           CamelService *service,
+                           const gchar *domain,
+                           const gchar *prompt,
+                           const gchar *item,
+                           guint32 flags,
+                           GError **error)
+{
+	gchar *url;
+	gchar *ret = NULL;
+	EAccount *account = NULL;
+
+	url = service?camel_url_to_string (service->url, CAMEL_URL_HIDE_ALL):NULL;
+
+	if (!strcmp(item, "popb4smtp_uri")) {
+		/* not 100% mt safe, but should be ok */
+		if (url
+		    && (account = mail_config_get_account_by_transport_url (url)))
+			ret = g_strdup (account->source->url);
+		else
+			ret = g_strdup (url);
+	} else {
+		gchar *key = mail_session_make_key (service, item);
+		EAccountService *config_service = NULL;
+
+		if (domain == NULL)
+			domain = "Mail";
+
+		ret = e_passwords_get_password (domain, key);
+		if (ret == NULL || (flags & CAMEL_SESSION_PASSWORD_REPROMPT)) {
+			gboolean remember;
+
+			if (url) {
+				if  ((account = mail_config_get_account_by_source_url (url)))
+					config_service = account->source;
+				else if ((account = mail_config_get_account_by_transport_url (url)))
+					config_service = account->transport;
+			}
+
+			remember = config_service?config_service->save_passwd:FALSE;
+
+			if (!config_service || (config_service && !config_service->get_password_canceled)) {
+				guint32 eflags;
+				gchar *title;
+
+				if (flags & CAMEL_SESSION_PASSPHRASE) {
+					if (account)
+						title = g_strdup_printf (_("Enter Passphrase for %s"), account->name);
+					else
+						title = g_strdup (_("Enter Passphrase"));
+				} else {
+					if (account)
+						title = g_strdup_printf (_("Enter Password for %s"), account->name);
+					else
+						title = g_strdup (_("Enter Password"));
+				}
+				if ((flags & CAMEL_SESSION_PASSWORD_STATIC) != 0)
+					eflags = E_PASSWORDS_REMEMBER_NEVER;
+				else if (config_service == NULL)
+					eflags = E_PASSWORDS_REMEMBER_SESSION;
+				else
+					eflags = E_PASSWORDS_REMEMBER_FOREVER;
+
+				if (flags & CAMEL_SESSION_PASSWORD_REPROMPT)
+					eflags |= E_PASSWORDS_REPROMPT;
+
+				if (flags & CAMEL_SESSION_PASSWORD_SECRET)
+					eflags |= E_PASSWORDS_SECRET;
+
+				if (flags & CAMEL_SESSION_PASSPHRASE)
+					eflags |= E_PASSWORDS_PASSPHRASE;
+
+				/* HACK: breaks abstraction ...
+				   e_account_writable doesn't use the eaccount, it also uses the same writable key for
+				   source and transport */
+				if (!e_account_writable (NULL, E_ACCOUNT_SOURCE_SAVE_PASSWD))
+					eflags |= E_PASSWORDS_DISABLE_REMEMBER;
+
+				ret = e_passwords_ask_password (title, domain, key, prompt, eflags, &remember, NULL);
+
+				g_free (title);
+
+				if (ret && config_service)
+					mail_config_service_set_save_passwd (config_service, remember);
+
+				if (config_service)
+					config_service->get_password_canceled = ret == NULL;
+			}
+		}
+
+		g_free (key);
+	}
+
+	g_free (url);
+
+	if (ret == NULL)
+		g_set_error (
+			error, G_IO_ERROR,
+			G_IO_ERROR_CANCELLED,
+			_("User canceled operation."));
+
+	return ret;
+}
+
+static gboolean
+mail_session_forget_password (CamelSession *session,
+                              CamelService *service,
+                              const gchar *domain,
+                              const gchar *item,
+                              GError **error)
+{
+	gchar *key;
+
+	domain = (domain != NULL) ? domain : "Mail";
+	key = mail_session_make_key (service, item);
+
+	e_passwords_forget_password (domain, key);
+
+	g_free (key);
+
+	return TRUE;
+}
+
+static gboolean
+mail_session_alert_user (CamelSession *session,
+                         CamelSessionAlertType type,
+                         const gchar *prompt,
+                         gboolean cancel)
+{
+	struct _user_message_msg *m;
+	gboolean result = TRUE;
+
+	m = mail_msg_new (&user_message_info);
+	m->ismain = mail_in_main_thread ();
+	m->type = type;
+	m->prompt = g_strdup (prompt);
+	m->done = e_flag_new ();
+	m->allow_cancel = cancel;
+
+	if (cancel)
+		mail_msg_ref (m);
+
+	if (m->ismain)
+		user_message_exec (m);
+	else
+		mail_msg_main_loop_push (m);
+
+	if (cancel) {
+		e_flag_wait (m->done);
+		result = m->result;
+		mail_msg_unref (m);
+	} else if (m->ismain)
+		mail_msg_unref (m);
+
+	return result;
+}
+
+static CamelFilterDriver *
+mail_session_get_filter_driver (CamelSession *session,
+                                const gchar *type,
+                                GError **error)
+{
+	return (CamelFilterDriver *) mail_call_main (
+		MAIL_CALL_p_ppp, (MailMainFunc) main_get_filter_driver,
+		session, type, error);
+}
+
+static gboolean
+mail_session_lookup_addressbook (CamelSession *session,
+                                 const gchar *name)
+{
+	CamelInternetAddress *addr;
+	gboolean ret;
+
+	if (!mail_config_get_lookup_book ())
+		return FALSE;
+
+	addr = camel_internet_address_new ();
+	camel_address_decode ((CamelAddress *)addr, name);
+	ret = em_utils_in_addressbook (
+		addr, mail_config_get_lookup_book_local_only ());
+	g_object_unref (addr);
+
+	return ret;
+}
+
+static gpointer
+mail_session_thread_msg_new (CamelSession *session,
+                             CamelSessionThreadOps *ops,
+                             guint size)
+{
+	CamelSessionThreadMsg *msg;
+	CamelSessionClass *session_class;
+
+	/* TODO This is very temporary, until we have a better way to do
+	 *      the progress reporting, we just borrow a dummy mail-mt
+	 *      thread message and hook it onto out camel thread message. */
+
+	/* Chain up to parent's thread_msg_new() method. */
+	session_class = CAMEL_SESSION_CLASS (e_mail_session_parent_class);
+	msg = session_class->thread_msg_new (session, ops, size);
+
+	/* We create a dummy mail_msg, and then copy its cancellation
+	 * port over to ours, so we get cancellation and progress in
+	 * common with hte existing mail code, for free. */
+	if (msg) {
+		MailMsg *m = mail_msg_new (&ms_thread_info_dummy);
+
+		msg->data = m;
+		g_object_unref (msg->cancellable);
+		msg->cancellable = g_object_ref (m->cancellable);
+	}
+
+	return msg;
+}
+
+static void
+mail_session_thread_msg_free (CamelSession *session,
+                              CamelSessionThreadMsg *msg)
+{
+	CamelSessionClass *session_class;
+
+	mail_msg_unref (msg->data);
+
+	/* Chain up to parent's thread_msg_free() method. */
+	session_class = CAMEL_SESSION_CLASS (e_mail_session_parent_class);
+	session_class->thread_msg_free (session, msg);
+}
+
+static void
+mail_session_thread_status (CamelSession *session,
+                            CamelSessionThreadMsg *msg,
+                            const gchar *text,
+                            gint pc)
+{
+	/* This should never be called since we bypass it in alloc! */
+	g_warn_if_reached ();
+}
+
+static gboolean
+mail_session_forward_to (CamelSession *session,
+                         CamelFolder *folder,
+                         CamelMimeMessage *message,
+                         const gchar *address,
+                         GError **error)
+{
+	EAccount *account;
+	CamelMimeMessage *forward;
+	CamelStream *mem;
+	CamelInternetAddress *addr;
+	CamelFolder *out_folder;
+	CamelMessageInfo *info;
+	struct _camel_header_raw *xev;
+	gchar *subject;
+
+	g_return_val_if_fail (folder != NULL, FALSE);
+	g_return_val_if_fail (message != NULL, FALSE);
+	g_return_val_if_fail (address != NULL, FALSE);
+
+	if (!*address) {
+		g_set_error (
+			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
+			_("No destination address provided, forward "
+			  "of the message has been cancelled."));
+		return FALSE;
+	}
+
+	account = em_utils_guess_account_with_recipients (message, folder);
+	if (!account) {
+		g_set_error (
+			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
+			_("No account found to use, forward of the "
+			  "message has been cancelled."));
+		return FALSE;
+	}
+
+	forward = camel_mime_message_new ();
+
+	/* make copy of the message, because we are going to modify it */
+	mem = camel_stream_mem_new ();
+	camel_data_wrapper_write_to_stream_sync ((CamelDataWrapper *)message, mem, NULL, NULL);
+	camel_seekable_stream_seek (CAMEL_SEEKABLE_STREAM (mem), 0, CAMEL_STREAM_SET, NULL);
+	camel_data_wrapper_construct_from_stream_sync ((CamelDataWrapper *)forward, mem, NULL, NULL);
+	g_object_unref (mem);
+
+	/* clear previous recipients */
+	camel_mime_message_set_recipients (forward, CAMEL_RECIPIENT_TYPE_TO, NULL);
+	camel_mime_message_set_recipients (forward, CAMEL_RECIPIENT_TYPE_CC, NULL);
+	camel_mime_message_set_recipients (forward, CAMEL_RECIPIENT_TYPE_BCC, NULL);
+	camel_mime_message_set_recipients (forward, CAMEL_RECIPIENT_TYPE_RESENT_TO, NULL);
+	camel_mime_message_set_recipients (forward, CAMEL_RECIPIENT_TYPE_RESENT_CC, NULL);
+	camel_mime_message_set_recipients (forward, CAMEL_RECIPIENT_TYPE_RESENT_BCC, NULL);
+
+	/* remove all delivery and notification headers */
+	while (camel_medium_get_header (CAMEL_MEDIUM (forward), "Disposition-Notification-To"))
+		camel_medium_remove_header (CAMEL_MEDIUM (forward), "Disposition-Notification-To");
+
+	while (camel_medium_get_header (CAMEL_MEDIUM (forward), "Delivered-To"))
+		camel_medium_remove_header (CAMEL_MEDIUM (forward), "Delivered-To");
+
+	/* remove any X-Evolution-* headers that may have been set */
+	xev = mail_tool_remove_xevolution_headers (forward);
+	camel_header_raw_clear (&xev);
+
+	/* from */
+	addr = camel_internet_address_new ();
+	camel_internet_address_add (addr, account->id->name, account->id->address);
+	camel_mime_message_set_from (forward, addr);
+	g_object_unref (addr);
+
+	/* to */
+	addr = camel_internet_address_new ();
+	camel_address_decode (CAMEL_ADDRESS (addr), address);
+	camel_mime_message_set_recipients (forward, CAMEL_RECIPIENT_TYPE_TO, addr);
+	g_object_unref (addr);
+
+	/* subject */
+	subject = mail_tool_generate_forward_subject (message);
+	camel_mime_message_set_subject (forward, subject);
+	g_free (subject);
+
+	/* and send it */
+	info = camel_message_info_new (NULL);
+	out_folder = e_mail_local_get_folder (E_MAIL_FOLDER_OUTBOX);
+	camel_message_info_set_flags (
+		info, CAMEL_MESSAGE_SEEN, CAMEL_MESSAGE_SEEN);
+	mail_append_mail (
+		out_folder, forward, info, ms_forward_to_cb, session);
+
+	return TRUE;
+}
+
+static void
+e_mail_session_class_init (EMailSessionClass *class)
+{
+	GObjectClass *object_class;
+	CamelSessionClass *session_class;
+
+	g_type_class_add_private (class, sizeof (EMailSessionPrivate));
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->finalize = mail_session_finalize;
+
+	session_class = CAMEL_SESSION_CLASS (class);
+	session_class->get_password = mail_session_get_password;
+	session_class->forget_password = mail_session_forget_password;
+	session_class->alert_user = mail_session_alert_user;
+	session_class->get_filter_driver = mail_session_get_filter_driver;
+	session_class->lookup_addressbook = mail_session_lookup_addressbook;
+	session_class->thread_msg_new = mail_session_thread_msg_new;
+	session_class->thread_msg_free = mail_session_thread_msg_free;
+	session_class->thread_status = mail_session_thread_status;
+	session_class->forward_to = mail_session_forward_to;
+}
+
+static void
+e_mail_session_init (EMailSession *session)
+{
+	GConfClient *client;
+
+	session->priv = E_MAIL_SESSION_GET_PRIVATE (session);
+
+	/* Initialize the EAccount setup. */
+	e_account_writable (NULL, E_ACCOUNT_SOURCE_SAVE_PASSWD);
+
+	camel_session_construct (
+		CAMEL_SESSION (session),
+		mail_session_get_data_dir ());
+
+	client = gconf_client_get_default ();
+
+	gconf_client_add_dir (
+		client, "/apps/evolution/mail/junk",
+		GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
+	camel_session_set_check_junk (
+		CAMEL_SESSION (session), gconf_client_get_bool (
+		client, "/apps/evolution/mail/junk/check_incoming", NULL));
+	session_check_junk_notify_id = gconf_client_notify_add (
+		client, "/apps/evolution/mail/junk",
+		(GConfClientNotifyFunc) mail_session_check_junk_notify,
+		session, NULL, NULL);
+	CAMEL_SESSION (session)->junk_plugin = NULL;
+
+	mail_config_reload_junk_headers (CAMEL_SESSION (session));
+
+	init_socks_proxy (CAMEL_SESSION (session));
+
+	g_object_unref (client);
+}
+
+EMailSession *
+e_mail_session_new (void)
+{
+	return g_object_new (E_TYPE_MAIL_SESSION, NULL);
+}
+
+static void
+mail_session_get_inbox_thread (GSimpleAsyncResult *simple,
+                               EMailSession *session,
+                               GCancellable *cancellable)
+{
+	AsyncContext *context;
+	GError *error = NULL;
+
+	context = g_simple_async_result_get_op_res_gpointer (simple);
+
+	context->folder = e_mail_session_get_inbox_sync (
+		session, context->uri, cancellable, &error);
+
+	if (error != NULL) {
+		g_simple_async_result_set_from_error (simple, error);
+		g_error_free (error);
+	}
+}
+
+CamelFolder *
+e_mail_session_get_inbox_sync (EMailSession *session,
+                               const gchar *service_uri,
+                               GCancellable *cancellable,
+                               GError **error)
+{
+	CamelStore *store;
+	CamelFolder *folder;
+
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+	g_return_val_if_fail (service_uri != NULL, NULL);
+
+	store = camel_session_get_store (
+		CAMEL_SESSION (session), service_uri, error);
+
+	if (store == NULL)
+		return NULL;
+
+	folder = camel_store_get_inbox_folder_sync (store, cancellable, error);
+
+	g_object_unref (store);
+
+	return folder;
+}
+
+void
+e_mail_session_get_inbox (EMailSession *session,
+                          const gchar *service_uri,
+                          gint io_priority,
+                          GCancellable *cancellable,
+                          GAsyncReadyCallback callback,
+                          gpointer user_data)
+{
+	GSimpleAsyncResult *simple;
+	AsyncContext *context;
+
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+	g_return_if_fail (service_uri != NULL);
+
+	context = g_slice_new0 (AsyncContext);
+	context->uri = g_strdup (service_uri);
+
+	simple = g_simple_async_result_new (
+		G_OBJECT (session), callback,
+		user_data, e_mail_session_get_inbox);
+
+	g_simple_async_result_set_op_res_gpointer (
+		simple, context, (GDestroyNotify) async_context_free);
+
+	g_simple_async_result_run_in_thread (
+		simple, (GSimpleAsyncThreadFunc)
+		mail_session_get_inbox_thread,
+		io_priority, cancellable);
+
+	g_object_unref (simple);
+}
+
+CamelFolder *
+e_mail_session_get_inbox_finish (EMailSession *session,
+                                 GAsyncResult *result,
+                                 GError **error)
+{
+	GSimpleAsyncResult *simple;
+	AsyncContext *context;
+
+	g_return_val_if_fail (
+		g_simple_async_result_is_valid (
+		result, G_OBJECT (session),
+		e_mail_session_get_inbox), NULL);
+
+	simple = G_SIMPLE_ASYNC_RESULT (result);
+	context = g_simple_async_result_get_op_res_gpointer (simple);
+
+	if (g_simple_async_result_propagate_error (simple, error))
+		return NULL;
+
+	g_return_val_if_fail (CAMEL_IS_FOLDER (context->folder), NULL);
+
+	return g_object_ref (context->folder);
+}
+
+static void
+mail_session_get_trash_thread (GSimpleAsyncResult *simple,
+                               EMailSession *session,
+                               GCancellable *cancellable)
+{
+	AsyncContext *context;
+	GError *error = NULL;
+
+	context = g_simple_async_result_get_op_res_gpointer (simple);
+
+	context->folder = e_mail_session_get_trash_sync (
+		session, context->uri, cancellable, &error);
+
+	if (error != NULL) {
+		g_simple_async_result_set_from_error (simple, error);
+		g_error_free (error);
+	}
+}
+
+CamelFolder *
+e_mail_session_get_trash_sync (EMailSession *session,
+                               const gchar *service_uri,
+                               GCancellable *cancellable,
+                               GError **error)
+{
+	CamelStore *store;
+	CamelFolder *folder;
+
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+	g_return_val_if_fail (service_uri != NULL, NULL);
+
+	store = camel_session_get_store (
+		CAMEL_SESSION (session), service_uri, error);
+
+	if (store == NULL)
+		return NULL;
+
+	folder = camel_store_get_trash_folder_sync (store, cancellable, error);
+
+	g_object_unref (store);
+
+	return folder;
+}
+
+void
+e_mail_session_get_trash (EMailSession *session,
+                          const gchar *service_uri,
+                          gint io_priority,
+                          GCancellable *cancellable,
+                          GAsyncReadyCallback callback,
+                          gpointer user_data)
+{
+	GSimpleAsyncResult *simple;
+	AsyncContext *context;
+
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+	g_return_if_fail (service_uri != NULL);
+
+	context = g_slice_new0 (AsyncContext);
+	context->uri = g_strdup (service_uri);
+
+	simple = g_simple_async_result_new (
+		G_OBJECT (session), callback,
+		user_data, e_mail_session_get_trash);
+
+	g_simple_async_result_set_op_res_gpointer (
+		simple, context, (GDestroyNotify) async_context_free);
+
+	g_simple_async_result_run_in_thread (
+		simple, (GSimpleAsyncThreadFunc)
+		mail_session_get_trash_thread,
+		io_priority, cancellable);
+
+	g_object_unref (simple);
+}
+
+CamelFolder *
+e_mail_session_get_trash_finish (EMailSession *session,
+                                 GAsyncResult *result,
+                                 GError **error)
+{
+	GSimpleAsyncResult *simple;
+	AsyncContext *context;
+
+	g_return_val_if_fail (
+		g_simple_async_result_is_valid (
+		result, G_OBJECT (session),
+		e_mail_session_get_trash), NULL);
+
+	simple = G_SIMPLE_ASYNC_RESULT (result);
+	context = g_simple_async_result_get_op_res_gpointer (simple);
+
+	if (g_simple_async_result_propagate_error (simple, error))
+		return NULL;
+
+	g_return_val_if_fail (CAMEL_IS_FOLDER (context->folder), NULL);
+
+	return g_object_ref (context->folder);
+}
+
+static void
+mail_session_uri_to_folder_thread (GSimpleAsyncResult *simple,
+                                   EMailSession *session,
+                                   GCancellable *cancellable)
+{
+	AsyncContext *context;
+	GError *error = NULL;
+
+	context = g_simple_async_result_get_op_res_gpointer (simple);
+
+	context->folder = e_mail_session_uri_to_folder_sync (
+		session, context->uri, context->flags,
+		cancellable, &error);
+
+	if (error != NULL) {
+		g_simple_async_result_set_from_error (simple, error);
+		g_error_free (error);
+	}
+}
+
+CamelFolder *
+e_mail_session_uri_to_folder_sync (EMailSession *session,
+                                   const gchar *folder_uri,
+                                   CamelStoreGetFolderFlags flags,
+                                   GCancellable *cancellable,
+                                   GError **error)
+{
+	CamelURL *url;
+	CamelStore *store;
+	CamelFolder *folder = NULL;
+	gchar *camel_uri = NULL;
+	gboolean vtrash = FALSE;
+	gboolean vjunk = FALSE;
+
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+	g_return_val_if_fail (folder_uri != NULL, NULL);
+
+	camel_operation_push_message (
+		cancellable, _("Opening folder '%s'"), folder_uri);
+
+	/* FIXME vtrash and vjunk are no longer used for these URI's. */
+	if (g_str_has_prefix (folder_uri, "vtrash:")) {
+		folder_uri += 7;
+		vtrash = TRUE;
+	} else if (g_str_has_prefix (folder_uri, "vjunk:")) {
+		folder_uri += 6;
+		vjunk = TRUE;
+	} else if (g_str_has_prefix (folder_uri, "email:")) {
+		/* FIXME Shouldn't the filter:get_folder
+		 *       callback do this itself? */
+		camel_uri = em_uri_to_camel (folder_uri);
+		if (camel_uri == NULL) {
+			g_set_error (
+				error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
+				_("Invalid folder: %s"), folder_uri);
+			goto exit;
+		}
+		folder_uri = camel_uri;
+	}
+
+	url = camel_url_new (folder_uri, error);
+
+	if (url == NULL) {
+		g_free (camel_uri);
+		goto exit;
+	}
+
+	store = (CamelStore *) camel_session_get_service (
+		CAMEL_SESSION (session), folder_uri,
+		CAMEL_PROVIDER_STORE, error);
+
+	if (store != NULL) {
+		const gchar *name = "";
+
+		/* If we have a fragment, then the path is actually
+		 * used by the store, so the fragment is the path to
+		 * the folder instead. */
+		if (url->fragment != NULL)
+			name = url->fragment;
+		else if (url->path != NULL && *url->path != '\0')
+			name = url->path + 1;
+
+		if (vtrash)
+			folder = camel_store_get_trash_folder_sync (
+				store, cancellable, error);
+		else if (vjunk)
+			folder = camel_store_get_junk_folder_sync (
+				store, cancellable, error);
+		else
+			folder = camel_store_get_folder_sync (
+				store, name, flags, cancellable, error);
+
+		g_object_unref (store);
+	}
+
+	if (folder != NULL) {
+		MailFolderCache *cache;
+
+		cache = mail_folder_cache_get_default ();
+		mail_folder_cache_note_folder (cache, folder);
+	}
+
+	camel_url_free (url);
+	g_free (camel_uri);
+
+exit:
+	camel_operation_pop_message (cancellable);
+
+	return folder;
+}
+
+void
+e_mail_session_uri_to_folder (EMailSession *session,
+                              const gchar *folder_uri,
+                              CamelStoreGetFolderFlags flags,
+                              gint io_priority,
+                              GCancellable *cancellable,
+                              GAsyncReadyCallback callback,
+                              gpointer user_data)
+{
+	GSimpleAsyncResult *simple;
+	AsyncContext *context;
+
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+	g_return_if_fail (folder_uri != NULL);
+
+	context = g_slice_new0 (AsyncContext);
+	context->uri = g_strdup (folder_uri);
+	context->flags = flags;
+
+	simple = g_simple_async_result_new (
+		G_OBJECT (session), callback,
+		user_data, e_mail_session_uri_to_folder);
+
+	g_simple_async_result_set_op_res_gpointer (
+		simple, context, (GDestroyNotify) async_context_free);
+
+	g_simple_async_result_run_in_thread (
+		simple, (GSimpleAsyncThreadFunc)
+		mail_session_uri_to_folder_thread,
+		io_priority, cancellable);
+
+	g_object_unref (simple);
+}
+
+CamelFolder *
+e_mail_session_uri_to_folder_finish (EMailSession *session,
+                                     GAsyncResult *result,
+                                     GError **error)
+{
+	GSimpleAsyncResult *simple;
+	AsyncContext *context;
+
+	g_return_val_if_fail (
+		g_simple_async_result_is_valid (
+		result, G_OBJECT (session),
+		e_mail_session_uri_to_folder), NULL);
+
+	simple = G_SIMPLE_ASYNC_RESULT (result);
+	context = g_simple_async_result_get_op_res_gpointer (simple);
+
+	if (g_simple_async_result_propagate_error (simple, error))
+		return NULL;
+
+	g_return_val_if_fail (CAMEL_IS_FOLDER (context->folder), NULL);
+
+	return g_object_ref (context->folder);
+}
+
+/******************************** Legacy API *********************************/
+
+void
+mail_session_flush_filter_log (EMailSession *session)
+{
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+
+	if (session->priv->filter_logfile)
+		fflush (session->priv->filter_logfile);
+}
+
+void
+mail_session_add_junk_plugin (EMailSession *session,
+                              const gchar *plugin_name,
+                              CamelJunkPlugin *junk_plugin)
+{
+	GConfClient *gconf;
+	gchar *def_plugin;
+
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+
+	gconf = mail_config_get_gconf_client ();
+	def_plugin = gconf_client_get_string (
+		gconf, "/apps/evolution/mail/junk/default_plugin", NULL);
+
+	session->priv->junk_plugins = g_list_append (
+		session->priv->junk_plugins, junk_plugin);
+	if (def_plugin && plugin_name) {
+		if (!strcmp (def_plugin, plugin_name)) {
+			CAMEL_SESSION (session)->junk_plugin = junk_plugin;
+			camel_junk_plugin_init (junk_plugin);
+		}
+	}
+
+	g_free (def_plugin);
+}
+
+const GList *
+mail_session_get_junk_plugins (EMailSession *session)
+{
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
+	return session->priv->junk_plugins;
+}
+
+const gchar *
+mail_session_get_data_dir (void)
+{
+	if (G_UNLIKELY (mail_data_dir == NULL))
+		mail_data_dir = g_build_filename (
+			e_get_user_data_dir (), "mail", NULL);
+
+	return mail_data_dir;
+}
+
+const gchar *
+mail_session_get_config_dir (void)
+{
+	if (G_UNLIKELY (mail_config_dir == NULL))
+		mail_config_dir = g_build_filename (
+			e_get_user_config_dir (), "mail", NULL);
+
+	return mail_config_dir;
+}
+
diff --git a/mail/e-mail-session.h b/mail/e-mail-session.h
new file mode 100644
index 0000000..71a5d73
--- /dev/null
+++ b/mail/e-mail-session.h
@@ -0,0 +1,123 @@
+/*
+ * e-mail-session.h
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ *
+ * Authors:
+ *		Jeffrey Stedfast <fejj ximian com>
+ *
+ * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
+ *
+ */
+
+#ifndef E_MAIL_SESSION_H
+#define E_MAIL_SESSION_H
+
+#include <camel/camel.h>
+
+/* Standard GObject macros */
+#define E_TYPE_MAIL_SESSION \
+	(e_mail_session_get_type ())
+#define E_MAIL_SESSION(obj) \
+	(G_TYPE_CHECK_INSTANCE_CAST \
+	((obj), E_TYPE_MAIL_SESSION, EMailSession))
+#define E_MAIL_SESSION_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_CAST \
+	((cls), E_TYPE_MAIL_SESSION, EMailSessionClass))
+#define E_IS_MAIL_SESSION(obj) \
+	(G_TYPE_CHECK_INSTANCE_TYPE \
+	((obj), E_TYPE_MAIL_SESSION))
+#define E_IS_MAIL_SESSION_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_TYPE \
+	((cls), E_TYPE_MAIL_SESSION))
+#define E_MAIL_SESSION_GET_CLASS(obj) \
+	(G_TYPE_INSTANCE_GET_CLASS \
+	((obj), E_TYPE_MAIL_SESSION, EMailSessionClass))
+
+G_BEGIN_DECLS
+
+typedef struct _EMailSession EMailSession;
+typedef struct _EMailSessionClass EMailSessionClass;
+typedef struct _EMailSessionPrivate EMailSessionPrivate;
+
+struct _EMailSession {
+	CamelSession parent;
+	EMailSessionPrivate *priv;
+};
+
+struct _EMailSessionClass {
+	CamelSessionClass parent_class;
+};
+
+GType		e_mail_session_get_type		(void);
+EMailSession *	e_mail_session_new		(void);
+CamelFolder *	e_mail_session_get_inbox_sync	(EMailSession *session,
+						 const gchar *service_uri,
+						 GCancellable *cancellable,
+						 GError **error);
+void		e_mail_session_get_inbox	(EMailSession *session,
+						 const gchar *service_uri,
+						 gint io_priority,
+						 GCancellable *cancellable,
+						 GAsyncReadyCallback callback,
+						 gpointer user_data);
+CamelFolder *	e_mail_session_get_inbox_finish	(EMailSession *session,
+						 GAsyncResult *result,
+						 GError **error);
+CamelFolder *	e_mail_session_get_trash_sync	(EMailSession *session,
+						 const gchar *service_uri,
+						 GCancellable *cancellable,
+						 GError **error);
+void		e_mail_session_get_trash	(EMailSession *session,
+						 const gchar *service_uri,
+						 gint io_priority,
+						 GCancellable *cancellable,
+						 GAsyncReadyCallback callback,
+						 gpointer user_data);
+CamelFolder *	e_mail_session_get_trash_finish	(EMailSession *session,
+						 GAsyncResult *result,
+						 GError **error);
+CamelFolder *	e_mail_session_uri_to_folder_sync
+						(EMailSession *session,
+						 const gchar *folder_uri,
+						 CamelStoreGetFolderFlags flags,
+						 GCancellable *cancellable,
+						 GError **error);
+void		e_mail_session_uri_to_folder	(EMailSession *session,
+						 const gchar *folder_uri,
+						 CamelStoreGetFolderFlags flags,
+						 gint io_priority,
+						 GCancellable *cancellable,
+						 GAsyncReadyCallback callback,
+						 gpointer user_data);
+CamelFolder *	e_mail_session_uri_to_folder_finish
+						(EMailSession *session,
+						 GAsyncResult *result,
+						 GError **error);
+
+
+/*** Legacy API ***/
+
+void		mail_session_flush_filter_log	(EMailSession *session);
+void		mail_session_add_junk_plugin	(EMailSession *session,
+						 const gchar *plugin_name,
+						 CamelJunkPlugin *junk_plugin);
+const GList *	mail_session_get_junk_plugins	(EMailSession *session);
+const gchar *	mail_session_get_data_dir	(void);
+const gchar *	mail_session_get_config_dir	(void);
+
+G_END_DECLS
+
+#endif /* E_MAIL_SESSION_H */
diff --git a/mail/e-mail-sidebar.c b/mail/e-mail-sidebar.c
index 5967734..a418dc1 100644
--- a/mail/e-mail-sidebar.c
+++ b/mail/e-mail-sidebar.c
@@ -474,9 +474,12 @@ e_mail_sidebar_get_type (void)
 }
 
 GtkWidget *
-e_mail_sidebar_new (void)
+e_mail_sidebar_new (EMailSession *session)
 {
-	return g_object_new (E_TYPE_MAIL_SIDEBAR, NULL);
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
+	return g_object_new (
+		E_TYPE_MAIL_SIDEBAR, "session", session, NULL);
 }
 
 GKeyFile *
diff --git a/mail/e-mail-sidebar.h b/mail/e-mail-sidebar.h
index b57ff76..ebb6d0a 100644
--- a/mail/e-mail-sidebar.h
+++ b/mail/e-mail-sidebar.h
@@ -76,7 +76,7 @@ struct _EMailSidebarClass {
 };
 
 GType		e_mail_sidebar_get_type		(void);
-GtkWidget *	e_mail_sidebar_new		(void);
+GtkWidget *	e_mail_sidebar_new		(EMailSession *session);
 GKeyFile *	e_mail_sidebar_get_key_file	(EMailSidebar *sidebar);
 void		e_mail_sidebar_set_key_file	(EMailSidebar *sidebar,
 						 GKeyFile *key_file);
diff --git a/mail/e-mail-store.c b/mail/e-mail-store.c
index 91de750..ef997d2 100644
--- a/mail/e-mail-store.c
+++ b/mail/e-mail-store.c
@@ -32,7 +32,6 @@
 #include "mail/em-folder-tree-model.h"
 #include "mail/mail-folder-cache.h"
 #include "mail/mail-mt.h"
-#include "mail/mail-session.h"
 
 typedef struct _StoreInfo StoreInfo;
 
@@ -160,7 +159,8 @@ mail_store_note_store_cb (CamelStore *store,
 }
 
 static void
-mail_store_add (CamelStore *store,
+mail_store_add (EMailSession *session,
+                CamelStore *store,
                 const gchar *display_name,
                 AddStoreCallback callback)
 {
@@ -181,7 +181,7 @@ mail_store_add (CamelStore *store,
 
 	mail_folder_cache_note_store (
 		mail_folder_cache_get_default (),
-		store, NULL,
+		CAMEL_SESSION (session), store, NULL,
 		mail_store_note_store_cb,
 		store_info_ref (store_info));
 }
@@ -203,7 +203,8 @@ mail_store_add_local_done_cb (CamelStore *store,
 }
 
 static void
-mail_store_load_accounts (const gchar *data_dir)
+mail_store_load_accounts (EMailSession *session,
+                          const gchar *data_dir)
 {
 	CamelStore *local_store;
 	EAccountList *account_list;
@@ -211,11 +212,11 @@ mail_store_load_accounts (const gchar *data_dir)
 
 	/* Set up the local store. */
 
-	e_mail_local_init (data_dir);
+	e_mail_local_init (session, data_dir);
 	local_store = e_mail_local_get_store ();
 
 	mail_store_add (
-		local_store, _("On This Computer"),
+		session, local_store, _("On This Computer"),
 		(AddStoreCallback) mail_store_add_local_done_cb);
 
 	/* Set up remote stores. */
@@ -247,18 +248,19 @@ mail_store_load_accounts (const gchar *data_dir)
 		if (g_str_has_prefix (uri, "mbox:"))
 			continue;
 
-		e_mail_store_add_by_uri (uri, display_name);
+		e_mail_store_add_by_uri (session, uri, display_name);
 	}
 
 	g_object_unref (iter);
 }
 
 void
-e_mail_store_init (const gchar *data_dir)
+e_mail_store_init (EMailSession *session,
+                   const gchar *data_dir)
 {
 	static gboolean initialized = FALSE;
 
-	g_return_if_fail (data_dir != NULL);
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
 
 	/* This function is idempotent, but there should
 	 * be no need to call it more than once. */
@@ -272,29 +274,33 @@ e_mail_store_init (const gchar *data_dir)
 		(GDestroyNotify) NULL,
 		(GDestroyNotify) store_table_free);
 
-	mail_store_load_accounts (data_dir);
+	mail_store_load_accounts (session, data_dir);
 
 	initialized = TRUE;
 }
 
 void
-e_mail_store_add (CamelStore *store,
+e_mail_store_add (EMailSession *session,
+                  CamelStore *store,
                   const gchar *display_name)
 {
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
 	g_return_if_fail (CAMEL_IS_STORE (store));
 	g_return_if_fail (display_name != NULL);
 
-	mail_store_add (store, display_name, NULL);
+	mail_store_add (session, store, display_name, NULL);
 }
 
 CamelStore *
-e_mail_store_add_by_uri (const gchar *uri,
+e_mail_store_add_by_uri (EMailSession *session,
+                         const gchar *uri,
                          const gchar *display_name)
 {
 	CamelService *service;
 	CamelProvider *provider;
 	GError *local_error = NULL;
 
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
 	g_return_val_if_fail (uri != NULL, NULL);
 	g_return_val_if_fail (display_name != NULL, NULL);
 
@@ -309,11 +315,12 @@ e_mail_store_add_by_uri (const gchar *uri,
 		return NULL;
 
 	service = camel_session_get_service (
-		session, uri, CAMEL_PROVIDER_STORE, &local_error);
+		CAMEL_SESSION (session), uri,
+		CAMEL_PROVIDER_STORE, &local_error);
 	if (service == NULL)
 		goto fail;
 
-	e_mail_store_add (CAMEL_STORE (service), display_name);
+	e_mail_store_add (session, CAMEL_STORE (service), display_name);
 
 	g_object_unref (service);
 
@@ -360,11 +367,13 @@ e_mail_store_remove (CamelStore *store)
 }
 
 void
-e_mail_store_remove_by_uri (const gchar *uri)
+e_mail_store_remove_by_uri (EMailSession *session,
+                            const gchar *uri)
 {
 	CamelService *service;
 	CamelProvider *provider;
 
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
 	g_return_if_fail (uri != NULL);
 
 	provider = camel_provider_get (uri, NULL);
@@ -375,7 +384,8 @@ e_mail_store_remove_by_uri (const gchar *uri)
 		return;
 
 	service = camel_session_get_service (
-		session, uri, CAMEL_PROVIDER_STORE, NULL);
+		CAMEL_SESSION (session), uri,
+		CAMEL_PROVIDER_STORE, NULL);
 	if (service == NULL)
 		return;
 
diff --git a/mail/e-mail-store.h b/mail/e-mail-store.h
index 7d42fc3..8def4b8 100644
--- a/mail/e-mail-store.h
+++ b/mail/e-mail-store.h
@@ -24,16 +24,21 @@
 
 #include <glib.h>
 #include <camel/camel.h>
+#include <mail/e-mail-session.h>
 
 G_BEGIN_DECLS
 
-void		e_mail_store_init		(const gchar *data_dir);
-void		e_mail_store_add		(CamelStore *store,
+void		e_mail_store_init		(EMailSession *session,
+						 const gchar *data_dir);
+void		e_mail_store_add		(EMailSession *session,
+						 CamelStore *store,
 						 const gchar *display_name);
-CamelStore *	e_mail_store_add_by_uri		(const gchar *uri,
+CamelStore *	e_mail_store_add_by_uri		(EMailSession *session,
+						 const gchar *uri,
 						 const gchar *display_name);
 void		e_mail_store_remove		(CamelStore *store);
-void		e_mail_store_remove_by_uri	(const gchar *uri);
+void		e_mail_store_remove_by_uri	(EMailSession *session,
+						 const gchar *uri);
 void		e_mail_store_foreach		(GHFunc func,
 						 gpointer user_data);
 
diff --git a/mail/em-account-editor.c b/mail/em-account-editor.c
index 08126ea..7d0b371 100644
--- a/mail/em-account-editor.c
+++ b/mail/em-account-editor.c
@@ -57,11 +57,11 @@
 #include "widgets/misc/e-signature-editor.h"
 
 #include "e-mail-local.h"
+#include "e-mail-session.h"
 #include "e-mail-store.h"
 #include "em-config.h"
 #include "em-folder-selection-button.h"
 #include "em-account-editor.h"
-#include "mail-session.h"
 #include "mail-send-recv.h"
 #include "em-utils.h"
 #include "mail-config.h"
@@ -141,6 +141,7 @@ typedef struct _EMAccountEditorService {
 
 struct _EMAccountEditorPrivate {
 
+	EMailSession *session;
 	EAccount *modified_account;
 	EAccount *original_account;
 	gboolean new_account;
@@ -213,7 +214,7 @@ enum {
 	PROP_0,
 	PROP_MODIFIED_ACCOUNT,
 	PROP_ORIGINAL_ACCOUNT,
-	PROP_SHELL
+	PROP_SESSION
 };
 
 static void emae_refresh_authtype (EMAccountEditor *emae, EMAccountEditorService *service);
@@ -267,6 +268,16 @@ emae_set_original_account (EMAccountEditor *emae,
 }
 
 static void
+emae_set_session (EMAccountEditor *emae,
+                  EMailSession *session)
+{
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+	g_return_if_fail (emae->priv->session == NULL);
+
+	emae->priv->session = g_object_ref (session);
+}
+
+static void
 emae_set_property (GObject *object,
                    guint property_id,
                    const GValue *value,
@@ -278,6 +289,12 @@ emae_set_property (GObject *object,
 				EM_ACCOUNT_EDITOR (object),
 				g_value_get_object (value));
 			return;
+
+		case PROP_SESSION:
+			emae_set_session (
+				EM_ACCOUNT_EDITOR (object),
+				g_value_get_object (value));
+			return;
 	}
 
 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -303,6 +320,13 @@ emae_get_property (GObject *object,
 				em_account_editor_get_original_account (
 				EM_ACCOUNT_EDITOR (object)));
 			return;
+
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				em_account_editor_get_session (
+				EM_ACCOUNT_EDITOR (object)));
+			return;
 	}
 
 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -315,6 +339,11 @@ emae_dispose (GObject *object)
 
 	priv = EM_ACCOUNT_EDITOR_GET_PRIVATE (object);
 
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
 	if (priv->modified_account != NULL) {
 		g_object_unref (priv->modified_account);
 		priv->modified_account = NULL;
@@ -387,6 +416,17 @@ emae_class_init (GObjectClass *class)
 			E_TYPE_ACCOUNT,
 			G_PARAM_READWRITE |
 			G_PARAM_CONSTRUCT_ONLY));
+
+	g_object_class_install_property (
+		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			"Mail Session",
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
 }
 
 static void
@@ -441,13 +481,17 @@ em_account_editor_get_type (void)
 EMAccountEditor *
 em_account_editor_new (EAccount *account,
                        EMAccountEditorType type,
+                       EMailSession *session,
                        const gchar *id)
 {
 	EMAccountEditor *emae;
 
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
 	emae = g_object_new (
 		EM_TYPE_ACCOUNT_EDITOR,
-		"original-account", account, NULL);
+		"original-account", account,
+		"session", session, NULL);
 
 	em_account_editor_construct (emae, type, id);
 
@@ -468,14 +512,18 @@ em_account_editor_new (EAccount *account,
 EMAccountEditor *
 em_account_editor_new_for_pages (EAccount *account,
                                  EMAccountEditorType type,
+                                 EMailSession *session,
                                  const gchar *id,
                                  GtkWidget **pages)
 {
 	EMAccountEditor *emae;
 
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
 	emae = g_object_new (
 		EM_TYPE_ACCOUNT_EDITOR,
-		"original-account", account, NULL);
+		"original-account", account,
+		"session", session, NULL);
 
 	emae->pages = pages;
 	em_account_editor_construct (emae, type, id);
@@ -499,6 +547,14 @@ em_account_editor_get_original_account (EMAccountEditor *emae)
 	return emae->priv->original_account;
 }
 
+EMailSession *
+em_account_editor_get_session (EMAccountEditor *emae)
+{
+	g_return_val_if_fail (EM_IS_ACCOUNT_EDITOR (emae), NULL);
+
+	return emae->priv->session;
+}
+
 /* ********************************************************************** */
 
 static struct {
@@ -2000,6 +2056,7 @@ emae_check_authtype (GtkWidget *w,
                      EMAccountEditorService *service)
 {
 	EMAccountEditor *emae = service->emae;
+	EMailSession *session;
 	EAccount *account;
 	GtkWidget *editor;
 	const gchar *uri;
@@ -2007,6 +2064,8 @@ emae_check_authtype (GtkWidget *w,
 	account = em_account_editor_get_modified_account (emae);
 	editor = E_CONFIG (service->emae->config)->window;
 
+	session = em_account_editor_get_session (emae);
+
 	/* TODO: do we need to remove the auth mechanism from the uri? */
 	uri = e_account_get_string (account, emae_service_info[service->type].account_uri_key);
 	g_object_ref (emae);
@@ -2017,7 +2076,10 @@ emae_check_authtype (GtkWidget *w,
 	gtk_widget_show (service->check_dialog);
 	if (editor != NULL)
 		gtk_widget_set_sensitive (editor, FALSE);
-	service->check_id = mail_check_service (uri, service->type, emae_check_authtype_done, service);
+
+	service->check_id = mail_check_service (
+		session, uri, service->type,
+		emae_check_authtype_done, service);
 }
 
 static void
@@ -2989,8 +3051,10 @@ emae_defaults_page (EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget
 {
 	EMAccountEditor *emae = data;
 	EMAccountEditorPrivate *priv = emae->priv;
+	EMFolderSelectionButton *button;
+	EMailSession *session;
 	EAccount *account;
-	GtkWidget *w;
+	GtkWidget *widget;
 	GtkBuilder *builder;
 
 	/*if (old)
@@ -2999,23 +3063,50 @@ emae_defaults_page (EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget
 		return NULL;
 
 	account = em_account_editor_get_modified_account (emae);
+	session = em_account_editor_get_session (emae);
+
+	/* Make sure we have a valid EMailSession. */
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
 
 	builder = gtk_builder_new ();
 	e_load_ui_builder_definition (builder, "mail-config.ui");
 
 	/* Special folders */
-	priv->drafts_folder_button = (GtkButton *)emae_account_folder (emae, "drafts_button", E_ACCOUNT_DRAFTS_FOLDER_URI, E_MAIL_FOLDER_DRAFTS, builder);
-	priv->sent_folder_button = (GtkButton *)emae_account_folder (emae, "sent_button", E_ACCOUNT_SENT_FOLDER_URI, E_MAIL_FOLDER_SENT, builder);
+	button = emae_account_folder (
+		emae, "drafts_button",
+		E_ACCOUNT_DRAFTS_FOLDER_URI,
+		E_MAIL_FOLDER_DRAFTS, builder);
+	em_folder_selection_button_set_session (button, session);
+	priv->drafts_folder_button = GTK_BUTTON (button);
+
+	button = emae_account_folder (
+		emae, "sent_button",
+		E_ACCOUNT_SENT_FOLDER_URI,
+		E_MAIL_FOLDER_SENT, builder);
+	em_folder_selection_button_set_session (button, session);
+	priv->sent_folder_button = GTK_BUTTON (button);
+
+	widget = e_builder_get_widget (builder, "trash_folder_check");
+	priv->trash_folder_check = GTK_TOGGLE_BUTTON (widget);
+
+	widget = e_builder_get_widget (builder, "trash_folder_butt");
+	button = EM_FOLDER_SELECTION_BUTTON (widget);
+	em_folder_selection_button_set_session (button, session);
+	priv->trash_folder_button = GTK_BUTTON (button);
 
-	priv->trash_folder_check = GTK_TOGGLE_BUTTON (e_builder_get_widget (builder, "trash_folder_check"));
-	priv->trash_folder_button = GTK_BUTTON (e_builder_get_widget (builder, "trash_folder_butt"));
 	setup_checkable_folder (
 		emae, CAMEL_PROVIDER_ALLOW_REAL_TRASH_FOLDER,
 		GTK_WIDGET (priv->trash_folder_check),
 		GTK_WIDGET (priv->trash_folder_button));
 
-	priv->junk_folder_check = GTK_TOGGLE_BUTTON (e_builder_get_widget (builder, "junk_folder_check"));
-	priv->junk_folder_button = GTK_BUTTON (e_builder_get_widget (builder, "junk_folder_butt"));
+	widget = e_builder_get_widget (builder, "junk_folder_check");
+	priv->junk_folder_check = GTK_TOGGLE_BUTTON (widget);
+
+	widget = e_builder_get_widget (builder, "junk_folder_butt");
+	button = EM_FOLDER_SELECTION_BUTTON (widget);
+	em_folder_selection_button_set_session (button, session);
+	priv->junk_folder_button = GTK_BUTTON (button);
+
 	setup_checkable_folder (
 		emae, CAMEL_PROVIDER_ALLOW_REAL_JUNK_FOLDER,
 		GTK_WIDGET (priv->junk_folder_check),
@@ -3047,12 +3138,12 @@ emae_defaults_page (EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget
 	/* Receipt policy */
 	emae_setup_receipt_policy (emae, builder);
 
-	w = e_builder_get_widget (builder, item->label);
+	widget = e_builder_get_widget (builder, item->label);
 	if (emae->type == EMAE_PAGES) {
-		gtk_box_pack_start ((GtkBox *)emae->pages[4], w, TRUE, TRUE, 0);
-		gtk_widget_show  (w);
+		gtk_box_pack_start ((GtkBox *)emae->pages[4], widget, TRUE, TRUE, 0);
+		gtk_widget_show  (widget);
 	}else {
-		gtk_notebook_append_page ((GtkNotebook *)parent, w, gtk_label_new (_("Defaults")));
+		gtk_notebook_append_page ((GtkNotebook *)parent, widget, gtk_label_new (_("Defaults")));
 	}
 
 	emae_queue_widgets (
@@ -3063,7 +3154,7 @@ emae_defaults_page (EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget
 
 	g_object_unref (builder);
 
-	return w;
+	return widget;
 }
 
 static void
@@ -3636,18 +3727,6 @@ em_account_editor_check (EMAccountEditor *emae, const gchar *page)
 	return emae_check_complete ((EConfig *)emae->config, page, emae);
 }
 
-/* HACK: FIXME: the component should listen to the account object directly */
-static void
-add_new_store (gchar *uri, CamelStore *store, gpointer user_data)
-{
-	EAccount *account = user_data;
-
-	if (store == NULL)
-		return;
-
-	e_mail_store_add (store, account->name);
-}
-
 static void
 emae_commit (EConfig *ec, GSList *items, gpointer data)
 {
@@ -3676,8 +3755,24 @@ emae_commit (EConfig *ec, GSList *items, gpointer data)
 		   We should just be listening to the account list directly for changed events */
 		if (account->enabled
 		    && emae->priv->source.provider
-		    && (emae->priv->source.provider->flags & CAMEL_PROVIDER_IS_STORAGE))
-			mail_get_store (e_account_get_string (modified_account, E_ACCOUNT_SOURCE_URL), NULL, add_new_store, account);
+		    && (emae->priv->source.provider->flags & CAMEL_PROVIDER_IS_STORAGE)) {
+			EMailSession *session;
+			CamelStore *store;
+			const gchar *uri;
+
+			session = em_account_editor_get_session (emae);
+			uri = e_account_get_string (
+				modified_account, E_ACCOUNT_SOURCE_URL);
+			store = (CamelStore *) camel_session_get_service (
+				CAMEL_SESSION (session), uri,
+				CAMEL_PROVIDER_STORE, NULL);
+			if (store != NULL) {
+				e_mail_store_add (
+					session, store,
+					modified_account->name);
+				g_object_unref (store);
+			}
+		}
 	}
 
 	if (gtk_toggle_button_get_active (emae->priv->default_account))
diff --git a/mail/em-account-editor.h b/mail/em-account-editor.h
index 64f514b..7be79c6 100644
--- a/mail/em-account-editor.h
+++ b/mail/em-account-editor.h
@@ -26,6 +26,7 @@
 
 #include <gtk/gtk.h>
 #include <mail/em-config.h>
+#include <mail/e-mail-session.h>
 
 /* Standard GObject macros */
 #define EM_TYPE_ACCOUNT_EDITOR \
@@ -98,16 +99,19 @@ GType		em_account_editor_get_type	(void);
 EMAccountEditor *
 		em_account_editor_new		(EAccount *account,
 						 EMAccountEditorType type,
+						 EMailSession *session,
 						 const gchar *id);
 EMAccountEditor *
 		em_account_editor_new_for_pages	(EAccount *account,
 						 EMAccountEditorType type,
+						 EMailSession *session,
 						 const gchar *id,
 						 GtkWidget **pages);
 EAccount *	em_account_editor_get_modified_account
 						(EMAccountEditor *emae);
 EAccount *	em_account_editor_get_original_account
 						(EMAccountEditor *emae);
+EMailSession *	em_account_editor_get_session	(EMAccountEditor *emae);
 void		em_account_editor_commit	(EMAccountEditor *emae);
 gboolean	em_account_editor_check		(EMAccountEditor *emae,
 						 const gchar *page);
diff --git a/mail/em-composer-utils.c b/mail/em-composer-utils.c
index b65237b..1114312 100644
--- a/mail/em-composer-utils.c
+++ b/mail/em-composer-utils.c
@@ -36,7 +36,6 @@
 #include "mail-ops.h"
 #include "mail-tools.h"
 #include "mail-config.h"
-#include "mail-session.h"
 #include "mail-send-recv.h"
 
 #include "e-util/e-account-utils.h"
@@ -46,6 +45,7 @@
 #include "shell/e-shell.h"
 
 #include "e-mail-local.h"
+#include "e-mail-session.h"
 #include "em-utils.h"
 #include "em-composer-utils.h"
 #include "composer/e-msg-composer.h"
@@ -221,11 +221,14 @@ static void
 composer_send_queued_cb (CamelFolder *folder, CamelMimeMessage *msg, CamelMessageInfo *info,
 			 gint queued, const gchar *appended_uid, gpointer data)
 {
+	CamelSession *session;
 	struct emcs_t *emcs;
 	struct _send_data *send = data;
 
 	emcs = send->emcs;
 
+	session = e_msg_composer_get_session (send->composer);
+
 	if (queued) {
 		if (emcs && emcs->drafts_folder) {
 			/* delete the old draft message */
@@ -257,7 +260,7 @@ composer_send_queued_cb (CamelFolder *folder, CamelMimeMessage *msg, CamelMessag
 
 		if (send->send && camel_session_get_online (session)) {
 			/* queue a message send */
-			mail_send ();
+			mail_send (E_MAIL_SESSION (session));
 		}
 	} else
 		gtk_widget_show (GTK_WIDGET (send->composer));
@@ -658,6 +661,7 @@ em_utils_composer_save_draft_cb (EMsgComposer *composer)
 	CamelFolder *folder = NULL;
 	CamelMimeMessage *msg;
 	CamelMessageInfo *info;
+	CamelSession *session;
 	EAccount *account;
 	GError *error = NULL;
 
@@ -665,6 +669,8 @@ em_utils_composer_save_draft_cb (EMsgComposer *composer)
 	 * get destroyed while we're in mail_msg_wait() a little lower
 	 * down, waiting for the folder to open */
 
+	session = e_msg_composer_get_session (composer);
+
 	local_drafts_folder =
 		e_mail_local_get_folder (E_MAIL_FOLDER_DRAFTS);
 	local_drafts_folder_uri =
@@ -704,7 +710,11 @@ em_utils_composer_save_draft_cb (EMsgComposer *composer)
 	    strcmp (account->drafts_folder_uri, local_drafts_folder_uri) != 0) {
 		gint id;
 
-		id = mail_get_folder (account->drafts_folder_uri, 0, save_draft_folder, &folder, mail_msg_unordered_push);
+		id = mail_get_folder (
+			E_MAIL_SESSION (session),
+			account->drafts_folder_uri, 0,
+			save_draft_folder, &folder,
+			mail_msg_unordered_push);
 		mail_msg_wait (id);
 
 		if (!folder || !account->enabled) {
@@ -1619,10 +1629,16 @@ em_utils_redirect_message_by_uid (EShell *shell,
 }
 
 static void
-emu_handle_receipt_message (CamelFolder *folder, const gchar *uid, CamelMimeMessage *msg, gpointer data, GError **error)
+emu_handle_receipt_message (CamelFolder *folder,
+                            const gchar *uid,
+                            CamelMimeMessage *msg,
+                            gpointer data,
+                            GError **error)
 {
+	EMailSession *session = E_MAIL_SESSION (data);
+
 	if (msg)
-		em_utils_handle_receipt (folder, uid, msg);
+		em_utils_handle_receipt (session, folder, uid, msg);
 
 	/* we dont care really if we can't get the message */
 	g_clear_error (error);
@@ -1630,7 +1646,10 @@ emu_handle_receipt_message (CamelFolder *folder, const gchar *uid, CamelMimeMess
 
 /* Message disposition notifications, rfc 2298 */
 void
-em_utils_handle_receipt (CamelFolder *folder, const gchar *uid, CamelMimeMessage *msg)
+em_utils_handle_receipt (EMailSession *session,
+                         CamelFolder *folder,
+                         const gchar *uid,
+                         CamelMimeMessage *msg)
 {
 	EAccount *account;
 	const gchar *addr;
@@ -1646,7 +1665,9 @@ em_utils_handle_receipt (CamelFolder *folder, const gchar *uid, CamelMimeMessage
 	}
 
 	if (msg == NULL) {
-		mail_get_messagex (folder, uid, emu_handle_receipt_message, NULL, mail_msg_unordered_push);
+		mail_get_messagex (
+			folder, uid, emu_handle_receipt_message,
+			session, mail_msg_unordered_push);
 		camel_folder_free_message_info (folder, info);
 		return;
 	}
@@ -1671,19 +1692,27 @@ em_utils_handle_receipt (CamelFolder *folder, const gchar *uid, CamelMimeMessage
 
 	if (account && (account->receipt_policy == E_ACCOUNT_RECEIPT_ALWAYS || account->receipt_policy == E_ACCOUNT_RECEIPT_ASK)
 	    && e_alert_run_dialog_for_args (e_shell_get_active_window (NULL), "mail:ask-receipt", addr, camel_mime_message_get_subject(msg), NULL) == GTK_RESPONSE_YES)
-		em_utils_send_receipt (folder, msg);
+		em_utils_send_receipt (session, folder, msg);
 }
 
 static void
-em_utils_receipt_done (CamelFolder *folder, CamelMimeMessage *msg, CamelMessageInfo *info,
-		       gint queued, const gchar *appended_uid, gpointer data)
+em_utils_receipt_done (CamelFolder *folder,
+                       CamelMimeMessage *msg,
+                       CamelMessageInfo *info,
+                       gint queued,
+                       const gchar *appended_uid,
+                       gpointer data)
 {
+	EMailSession *session = E_MAIL_SESSION (data);
+
 	camel_message_info_free (info);
-	mail_send ();
+	mail_send (session);
 }
 
 void
-em_utils_send_receipt (CamelFolder *folder, CamelMimeMessage *message)
+em_utils_send_receipt (EMailSession *session,
+                       CamelFolder *folder,
+                       CamelMimeMessage *message)
 {
 	/* See RFC #3798 for a description of message receipts */
 	EAccount *account = em_utils_guess_account_with_recipients (message, folder);
@@ -1815,7 +1844,8 @@ em_utils_send_receipt (CamelFolder *folder, CamelMimeMessage *message)
 	out_folder = e_mail_local_get_folder (E_MAIL_FOLDER_OUTBOX);
 	camel_message_info_set_flags (
 		info, CAMEL_MESSAGE_SEEN, CAMEL_MESSAGE_SEEN);
-	mail_append_mail (out_folder, receipt, info, em_utils_receipt_done, NULL);
+	mail_append_mail (
+		out_folder, receipt, info, em_utils_receipt_done, session);
 }
 
 /* Replying to messages... */
@@ -2584,11 +2614,14 @@ post_header_clicked_cb (EComposerPostHeader *header,
                         EMsgComposer *composer)
 {
 	GtkTreeSelection *selection;
+	CamelSession *session;
 	GtkWidget *folder_tree;
 	GtkWidget *dialog;
 	GList *list;
 
-	folder_tree = em_folder_tree_new ();
+	session = e_msg_composer_get_session (composer);
+
+	folder_tree = em_folder_tree_new (E_MAIL_SESSION (session));
 	emu_restore_folder_tree_state (EM_FOLDER_TREE (folder_tree));
 
 	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (folder_tree));
diff --git a/mail/em-composer-utils.h b/mail/em-composer-utils.h
index f1bee56..fa99186 100644
--- a/mail/em-composer-utils.h
+++ b/mail/em-composer-utils.h
@@ -25,6 +25,7 @@
 #define __EM_COMPOSER_UTILS_H__
 
 #include <em-format/em-format.h>
+#include <mail/e-mail-session.h>
 #include <composer/e-msg-composer.h>
 
 G_BEGIN_DECLS
@@ -70,10 +71,12 @@ void		em_utils_forward_message_raw	(CamelFolder *folder,
 						 CamelMimeMessage *message,
 						 const gchar *address,
 						 GError **error);
-void		em_utils_handle_receipt		(CamelFolder *folder,
+void		em_utils_handle_receipt		(EMailSession *session,
+						 CamelFolder *folder,
 						 const gchar *uid,
 						 CamelMimeMessage *msg);
-void		em_utils_send_receipt		(CamelFolder *folder,
+void		em_utils_send_receipt		(EMailSession *session,
+						 CamelFolder *folder,
 						 CamelMimeMessage *message);
 
 enum {
diff --git a/mail/em-filter-context.c b/mail/em-filter-context.c
index 6f6b634..2e6d19c 100644
--- a/mail/em-filter-context.c
+++ b/mail/em-filter-context.c
@@ -36,129 +36,96 @@
 /* For poking into filter-folder guts */
 #include "em-filter-folder-element.h"
 
-#define d(x)
+#define EM_FILTER_CONTEXT_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE \
+	((obj), EM_TYPE_FILTER_CONTEXT, EMFilterContextPrivate))
 
-static void em_filter_context_class_init (EMFilterContextClass *klass);
-static void em_filter_context_init (EMFilterContext *fc);
-static void em_filter_context_finalise (GObject *obj);
+struct _EMFilterContextPrivate {
+	EMailSession *session;
+	GList *actions;
+};
 
-static GList *filter_rename_uri (ERuleContext *rc, const gchar *olduri, const gchar *newuri, GCompareFunc cmp);
-static GList *filter_delete_uri (ERuleContext *rc, const gchar *uri, GCompareFunc cmp);
-static EFilterElement *filter_new_element (ERuleContext *rc, const gchar *name);
+enum {
+	PROP_0,
+	PROP_SESSION
+};
 
-static ERuleContextClass *parent_class = NULL;
-
-GType
-em_filter_context_get_type (void)
-{
-	static GType type = 0;
-
-	if (!type) {
-		static const GTypeInfo info = {
-			sizeof (EMFilterContextClass),
-			NULL, /* base_class_init */
-			NULL, /* base_class_finalize */
-			(GClassInitFunc) em_filter_context_class_init,
-			NULL, /* class_finalize */
-			NULL, /* class_data */
-			sizeof (EMFilterContext),
-			0,    /* n_preallocs */
-			(GInstanceInitFunc) em_filter_context_init,
-		};
-
-		type = g_type_register_static(E_TYPE_RULE_CONTEXT, "EMFilterContext", &info, 0);
-	}
-
-	return type;
-}
+G_DEFINE_TYPE (
+	EMFilterContext,
+	em_filter_context,
+	E_TYPE_RULE_CONTEXT)
 
 static void
-em_filter_context_class_init (EMFilterContextClass *klass)
+filter_context_set_session (EMFilterContext *context,
+                            EMailSession *session)
 {
-	GObjectClass *object_class = G_OBJECT_CLASS (klass);
-	ERuleContextClass *rc_class = E_RULE_CONTEXT_CLASS (klass);
-
-	parent_class = g_type_class_ref (E_TYPE_RULE_CONTEXT);
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+	g_return_if_fail (context->priv->session == NULL);
 
-	object_class->finalize = em_filter_context_finalise;
-
-	/* override methods */
-	rc_class->rename_uri = filter_rename_uri;
-	rc_class->delete_uri = filter_delete_uri;
-	rc_class->new_element = filter_new_element;
-}
-
-static void
-em_filter_context_init (EMFilterContext *fc)
-{
-	e_rule_context_add_part_set((ERuleContext *) fc, "partset", e_filter_part_get_type(),
-				   e_rule_context_add_part, e_rule_context_next_part);
-	e_rule_context_add_part_set((ERuleContext *) fc, "actionset", e_filter_part_get_type(),
-				  (ERuleContextPartFunc) em_filter_context_add_action,
-				  (ERuleContextNextPartFunc) em_filter_context_next_action);
-
-	e_rule_context_add_rule_set((ERuleContext *) fc, "ruleset", em_filter_rule_get_type(),
-				  (ERuleContextRuleFunc) e_rule_context_add_rule, e_rule_context_next_rule);
+	context->priv->session = g_object_ref (session);
 }
 
 static void
-em_filter_context_finalise (GObject *obj)
+filter_context_set_property (GObject *object,
+                             guint property_id,
+                             const GValue *value,
+                             GParamSpec *pspec)
 {
-	EMFilterContext *fc = (EMFilterContext *)obj;
-
-	g_list_foreach (fc->actions, (GFunc)g_object_unref, NULL);
-	g_list_free (fc->actions);
+	switch (property_id) {
+		case PROP_SESSION:
+			filter_context_set_session (
+				EM_FILTER_CONTEXT (object),
+				g_value_get_object (value));
+			return;
+	}
 
-        G_OBJECT_CLASS (parent_class)->finalize (obj);
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 }
 
-/**
- * em_filter_context_new:
- *
- * Create a new EMFilterContext object.
- *
- * Return value: A new #EMFilterContext object.
- **/
-EMFilterContext *
-em_filter_context_new (void)
+static void
+filter_context_get_property (GObject *object,
+                             guint property_id,
+                             GValue *value,
+                             GParamSpec *pspec)
 {
-	return (EMFilterContext *) g_object_new (em_filter_context_get_type (), NULL, NULL);
-}
+	switch (property_id) {
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				em_filter_context_get_session (
+				EM_FILTER_CONTEXT (object)));
+			return;
+	}
 
-void
-em_filter_context_add_action (EMFilterContext *fc, EFilterPart *action)
-{
-	d(printf("find action : "));
-	fc->actions = g_list_append (fc->actions, action);
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 }
 
-EFilterPart *
-em_filter_context_find_action (EMFilterContext *fc, const gchar *name)
+static void
+filter_context_dispose (GObject *object)
 {
-	d(printf("find action : "));
-	return e_filter_part_find_list (fc->actions, name);
-}
+	EMFilterContextPrivate *priv;
 
-EFilterPart *
-em_filter_context_create_action (EMFilterContext *fc, const gchar *name)
-{
-	EFilterPart *part;
+	priv = EM_FILTER_CONTEXT_GET_PRIVATE (object);
 
-	if ((part = em_filter_context_find_action (fc, name)))
-		return e_filter_part_clone (part);
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
 
-	return NULL;
-}
+	g_list_foreach (priv->actions, (GFunc) g_object_unref, NULL);
+	g_list_free (priv->actions);
 
-EFilterPart *
-em_filter_context_next_action (EMFilterContext *fc, EFilterPart *last)
-{
-	return e_filter_part_next_list (fc->actions, last);
+	/* Chain up to parent's dispose() method. */
+	G_OBJECT_CLASS (em_filter_context_parent_class)->dispose (object);
 }
 
-/* We search for any folders in our actions list that need updating, update them */
+/* We search for any folders in our actions list that need updating
+ * and update them. */
 static GList *
-filter_rename_uri (ERuleContext *rc, const gchar *olduri, const gchar *newuri, GCompareFunc cmp)
+filter_context_rename_uri (ERuleContext *context,
+                           const gchar *olduri,
+                           const gchar *newuri,
+                           GCompareFunc cmp)
 {
 	EFilterRule *rule;
 	GList *l, *el;
@@ -167,36 +134,24 @@ filter_rename_uri (ERuleContext *rc, const gchar *olduri, const gchar *newuri, G
 	gint count = 0;
 	GList *changed = NULL;
 
-	d(printf("uri '%s' renamed to '%s'\n", olduri, newuri));
-
-	/* For all rules, for all actions, for all elements, rename any folder elements */
-	/* Yes we could do this inside each part itself, but not today */
+	/* For all rules, for all actions, for all elements, rename any
+	 * folder elements.  XXX Yes we could do this inside each part
+	 * itself, but not today. */
 	rule = NULL;
-	while ((rule = e_rule_context_next_rule (rc, rule, NULL))) {
+	while ((rule = e_rule_context_next_rule (context, rule, NULL))) {
 		gint rulecount = 0;
 
-		d(printf("checking rule '%s'\n", rule->name));
-
 		l = EM_FILTER_RULE (rule)->actions;
 		while (l) {
 			action = l->data;
 
-			d(printf("checking action '%s'\n", action->name));
-
 			el = action->elements;
 			while (el) {
 				element = el->data;
 
-				d(printf("checking element '%s'\n", element->name));
-				if (EM_IS_FILTER_FOLDER_ELEMENT (element)) {
-					d(printf(" is folder, existing uri = '%s'\n",
-						 FILTER_FOLDER (element)->uri));
-				}
-
 				if (EM_IS_FILTER_FOLDER_ELEMENT (element)
-				    && cmp (((EMFilterFolderElement *)element)->uri, olduri)) {
-					d(printf(" Changed!\n"));
-					em_filter_folder_element_set_value ((EMFilterFolderElement *)element, newuri);
+				    && cmp (em_filter_folder_element_get_uri (EM_FILTER_FOLDER_ELEMENT (element)), olduri)) {
+					em_filter_folder_element_set_uri (EM_FILTER_FOLDER_ELEMENT (element), newuri);
 					rulecount++;
 				}
 				el = el->next;
@@ -212,16 +167,17 @@ filter_rename_uri (ERuleContext *rc, const gchar *olduri, const gchar *newuri, G
 		count += rulecount;
 	}
 
-	/* might need to call parent class, if it did anything ... parent_class->rename_uri(f, olduri, newuri, cmp); */
-
 	return changed;
 }
 
 static GList *
-filter_delete_uri (ERuleContext *rc, const gchar *uri, GCompareFunc cmp)
+filter_context_delete_uri (ERuleContext *context,
+                           const gchar *uri,
+                           GCompareFunc cmp)
 {
 	/* We basically do similar to above, but when we find it,
-	   Remove the action, and if thats the last action, this might create an empty rule?  remove the rule? */
+	 * remove the action, and if thats the last action, this
+	 * might create an empty rule?  Remove the rule? */
 
 	EFilterRule *rule;
 	GList *l, *el;
@@ -230,35 +186,23 @@ filter_delete_uri (ERuleContext *rc, const gchar *uri, GCompareFunc cmp)
 	gint count = 0;
 	GList *deleted = NULL;
 
-	d(printf("uri '%s' deleted\n", uri));
-
-	/* For all rules, for all actions, for all elements, check deleted folder elements */
-	/* Yes we could do this inside each part itself, but not today */
+	/* For all rules, for all actions, for all elements, check
+	 * deleted folder elements. XXX  Yes we could do this inside
+	 * each part itself, but not today. */
 	rule = NULL;
-	while ((rule = e_rule_context_next_rule (rc, rule, NULL))) {
+	while ((rule = e_rule_context_next_rule (context, rule, NULL))) {
 		gint recorded = 0;
 
-		d(printf("checking rule '%s'\n", rule->name));
-
 		l = EM_FILTER_RULE (rule)->actions;
 		while (l) {
 			action = l->data;
 
-			d(printf("checking action '%s'\n", action->name));
-
 			el = action->elements;
 			while (el) {
 				element = el->data;
 
-				d(printf("checking element '%s'\n", element->name));
-				if (EM_IS_FILTER_FOLDER_ELEMENT (element)) {
-					d(printf(" is folder, existing uri = '%s'\n",
-						 FILTER_FOLDER (element)->uri));
-				}
-
 				if (EM_IS_FILTER_FOLDER_ELEMENT (element)
-				    && cmp (((EMFilterFolderElement *)element)->uri, uri)) {
-					d(printf(" Deleted!\n"));
+				    && cmp (em_filter_folder_element_get_uri (EM_FILTER_FOLDER_ELEMENT (element)), uri)) {
 					/* check if last action, if so, remove rule instead? */
 					l = l->next;
 					em_filter_rule_remove_action ((EMFilterRule *)rule, action);
@@ -276,23 +220,135 @@ filter_delete_uri (ERuleContext *rc, const gchar *uri, GCompareFunc cmp)
 		}
 	}
 
-	/* TODO: could call parent and merge lists */
-
 	return deleted;
 }
 
 static EFilterElement *
-filter_new_element (ERuleContext *rc, const gchar *type)
+filter_context_new_element (ERuleContext *context,
+                            const gchar *type)
 {
-	if (!strcmp(type, "folder")) {
-		return (EFilterElement *) em_filter_folder_element_new ();
-	} else if (!strcmp(type, "system-flag")) {
-		return (EFilterElement *) e_filter_option_new ();
-	} else if (!strcmp(type, "score")) {
-		return (EFilterElement *) e_filter_int_new_type("score", -3, 3);
-	} else if (!strcmp(type, "source")) {
-		return (EFilterElement *) em_filter_source_element_new ();
-	} else {
-		return parent_class->new_element (rc, type);
-	}
+	EMFilterContextPrivate *priv;
+
+	priv = EM_FILTER_CONTEXT_GET_PRIVATE (context);
+
+	if (strcmp (type, "folder") == 0)
+		return em_filter_folder_element_new (priv->session);
+
+	if (strcmp (type, "system-flag") == 0)
+		return e_filter_option_new ();
+
+	if (strcmp (type, "score") == 0)
+		return e_filter_int_new_type ("score", -3, 3);
+
+	if (strcmp (type, "source") == 0)
+		return em_filter_source_element_new ();
+
+	return E_RULE_CONTEXT_CLASS (em_filter_context_parent_class)->
+		new_element (context, type);
+}
+
+static void
+em_filter_context_class_init (EMFilterContextClass *class)
+{
+	GObjectClass *object_class;
+	ERuleContextClass *rule_context_class;
+
+	g_type_class_add_private (class, sizeof (EMFilterContextPrivate));
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->set_property = filter_context_set_property;
+	object_class->get_property = filter_context_get_property;
+	object_class->dispose = filter_context_dispose;
+
+	rule_context_class = E_RULE_CONTEXT_CLASS (class);
+	rule_context_class->rename_uri = filter_context_rename_uri;
+	rule_context_class->delete_uri = filter_context_delete_uri;
+	rule_context_class->new_element = filter_context_new_element;
+
+	g_object_class_install_property (
+		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			NULL,
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
+}
+
+static void
+em_filter_context_init (EMFilterContext *context)
+{
+	context->priv = EM_FILTER_CONTEXT_GET_PRIVATE (context);
+
+	e_rule_context_add_part_set (
+		E_RULE_CONTEXT (context),
+		"partset", E_TYPE_FILTER_PART,
+		(ERuleContextPartFunc) e_rule_context_add_part,
+		(ERuleContextNextPartFunc) e_rule_context_next_part);
+
+	e_rule_context_add_part_set (
+		E_RULE_CONTEXT (context),
+		"actionset", E_TYPE_FILTER_PART,
+		(ERuleContextPartFunc) em_filter_context_add_action,
+		(ERuleContextNextPartFunc) em_filter_context_next_action);
+
+	e_rule_context_add_rule_set (
+		E_RULE_CONTEXT (context),
+		"ruleset", EM_TYPE_FILTER_RULE,
+		(ERuleContextRuleFunc) e_rule_context_add_rule,
+		(ERuleContextNextRuleFunc) e_rule_context_next_rule);
+}
+
+EMFilterContext *
+em_filter_context_new (EMailSession *session)
+{
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
+	return g_object_new (
+		EM_TYPE_FILTER_CONTEXT, "session", session, NULL);
+}
+
+EMailSession *
+em_filter_context_get_session (EMFilterContext *context)
+{
+	g_return_val_if_fail (EM_IS_FILTER_CONTEXT (context), NULL);
+
+	return context->priv->session;
 }
+
+void
+em_filter_context_add_action (EMFilterContext *context,
+                              EFilterPart *action)
+{
+	context->priv->actions =
+		g_list_append (context->priv->actions, action);
+}
+
+EFilterPart *
+em_filter_context_find_action (EMFilterContext *context,
+                               const gchar *name)
+{
+	return e_filter_part_find_list (context->priv->actions, name);
+}
+
+EFilterPart *
+em_filter_context_create_action (EMFilterContext *context,
+                                 const gchar *name)
+{
+	EFilterPart *part;
+
+	if ((part = em_filter_context_find_action (context, name)))
+		return e_filter_part_clone (part);
+
+	return NULL;
+}
+
+EFilterPart *
+em_filter_context_next_action (EMFilterContext *context,
+                               EFilterPart *last)
+{
+	return e_filter_part_next_list (context->priv->actions, last);
+}
+
diff --git a/mail/em-filter-context.h b/mail/em-filter-context.h
index 201f8ff..3545bf1 100644
--- a/mail/em-filter-context.h
+++ b/mail/em-filter-context.h
@@ -22,38 +22,59 @@
  *
  */
 
-#ifndef _EM_FILTER_CONTEXT_H
-#define _EM_FILTER_CONTEXT_H
+#ifndef EM_FILTER_CONTEXT_H
+#define EM_FILTER_CONTEXT_H
 
-#include "filter/e-rule-context.h"
+#include <mail/e-mail-session.h>
+#include <filter/e-rule-context.h>
 
-#define EM_TYPE_FILTER_CONTEXT            (em_filter_context_get_type ())
-#define EM_FILTER_CONTEXT(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), FILTER_TYPE_CONTEXT, EMFilterContext))
-#define EM_FILTER_CONTEXT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), FILTER_TYPE_CONTEXT, EMFilterContextClass))
-#define EM_IS_FILTER_CONTEXT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FILTER_TYPE_CONTEXT))
-#define EM_IS_FILTER_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), FILTER_TYPE_CONTEXT))
-#define EM_FILTER_CONTEXT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), FILTER_TYPE_CONTEXT, EMFilterContextClass))
+/* Standard GObject macros */
+#define EM_TYPE_FILTER_CONTEXT \
+	(em_filter_context_get_type ())
+#define EM_FILTER_CONTEXT(obj) \
+	(G_TYPE_CHECK_INSTANCE_CAST \
+	((obj), EM_TYPE_FILTER_CONTEXT, EMFilterContext))
+#define EM_FILTER_CONTEXT_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_CAST \
+	((cls), EM_TYPE_FILTER_CONTEXT, EMFilterContextClass))
+#define EM_IS_FILTER_CONTEXT(obj) \
+	(G_TYPE_CHECK_INSTANCE_TYPE \
+	((obj), EM_TYPE_FILTER_CONTEXT))
+#define EM_IS_FILTER_CONTEXT_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_TYPE \
+	((cls), EM_TYPE_FILTER_CONTEXT))
+#define EM_FILTER_CONTEXT_GET_CLASS(obj) \
+	(G_TYPE_INSTANCE_GET_CLASS \
+	((obj), EM_TYPE_FILTER_CONTEXT, EMFilterContextClass))
+
+G_BEGIN_DECLS
 
 typedef struct _EMFilterContext EMFilterContext;
 typedef struct _EMFilterContextClass EMFilterContextClass;
+typedef struct _EMFilterContextPrivate EMFilterContextPrivate;
 
 struct _EMFilterContext {
-	ERuleContext parent_object;
-
-	GList *actions;
+	ERuleContext parent;
+	EMFilterContextPrivate *priv;
 };
 
 struct _EMFilterContextClass {
 	ERuleContextClass parent_class;
 };
 
-GType em_filter_context_get_type (void);
-EMFilterContext *em_filter_context_new (void);
+GType		em_filter_context_get_type	(void);
+EMFilterContext *
+		em_filter_context_new		(EMailSession *session);
+EMailSession *	em_filter_context_get_session	(EMFilterContext *context);
+void		em_filter_context_add_action	(EMFilterContext *context,
+						 EFilterPart *action);
+EFilterPart *	em_filter_context_find_action	(EMFilterContext *context,
+						 const gchar *name);
+EFilterPart *	em_filter_context_create_action	(EMFilterContext *context,
+						 const gchar *name);
+EFilterPart *	em_filter_context_next_action	(EMFilterContext *context,
+						 EFilterPart *last);
 
-/* methods */
-void em_filter_context_add_action (EMFilterContext *fc, EFilterPart *action);
-EFilterPart *em_filter_context_find_action (EMFilterContext *fc, const gchar *name);
-EFilterPart *em_filter_context_create_action (EMFilterContext *fc, const gchar *name);
-EFilterPart *em_filter_context_next_action (EMFilterContext *fc, EFilterPart *last);
+G_END_DECLS
 
-#endif /* _EM_FILTER_CONTEXT_H */
+#endif /* EM_FILTER_CONTEXT_H */
diff --git a/mail/em-filter-folder-element.c b/mail/em-filter-folder-element.c
index 5092849..62ce681 100644
--- a/mail/em-filter-folder-element.c
+++ b/mail/em-filter-folder-element.c
@@ -38,11 +38,22 @@
 #include "libedataserver/e-sexp.h"
 #include "e-util/e-alert.h"
 
-#define d(x)
+#define EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE \
+	((obj), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElementPrivate))
+
+struct _EMFilterFolderElementPrivate {
+	EMailSession *session;
+	gchar *uri;
+};
+
+enum {
+	PROP_0,
+	PROP_SESSION
+};
 
 static gboolean validate (EFilterElement *fe, EAlert **alert);
 static gint folder_eq (EFilterElement *fe, EFilterElement *cm);
-static void xml_create (EFilterElement *fe, xmlNodePtr node);
 static xmlNodePtr xml_encode (EFilterElement *fe);
 static gint xml_decode (EFilterElement *fe, xmlNodePtr node);
 static GtkWidget *get_widget (EFilterElement *fe);
@@ -50,92 +61,161 @@ static void build_code (EFilterElement *fe, GString *out, EFilterPart *ff);
 static void format_sexp (EFilterElement *, GString *);
 static void emff_copy_value (EFilterElement *de, EFilterElement *se);
 
-static void em_filter_folder_element_class_init (EMFilterFolderElementClass *class);
-static void em_filter_folder_element_init (EMFilterFolderElement *ff);
-static void em_filter_folder_element_finalise (GObject *obj);
+G_DEFINE_TYPE (
+	EMFilterFolderElement,
+	em_filter_folder_element,
+	E_TYPE_FILTER_ELEMENT)
 
-static EFilterElementClass *parent_class = NULL;
+static void
+filter_folder_element_set_session (EMFilterFolderElement *element,
+                                   EMailSession *session)
+{
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+	g_return_if_fail (element->priv->session == NULL);
 
-GType
-em_filter_folder_element_get_type (void)
+	element->priv->session = g_object_ref (session);
+}
+
+static void
+filter_folder_element_set_property (GObject *object,
+                                    guint property_id,
+                                    const GValue *value,
+                                    GParamSpec *pspec)
 {
-	static GType type = 0;
-
-	if (!type) {
-		static const GTypeInfo info = {
-			sizeof (EMFilterFolderElementClass),
-			NULL, /* base_class_init */
-			NULL, /* base_class_finalize */
-			(GClassInitFunc)em_filter_folder_element_class_init,
-			NULL, /* class_finalize */
-			NULL, /* class_data */
-			sizeof (EMFilterFolderElement),
-			0,    /* n_preallocs */
-			(GInstanceInitFunc)em_filter_folder_element_init,
-		};
-
-		type = g_type_register_static(E_TYPE_FILTER_ELEMENT, "EMFilterFolderElement", &info, 0);
+	switch (property_id) {
+		case PROP_SESSION:
+			filter_folder_element_set_session (
+				EM_FILTER_FOLDER_ELEMENT (object),
+				g_value_get_object (value));
+			return;
 	}
 
-	return type;
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 }
 
 static void
-em_filter_folder_element_class_init (EMFilterFolderElementClass *klass)
+filter_folder_element_get_property (GObject *object,
+                                    guint property_id,
+                                    GValue *value,
+                                    GParamSpec *pspec)
 {
-	GObjectClass *object_class = G_OBJECT_CLASS (klass);
-	EFilterElementClass *fe_class = E_FILTER_ELEMENT_CLASS (klass);
-
-	parent_class = g_type_class_ref (E_TYPE_FILTER_ELEMENT);
-
-	object_class->finalize = em_filter_folder_element_finalise;
-
-	/* override methods */
-	fe_class->validate = validate;
-	fe_class->eq = folder_eq;
-	fe_class->xml_create = xml_create;
-	fe_class->xml_encode = xml_encode;
-	fe_class->xml_decode = xml_decode;
-	fe_class->get_widget = get_widget;
-	fe_class->build_code = build_code;
-	fe_class->format_sexp = format_sexp;
-	fe_class->copy_value = emff_copy_value;
+	switch (property_id) {
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				em_filter_folder_element_get_session (
+				EM_FILTER_FOLDER_ELEMENT (object)));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 }
 
 static void
-em_filter_folder_element_init (EMFilterFolderElement *ff)
+filter_folder_element_dispose (GObject *object)
 {
-	;
+	EMFilterFolderElementPrivate *priv;
+
+	priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (object);
+
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
+	/* Chain up to parent's dispose() method. */
+	G_OBJECT_CLASS (em_filter_folder_element_parent_class)->dispose (object);
 }
 
 static void
-em_filter_folder_element_finalise (GObject *obj)
+filter_folder_element_finalize (GObject *object)
 {
-	EMFilterFolderElement *ff = (EMFilterFolderElement *)obj;
+	EMFilterFolderElementPrivate *priv;
 
-	g_free (ff->uri);
+	priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (object);
 
-        G_OBJECT_CLASS (parent_class)->finalize (obj);
+	g_free (priv->uri);
+
+	/* Chain up to parent's finalize() method. */
+        G_OBJECT_CLASS (em_filter_folder_element_parent_class)->finalize (object);
 }
 
-/**
- * em_filter_folder_element_new:
- *
- * Create a new EMFilterFolderElement object.
- *
- * Return value: A new #EMFilterFolderElement object.
- **/
-EMFilterFolderElement *
-em_filter_folder_element_new (void)
+static void
+em_filter_folder_element_class_init (EMFilterFolderElementClass *class)
+{
+	GObjectClass *object_class;
+	EFilterElementClass *filter_element_class;
+
+	g_type_class_add_private (class, sizeof (EMFilterFolderElementPrivate));
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->set_property = filter_folder_element_set_property;
+	object_class->get_property = filter_folder_element_get_property;
+	object_class->dispose = filter_folder_element_dispose;
+	object_class->finalize = filter_folder_element_finalize;
+
+	filter_element_class = E_FILTER_ELEMENT_CLASS (class);
+	filter_element_class->validate = validate;
+	filter_element_class->eq = folder_eq;
+	filter_element_class->xml_encode = xml_encode;
+	filter_element_class->xml_decode = xml_decode;
+	filter_element_class->get_widget = get_widget;
+	filter_element_class->build_code = build_code;
+	filter_element_class->format_sexp = format_sexp;
+	filter_element_class->copy_value = emff_copy_value;
+
+	g_object_class_install_property (
+		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			NULL,
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
+}
+
+static void
+em_filter_folder_element_init (EMFilterFolderElement *element)
+{
+	element->priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (element);
+}
+
+EFilterElement *
+em_filter_folder_element_new (EMailSession *session)
 {
-	return (EMFilterFolderElement *)g_object_new (em_filter_folder_element_get_type (), NULL, NULL);
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
+	return g_object_new (
+		EM_TYPE_FILTER_FOLDER_ELEMENT,
+		"session", session, NULL);
+}
+
+EMailSession *
+em_filter_folder_element_get_session (EMFilterFolderElement *element)
+{
+	g_return_val_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element), NULL);
+
+	return element->priv->session;
+}
+
+const gchar *
+em_filter_folder_element_get_uri (EMFilterFolderElement *element)
+{
+	g_return_val_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element), NULL);
+
+	return element->priv->uri;
 }
 
 void
-em_filter_folder_element_set_value (EMFilterFolderElement *ff, const gchar *uri)
+em_filter_folder_element_set_uri (EMFilterFolderElement *element,
+                                  const gchar *uri)
 {
-	g_free (ff->uri);
-	ff->uri = g_strdup (uri);
+	g_return_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element));
+
+	g_free (element->priv->uri);
+	element->priv->uri = g_strdup (uri);
 }
 
 static gboolean
@@ -145,7 +225,7 @@ validate (EFilterElement *fe, EAlert **alert)
 
 	g_warn_if_fail (alert == NULL || *alert == NULL);
 
-	if (ff->uri && *ff->uri) {
+	if (ff->priv->uri && *ff->priv->uri) {
 		return TRUE;
 	} else {
 		if (alert)
@@ -158,15 +238,8 @@ validate (EFilterElement *fe, EAlert **alert)
 static gint
 folder_eq (EFilterElement *fe, EFilterElement *cm)
 {
-        return E_FILTER_ELEMENT_CLASS (parent_class)->eq (fe, cm)
-		&& strcmp (((EMFilterFolderElement *)fe)->uri, ((EMFilterFolderElement *)cm)->uri)== 0;
-}
-
-static void
-xml_create (EFilterElement *fe, xmlNodePtr node)
-{
-	/* parent implementation */
-        E_FILTER_ELEMENT_CLASS (parent_class)->xml_create (fe, node);
+        return E_FILTER_ELEMENT_CLASS (em_filter_folder_element_parent_class)->eq (fe, cm)
+		&& strcmp (((EMFilterFolderElement *)fe)->priv->uri, ((EMFilterFolderElement *)cm)->priv->uri)== 0;
 }
 
 static xmlNodePtr
@@ -175,8 +248,6 @@ xml_encode (EFilterElement *fe)
 	xmlNodePtr value, work;
 	EMFilterFolderElement *ff = (EMFilterFolderElement *)fe;
 
-	d(printf("Encoding folder as xml\n"));
-
 	value = xmlNewNode(NULL, (const guchar *)"value");
 	xmlSetProp(value, (const guchar *)"name", (guchar *)fe->name);
 	if (ff->store_camel_uri)
@@ -185,7 +256,7 @@ xml_encode (EFilterElement *fe)
 		xmlSetProp(value, (const guchar *)"type", (const guchar *)"folder");
 
 	work = xmlNewChild(value, NULL, (const guchar *)"folder", NULL);
-	xmlSetProp(work, (const guchar *)"uri", (const guchar *)ff->uri);
+	xmlSetProp(work, (const guchar *)"uri", (const guchar *)ff->priv->uri);
 
 	return value;
 }
@@ -197,8 +268,6 @@ xml_decode (EFilterElement *fe, xmlNodePtr node)
 	xmlNodePtr n;
 	xmlChar *type;
 
-	d(printf("Decoding folder from xml %p\n", fe));
-
 	xmlFree (fe->name);
 	fe->name = (gchar *)xmlGetProp(node, (const guchar *)"name");
 
@@ -216,8 +285,8 @@ xml_decode (EFilterElement *fe, xmlNodePtr node)
 			gchar *uri;
 
 			uri = (gchar *)xmlGetProp(n, (const guchar *)"uri");
-			g_free (ff->uri);
-			ff->uri = g_strdup (uri);
+			g_free (ff->priv->uri);
+			ff->priv->uri = g_strdup (uri);
 			xmlFree (uri);
 			break;
 		}
@@ -234,12 +303,12 @@ folder_selected (EMFolderSelectionButton *button, EMFilterFolderElement *ff)
 	const gchar *uri;
 
 	uri = em_folder_selection_button_get_selection (button);
-	g_free (ff->uri);
+	g_free (ff->priv->uri);
 
 	if (ff->store_camel_uri)
-		ff->uri = g_strdup (uri);
+		ff->priv->uri = g_strdup (uri);
 	else
-		ff->uri = uri != NULL ? em_uri_from_camel (uri) : NULL;
+		ff->priv->uri = uri != NULL ? em_uri_from_camel (uri) : NULL;
 
 	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
 	gtk_window_present (GTK_WINDOW (toplevel));
@@ -249,15 +318,19 @@ static GtkWidget *
 get_widget (EFilterElement *fe)
 {
 	EMFilterFolderElement *ff = (EMFilterFolderElement *)fe;
+	EMailSession *session;
 	GtkWidget *button;
 	gchar *uri;
 
+	session = em_filter_folder_element_get_session (ff);
+
 	if (ff->store_camel_uri)
-		uri = ff->uri;
+		uri = ff->priv->uri;
 	else
-		uri = em_uri_to_camel (ff->uri);
+		uri = em_uri_to_camel (ff->priv->uri);
 
-	button = em_folder_selection_button_new (_("Select Folder"), NULL);
+	button = em_folder_selection_button_new (
+		session, _("Select Folder"), NULL);
 
 	em_folder_selection_button_set_selection (
 		EM_FOLDER_SELECTION_BUTTON (button), uri);
@@ -282,7 +355,7 @@ format_sexp (EFilterElement *fe, GString *out)
 {
 	EMFilterFolderElement *ff = (EMFilterFolderElement *)fe;
 
-	e_sexp_encode_string (out, ff->uri);
+	e_sexp_encode_string (out, ff->priv->uri);
 }
 
 static void
@@ -290,7 +363,7 @@ emff_copy_value (EFilterElement *de, EFilterElement *se)
 {
 	if (EM_IS_FILTER_FOLDER_ELEMENT (se)) {
 		((EMFilterFolderElement *)de)->store_camel_uri = ((EMFilterFolderElement *)se)->store_camel_uri;
-		em_filter_folder_element_set_value ((EMFilterFolderElement *)de, ((EMFilterFolderElement *)se)->uri);
+		em_filter_folder_element_set_uri ((EMFilterFolderElement *)de, ((EMFilterFolderElement *)se)->priv->uri);
 	} else
-		parent_class->copy_value (de, se);
+		E_FILTER_ELEMENT_CLASS (em_filter_folder_element_parent_class)->copy_value (de, se);
 }
diff --git a/mail/em-filter-folder-element.h b/mail/em-filter-folder-element.h
index 2aae15e..95061be 100644
--- a/mail/em-filter-folder-element.h
+++ b/mail/em-filter-folder-element.h
@@ -16,41 +16,66 @@
  *
  * Authors:
  *		Not Zed <notzed lostzed mmc com au>
- *      Jeffrey Stedfast <fejj ximian com>
+ *      Jeelementrey Stedfast <fejj ximian com>
  *
  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
  *
  */
 
-#ifndef _EM_FILTER_FOLDER_ELEMENT_H
-#define _EM_FILTER_FOLDER_ELEMENT_H
+#ifndef EM_FILTER_FOLDER_ELEMENT_H
+#define EM_FILTER_FOLDER_ELEMENT_H
 
-#include "filter/e-filter-element.h"
+#include <mail/e-mail-session.h>
+#include <filter/e-filter-element.h>
 
-#define EM_FILTER_FOLDER_ELEMENT(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), em_filter_folder_element_get_type(), EMFilterFolderElement))
-#define EM_FILTER_FOLDER_ELEMENT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), em_filter_folder_element_get_type(), EMFilterFolderElementClass))
-#define EM_IS_FILTER_FOLDER_ELEMENT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), em_filter_folder_element_get_type()))
-#define EM_IS_FILTER_FOLDER_ELEMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), em_filter_folder_element_get_type()))
-#define EM_FILTER_FOLDER_ELEMENT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), em_filter_folder_element_get_type(), EMFilterFolderElementClass))
+/* Standard GObject macros */
+#define EM_TYPE_FILTER_FOLDER_ELEMENT \
+	(em_filter_folder_element_get_type ())
+#define EM_FILTER_FOLDER_ELEMENT(obj) \
+	(G_TYPE_CHECK_INSTANCE_CAST \
+	((obj), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElement))
+#define EM_FILTER_FOLDER_ELEMENT_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_CAST \
+	((cls), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElementClass))
+#define EM_IS_FILTER_FOLDER_ELEMENT(obj) \
+	(G_TYPE_CHECK_INSTANCE_TYPE \
+	((obj), EM_TYPE_FILTER_FOLDER_ELEMENT))
+#define EM_IS_FILTER_FOLDER_ELEMENT_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_TYPE \
+	((cls), EM_TYPE_FILTER_FOLDER_ELEMENT))
+#define EM_FILTER_FOLDER_ELEMENT_GET_CLASS(obj) \
+	(G_TYPE_INSTANCE_GET_CLASS \
+	((obj), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElementClass))
+
+G_BEGIN_DECLS
 
 typedef struct _EMFilterFolderElement EMFilterFolderElement;
 typedef struct _EMFilterFolderElementClass EMFilterFolderElementClass;
+typedef struct _EMFilterFolderElementPrivate EMFilterFolderElementPrivate;
 
 struct _EMFilterFolderElement {
-	EFilterElement parent_object;
+	EFilterElement parent;
+	EMFilterFolderElementPrivate *priv;
 
-	gchar *uri;
-	gboolean store_camel_uri; /* true if uri should contain camel uri, otherwise contains evolution's uri with an Account ID */
+	/* TRUE if 'uri' should contain Camel URI, otherwise
+	 * contains Evolution's URI with an Account ID. */
+	gboolean store_camel_uri;
 };
 
 struct _EMFilterFolderElementClass {
 	EFilterElementClass parent_class;
 };
 
-GType em_filter_folder_element_get_type (void);
-EMFilterFolderElement *em_filter_folder_element_new (void);
+GType		em_filter_folder_element_get_type (void);
+EFilterElement *em_filter_folder_element_new	(EMailSession *session);
+EMailSession *	em_filter_folder_element_get_session
+						(EMFilterFolderElement *element);
+const gchar *	em_filter_folder_element_get_uri
+						(EMFilterFolderElement *element);
+void		em_filter_folder_element_set_uri
+						(EMFilterFolderElement *element,
+						 const gchar *uri);
 
-/* methods */
-void em_filter_folder_element_set_value (EMFilterFolderElement *ff, const gchar *uri);
+G_END_DECLS
 
-#endif /* _EM_FILTER_FOLDER_ELEMENT_H */
+#endif /* EM_FILTER_FOLDER_ELEMENT_H */
diff --git a/mail/em-filter-rule.c b/mail/em-filter-rule.c
index 9d5ba0a..316f1ad 100644
--- a/mail/em-filter-rule.c
+++ b/mail/em-filter-rule.c
@@ -126,10 +126,10 @@ em_filter_rule_finalise (GObject *obj)
  *
  * Return value: A new #EMFilterRule object.
  **/
-EMFilterRule *
+EFilterRule *
 em_filter_rule_new (void)
 {
-	return (EMFilterRule *)g_object_new (em_filter_rule_get_type (), NULL, NULL);
+	return g_object_new (em_filter_rule_get_type (), NULL, NULL);
 }
 
 void
diff --git a/mail/em-filter-rule.h b/mail/em-filter-rule.h
index 6e75dc7..3b5fbd7 100644
--- a/mail/em-filter-rule.h
+++ b/mail/em-filter-rule.h
@@ -22,16 +22,18 @@
  *
  */
 
-#ifndef _EM_FILTER_RULE_H
-#define _EM_FILTER_RULE_H
+#ifndef EM_FILTER_RULE_H
+#define EM_FILTER_RULE_H
 
 #include "filter/e-filter-rule.h"
 
-#define EM_FILTER_RULE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), em_filter_rule_get_type(), EMFilterRule))
-#define EM_FILTER_RULE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), em_filter_rule_get_type(), EMFilterRuleClass))
-#define EM_IS_FILTER_RULE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), em_filter_rule_get_type()))
-#define EM_IS_FILTER_RULE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), em_filter_rule_get_type()))
-#define EM_FILTER_RULE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), em_filter_rule_get_type(), EMFilterRuleClass))
+#define EM_TYPE_FILTER_RULE \
+	(em_filter_rule_get_type ())
+#define EM_FILTER_RULE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), EM_TYPE_FILTER_RULE, EMFilterRule))
+#define EM_FILTER_RULE_CLASS(cls)    (G_TYPE_CHECK_CLASS_CAST ((cls), EM_TYPE_FILTER_RULE, EMFilterRuleClass))
+#define EM_IS_FILTER_RULE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EM_TYPE_FILTER_RULE))
+#define EM_IS_FILTER_RULE_CLASS(cls) (G_TYPE_CHECK_CLASS_TYPE ((cls), EM_TYPE_FILTER_RULE))
+#define EM_FILTER_RULE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), EM_TYPE_FILTER_RULE, EMFilterRuleClass))
 
 typedef struct _EMFilterRule EMFilterRule;
 typedef struct _EMFilterRuleClass EMFilterRuleClass;
@@ -47,7 +49,7 @@ struct _EMFilterRuleClass {
 };
 
 GType           em_filter_rule_get_type (void);
-EMFilterRule   *em_filter_rule_new      (void);
+EFilterRule *	em_filter_rule_new      (void);
 
 /* methods */
 void            em_filter_rule_add_action     (EMFilterRule *fr, EFilterPart *fp);
@@ -56,4 +58,4 @@ void            em_filter_rule_replace_action (EMFilterRule *fr, EFilterPart *fp
 
 void            em_filter_rule_build_action   (EMFilterRule *fr, GString *out);
 
-#endif /* _EM_FILTER_RULE_H */
+#endif /* EM_FILTER_RULE_H */
diff --git a/mail/em-filter-source-element.c b/mail/em-filter-source-element.c
index 9b30722..1d56cc1 100644
--- a/mail/em-filter-source-element.c
+++ b/mail/em-filter-source-element.c
@@ -144,10 +144,10 @@ em_filter_source_element_finalize (GObject *obj)
 	G_OBJECT_CLASS (parent_class)->finalize (obj);
 }
 
-EMFilterSourceElement *
+EFilterElement *
 em_filter_source_element_new (void)
 {
-	return (EMFilterSourceElement *)g_object_new (em_filter_source_element_get_type (), NULL, NULL);
+	return g_object_new (em_filter_source_element_get_type (), NULL, NULL);
 }
 
 static gint
@@ -215,9 +215,10 @@ static EFilterElement *
 filter_clone (EFilterElement *fe)
 {
 	EMFilterSourceElement *fs = (EMFilterSourceElement *)fe;
-	EMFilterSourceElement *cpy = em_filter_source_element_new ();
+	EMFilterSourceElement *cpy;
 	GList *i;
 
+	cpy = (EMFilterSourceElement *) em_filter_source_element_new ();
 	((EFilterElement *)cpy)->name = (gchar *)xmlStrdup ((guchar *)fe->name);
 
 	cpy->priv->current_url = g_strdup (fs->priv->current_url);
diff --git a/mail/em-filter-source-element.h b/mail/em-filter-source-element.h
index cb68552..5940a22 100644
--- a/mail/em-filter-source-element.h
+++ b/mail/em-filter-source-element.h
@@ -45,7 +45,7 @@ struct _EMFilterSourceElementClass {
 };
 
 GType em_filter_source_element_get_type (void);
-EMFilterSourceElement *em_filter_source_element_new (void);
+EFilterElement *em_filter_source_element_new (void);
 
 void em_filter_source_element_set_current (EMFilterSourceElement *src, const gchar *url);
 
diff --git a/mail/em-folder-properties.c b/mail/em-folder-properties.c
index d79e3f7..c0e9ad9 100644
--- a/mail/em-folder-properties.c
+++ b/mail/em-folder-properties.c
@@ -36,6 +36,7 @@
 #include "em-folder-properties.h"
 #include "em-config.h"
 
+#include "e-mail-backend.h"
 #include "e-mail-local.h"
 #include "mail-ops.h"
 #include "mail-mt.h"
@@ -347,9 +348,15 @@ em_folder_properties_show (EShellView *shell_view,
                            CamelFolder *folder,
                            const gchar *uri)
 {
+	EShellBackend *shell_backend;
+	EMailSession *session;
+
 	g_return_if_fail (E_IS_SHELL_VIEW (shell_view));
 	g_return_if_fail (uri != NULL);
 
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	/* HACK: its the old behaviour, not very 'neat' but it works */
 	if (!strncmp (uri, "vfolder:", 8)) {
 		CamelURL *url = camel_url_new (uri, NULL);
@@ -368,7 +375,9 @@ em_folder_properties_show (EShellView *shell_view,
 	}
 
 	if (folder == NULL)
-		mail_get_folder (uri, 0, emfp_dialog_got_folder, shell_view, mail_msg_unordered_push);
+		mail_get_folder (
+			session, uri, 0, emfp_dialog_got_folder,
+			shell_view, mail_msg_unordered_push);
 	else
 		emfp_dialog_got_folder ((gchar *)uri, folder, shell_view);
 }
diff --git a/mail/em-folder-selection-button.c b/mail/em-folder-selection-button.c
index 76c1676..d6bcd34 100644
--- a/mail/em-folder-selection-button.c
+++ b/mail/em-folder-selection-button.c
@@ -40,6 +40,7 @@
 	((obj), EM_TYPE_FOLDER_SELECTION_BUTTON, EMFolderSelectionButtonPrivate))
 
 struct _EMFolderSelectionButtonPrivate {
+	EMailSession *session;
 	GtkWidget *icon;
 	GtkWidget *label;
 
@@ -56,6 +57,7 @@ enum {
 	PROP_0,
 	PROP_CAPTION,
 	PROP_MULTISELECT,
+	PROP_SESSION,
 	PROP_TITLE
 };
 
@@ -64,9 +66,13 @@ enum {
 	LAST_SIGNAL
 };
 
-static gpointer parent_class;
 static guint signals[LAST_SIGNAL];
 
+G_DEFINE_TYPE (
+	EMFolderSelectionButton,
+	em_folder_selection_button,
+	GTK_TYPE_BUTTON)
+
 static void
 folder_selection_button_unselected (EMFolderSelectionButton *button)
 {
@@ -129,6 +135,12 @@ folder_selection_button_set_property (GObject *object,
 				g_value_get_boolean (value));
 			return;
 
+		case PROP_SESSION:
+			em_folder_selection_button_set_session (
+				EM_FOLDER_SELECTION_BUTTON (object),
+				g_value_get_object (value));
+			return;
+
 		case PROP_TITLE:
 			em_folder_selection_button_set_title (
 				EM_FOLDER_SELECTION_BUTTON (object),
@@ -160,6 +172,13 @@ folder_selection_button_get_property (GObject *object,
 				EM_FOLDER_SELECTION_BUTTON (object)));
 			return;
 
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				em_folder_selection_button_get_session (
+				EM_FOLDER_SELECTION_BUTTON (object)));
+			return;
+
 		case PROP_TITLE:
 			g_value_set_string (
 				value,
@@ -172,6 +191,22 @@ folder_selection_button_get_property (GObject *object,
 }
 
 static void
+folder_selection_button_dispose (GObject *object)
+{
+	EMFolderSelectionButtonPrivate *priv;
+
+	priv = EM_FOLDER_SELECTION_BUTTON_GET_PRIVATE (object);
+
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
+	/* Chain up to parent's dispose() method. */
+	G_OBJECT_CLASS (em_folder_selection_button_parent_class)->dispose (object);
+}
+
+static void
 folder_selection_button_finalize (GObject *object)
 {
 	EMFolderSelectionButtonPrivate *priv;
@@ -186,7 +221,7 @@ folder_selection_button_finalize (GObject *object)
 	g_free (priv->uri);
 
 	/* Chain up to parent's finalize() method. */
-	G_OBJECT_CLASS (parent_class)->finalize (object);
+	G_OBJECT_CLASS (em_folder_selection_button_parent_class)->finalize (object);
 }
 
 static void
@@ -204,7 +239,7 @@ folder_selection_button_clicked (GtkButton *button)
 	parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
 	parent = gtk_widget_is_toplevel (parent) ? parent : NULL;
 
-	emft = (EMFolderTree *) em_folder_tree_new ();
+	emft = (EMFolderTree *) em_folder_tree_new (priv->session);
 	emu_restore_folder_tree_state (emft);
 
 	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (emft));
@@ -255,17 +290,17 @@ exit:
 }
 
 static void
-folder_selection_button_class_init (EMFolderSelectionButtonClass *class)
+em_folder_selection_button_class_init (EMFolderSelectionButtonClass *class)
 {
 	GObjectClass *object_class;
 	GtkButtonClass *button_class;
 
-	parent_class = g_type_class_peek_parent (class);
 	g_type_class_add_private (class, sizeof (EMFolderSelectionButtonPrivate));
 
 	object_class = G_OBJECT_CLASS (class);
 	object_class->set_property = folder_selection_button_set_property;
 	object_class->get_property = folder_selection_button_get_property;
+	object_class->dispose = folder_selection_button_dispose;
 	object_class->finalize = folder_selection_button_finalize;
 
 	button_class = GTK_BUTTON_CLASS (class);
@@ -295,6 +330,17 @@ folder_selection_button_class_init (EMFolderSelectionButtonClass *class)
 
 	g_object_class_install_property (
 		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			NULL,
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT));
+
+	g_object_class_install_property (
+		object_class,
 		PROP_TITLE,
 		g_param_spec_string (
 			"title",
@@ -315,7 +361,7 @@ folder_selection_button_class_init (EMFolderSelectionButtonClass *class)
 }
 
 static void
-folder_selection_button_init (EMFolderSelectionButton *emfsb)
+em_folder_selection_button_init (EMFolderSelectionButton *emfsb)
 {
 	GtkWidget *box;
 
@@ -341,40 +387,17 @@ folder_selection_button_init (EMFolderSelectionButton *emfsb)
 	folder_selection_button_set_contents (emfsb);
 }
 
-GType
-em_folder_selection_button_get_type (void)
-{
-	static GType type = 0;
-
-	if (G_UNLIKELY (type == 0)) {
-		static const GTypeInfo type_info = {
-			sizeof (EMFolderSelectionButtonClass),
-			(GBaseInitFunc) NULL,
-			(GBaseFinalizeFunc) NULL,
-			(GClassInitFunc) folder_selection_button_class_init,
-			(GClassFinalizeFunc) NULL,
-			NULL,  /* class_data */
-			sizeof (EMFolderSelectionButton),
-			0,     /* n_preallocs */
-			(GInstanceInitFunc) folder_selection_button_init,
-			NULL   /* value_table */
-		};
-
-		type = g_type_register_static (
-			GTK_TYPE_BUTTON, "EMFolderSelectionButton",
-			&type_info, 0);
-	}
-
-	return type;
-}
-
 GtkWidget *
-em_folder_selection_button_new (const gchar *title,
+em_folder_selection_button_new (EMailSession *session,
+                                const gchar *title,
                                 const gchar *caption)
 {
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
 	return g_object_new (
 		EM_TYPE_FOLDER_SELECTION_BUTTON,
-		"title", title, "caption", caption, NULL);
+		"session", session, "title", title,
+		"caption", caption, NULL);
 }
 
 const gchar *
@@ -489,6 +512,33 @@ em_folder_selection_button_set_selection_mult (EMFolderSelectionButton *button,
 	g_free (caption);
 }
 
+EMailSession *
+em_folder_selection_button_get_session (EMFolderSelectionButton *button)
+{
+	g_return_val_if_fail (EM_IS_FOLDER_SELECTION_BUTTON (button), NULL);
+
+	return button->priv->session;
+}
+
+void
+em_folder_selection_button_set_session (EMFolderSelectionButton *button,
+                                        EMailSession *session)
+{
+	g_return_if_fail (EM_IS_FOLDER_SELECTION_BUTTON (button));
+
+	if (session != NULL) {
+		g_return_if_fail (E_IS_MAIL_SESSION (session));
+		g_object_ref (session);
+	}
+
+	if (button->priv->session != NULL)
+		g_object_unref (button->priv->session);
+
+	button->priv->session = session;
+
+	g_object_notify (G_OBJECT (button), "session");
+}
+
 const gchar *
 em_folder_selection_button_get_title (EMFolderSelectionButton *button)
 {
@@ -501,7 +551,7 @@ void
 em_folder_selection_button_set_title (EMFolderSelectionButton *button,
                                       const gchar *title)
 {
-	g_return_if_fail (EM_FOLDER_SELECTION_BUTTON (button));
+	g_return_if_fail (EM_IS_FOLDER_SELECTION_BUTTON (button));
 
 	g_free (button->priv->title);
 	button->priv->title = g_strdup (title);
diff --git a/mail/em-folder-selection-button.h b/mail/em-folder-selection-button.h
index cc75fac..8eb89b9 100644
--- a/mail/em-folder-selection-button.h
+++ b/mail/em-folder-selection-button.h
@@ -25,6 +25,7 @@
 #define EM_FOLDER_SELECTION_BUTTON_H
 
 #include <gtk/gtk.h>
+#include <mail/e-mail-session.h>
 
 /* Standard GObject macros */
 #define EM_TYPE_FOLDER_SELECTION_BUTTON \
@@ -66,7 +67,8 @@ struct _EMFolderSelectionButtonClass {
 
 GType		em_folder_selection_button_get_type (void);
 GtkWidget *	em_folder_selection_button_new
-					(const gchar *title,
+					(EMailSession *session,
+					 const gchar *title,
 					 const gchar *caption);
 const gchar *	em_folder_selection_button_get_caption
 					(EMFolderSelectionButton *button);
@@ -88,6 +90,11 @@ GList *		em_folder_selection_button_get_selection_mult
 void		em_folder_selection_button_set_selection_mult
 					(EMFolderSelectionButton *button,
 					 GList *uris);
+void		em_folder_selection_button_set_session
+					(EMFolderSelectionButton *button,
+					 EMailSession *session);
+EMailSession *	em_folder_selection_button_get_session
+					(EMFolderSelectionButton *button);
 const gchar *	em_folder_selection_button_get_title
 					(EMFolderSelectionButton *button);
 void		em_folder_selection_button_set_title
diff --git a/mail/em-folder-selector.c b/mail/em-folder-selector.c
index e4e9bbd..621c542 100644
--- a/mail/em-folder-selector.c
+++ b/mail/em-folder-selector.c
@@ -25,13 +25,13 @@
 #include <glib/gi18n.h>
 #include <e-util/e-util.h>
 
+#include "e-mail-session.h"
 #include "em-folder-tree.h"
 #include "em-folder-selector.h"
 #include "em-folder-utils.h"
 
 #define d(x)
 
-extern CamelSession *session;
 static gpointer parent_class;
 
 static void
diff --git a/mail/em-folder-selector.h b/mail/em-folder-selector.h
index ca31d21..1056a11 100644
--- a/mail/em-folder-selector.h
+++ b/mail/em-folder-selector.h
@@ -68,7 +68,6 @@ struct _EMFolderSelector {
 
 struct _EMFolderSelectorClass {
 	GtkDialogClass parent_class;
-
 };
 
 enum {
diff --git a/mail/em-folder-tree-model.c b/mail/em-folder-tree-model.c
index 1369f3d..5b22bd0 100644
--- a/mail/em-folder-tree-model.c
+++ b/mail/em-folder-tree-model.c
@@ -36,7 +36,6 @@
 #include <glib/gi18n.h>
 
 #include "mail-config.h"
-#include "mail-session.h"
 #include "mail-tools.h"
 #include "mail-mt.h"
 #include "mail-ops.h"
@@ -65,6 +64,7 @@ struct _EMFolderTreeModelPrivate {
 	GtkTreeSelection *selection;  /* weak reference */
 
 	EAccountList *accounts;
+	EMailSession *session;
 
 	/* EAccount -> EMFolderTreeStoreInfo */
 	GHashTable *account_index;
@@ -82,7 +82,8 @@ struct _EMFolderTreeModelPrivate {
 
 enum {
 	PROP_0,
-	PROP_SELECTION
+	PROP_SELECTION,
+	PROP_SESSION
 };
 
 enum {
@@ -204,10 +205,13 @@ account_changed_cb (EAccountList *accounts,
                     EMFolderTreeModel *model)
 {
 	EMFolderTreeModelStoreInfo *si;
+	EMailSession *session;
 	CamelProvider *provider;
 	CamelStore *store;
 	gchar *uri;
 
+	session = em_folder_tree_model_get_session (model);
+
 	si = g_hash_table_lookup (model->priv->account_index, account);
 	if (si == NULL)
 		return;
@@ -226,7 +230,8 @@ account_changed_cb (EAccountList *accounts,
 		return;
 
 	store = (CamelStore *) camel_session_get_service (
-		session, uri, CAMEL_PROVIDER_STORE, NULL);
+		CAMEL_SESSION (session), uri,
+		CAMEL_PROVIDER_STORE, NULL);
 	if (store == NULL)
 		return;
 
@@ -250,27 +255,24 @@ account_removed_cb (EAccountList *accounts,
 
 /* HACK: FIXME: the component should listen to the account object directly */
 static void
-add_new_store (gchar *uri,
-               CamelStore *store,
-               gpointer user_data)
-{
-	EAccount *account = user_data;
-
-	if (store == NULL)
-		return;
-
-	e_mail_store_add (store, account->name);
-}
-
-static void
 account_added_cb (EAccountList *accounts,
                   EAccount *account,
                   EMFolderTreeModel *model)
 {
+	EMailSession *session;
+	CamelStore *store;
 	const gchar *uri;
 
+	session = em_folder_tree_model_get_session (model);
 	uri = e_account_get_string (account, E_ACCOUNT_SOURCE_URL);
-	mail_get_store (uri, NULL, add_new_store, account);
+
+	store = (CamelStore *) camel_session_get_service (
+		CAMEL_SESSION (session), uri, CAMEL_PROVIDER_STORE, NULL);
+
+	if (store != NULL) {
+		e_mail_store_add (session, store, account->name);
+		g_object_unref (store);
+	}
 }
 
 static void
@@ -293,6 +295,12 @@ folder_tree_model_set_property (GObject *object,
 				EM_FOLDER_TREE_MODEL (object),
 				g_value_get_object (value));
 			return;
+
+		case PROP_SESSION:
+			em_folder_tree_model_set_session (
+				EM_FOLDER_TREE_MODEL (object),
+				g_value_get_object (value));
+			return;
 	}
 
 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -311,6 +319,13 @@ folder_tree_model_get_property (GObject *object,
 				em_folder_tree_model_get_selection (
 				EM_FOLDER_TREE_MODEL (object)));
 			return;
+
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				em_folder_tree_model_get_session (
+				EM_FOLDER_TREE_MODEL (object)));
+			return;
 	}
 
 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -330,6 +345,11 @@ folder_tree_model_dispose (GObject *object)
 		priv->selection = NULL;
 	}
 
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
 	/* Chain up to parent's dispose() method. */
 	G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -380,6 +400,16 @@ folder_tree_model_class_init (EMFolderTreeModelClass *class)
 			GTK_TYPE_TREE_SELECTION,
 			G_PARAM_READWRITE));
 
+	g_object_class_install_property (
+		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			NULL,
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READWRITE));
+
 	signals[LOADING_ROW] = g_signal_new (
 		"loading-row",
 		G_OBJECT_CLASS_TYPE (object_class),
@@ -611,6 +641,33 @@ em_folder_tree_model_set_selection (EMFolderTreeModel *model,
 	g_object_notify (G_OBJECT (model), "selection");
 }
 
+EMailSession *
+em_folder_tree_model_get_session (EMFolderTreeModel *model)
+{
+	g_return_val_if_fail (EM_IS_FOLDER_TREE_MODEL (model), NULL);
+
+	return model->priv->session;
+}
+
+void
+em_folder_tree_model_set_session (EMFolderTreeModel *model,
+                                  EMailSession *session)
+{
+	g_return_if_fail (EM_IS_FOLDER_TREE_MODEL (model));
+
+	if (session != NULL) {
+		g_return_if_fail (E_IS_MAIL_SESSION (session));
+		g_object_ref (session);
+	}
+
+	if (model->priv->session != NULL)
+		g_object_unref (model->priv->session);
+
+	model->priv->session = session;
+
+	g_object_notify (G_OBJECT (model), "session");
+}
+
 void
 em_folder_tree_model_set_folder_info (EMFolderTreeModel *model,
                                       GtkTreeIter *iter,
diff --git a/mail/em-folder-tree-model.h b/mail/em-folder-tree-model.h
index f64f02d..ac13c3d 100644
--- a/mail/em-folder-tree-model.h
+++ b/mail/em-folder-tree-model.h
@@ -27,6 +27,8 @@
 #include <camel/camel.h>
 #include <libedataserver/e-account-list.h>
 
+#include <mail/e-mail-session.h>
+
 /* Standard GObject macros */
 #define EM_TYPE_FOLDER_TREE_MODEL \
 	(em_folder_tree_model_get_type ())
@@ -124,6 +126,11 @@ GtkTreeSelection *
 void		em_folder_tree_model_set_selection
 					(EMFolderTreeModel *model,
 					 GtkTreeSelection *selection);
+EMailSession *	em_folder_tree_model_get_session
+					(EMFolderTreeModel *model);
+void		em_folder_tree_model_set_session
+					(EMFolderTreeModel *model,
+					 EMailSession *session);
 void		em_folder_tree_model_set_folder_info
 					(EMFolderTreeModel *model,
 					 GtkTreeIter *iter,
diff --git a/mail/em-folder-tree.c b/mail/em-folder-tree.c
index db23df4..4441d39 100644
--- a/mail/em-folder-tree.c
+++ b/mail/em-folder-tree.c
@@ -64,6 +64,7 @@
 #include "em-event.h"
 
 #include "e-mail-local.h"
+#include "e-mail-session.h"
 
 /* backward-compatibility cruft */
 #include "e-util/gtk-compat.h"
@@ -82,6 +83,8 @@ struct _selected_uri {
 };
 
 struct _EMFolderTreePrivate {
+	EMailSession *session;
+
 	/* selected_uri structures of each path pending selection. */
 	GSList *select_uris;
 
@@ -120,6 +123,11 @@ struct _EMFolderTreePrivate {
 };
 
 enum {
+	PROP_0,
+	PROP_SESSION
+};
+
+enum {
 	FOLDER_ACTIVATED,  /* aka double-clicked or user hit enter */
 	FOLDER_SELECTED,
 	POPUP_EVENT,
@@ -159,7 +167,6 @@ static GdkAtom drop_atoms[NUM_DROP_TYPES];
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
-extern CamelSession *session;
 extern CamelStore *vfolder_store;
 
 struct _folder_tree_selection_data {
@@ -442,6 +449,7 @@ folder_tree_expand_node (const gchar *key,
 	GtkTreeModel *model;
 	GtkTreePath *path;
 	EAccount *account;
+	EMailSession *session;
 	CamelStore *store;
 	const gchar *p;
 	gchar *uid;
@@ -460,9 +468,11 @@ folder_tree_expand_node (const gchar *key,
 	tree_view = GTK_TREE_VIEW (folder_tree);
 	model = gtk_tree_view_get_model (tree_view);
 
+	session = em_folder_tree_get_session (folder_tree);
+
 	if ((account = e_get_account_by_uid (uid)) && account->enabled) {
 		store = (CamelStore *) camel_session_get_service (
-			session, account->source->url,
+			CAMEL_SESSION (session), account->source->url,
 			CAMEL_PROVIDER_STORE, NULL);
 
 		if (store == NULL)
@@ -699,6 +709,51 @@ exit:
 }
 
 static void
+folder_tree_set_session (EMFolderTree *folder_tree,
+                         EMailSession *session)
+{
+	g_return_if_fail (CAMEL_IS_SESSION (session));
+	g_return_if_fail (folder_tree->priv->session == NULL);
+
+	folder_tree->priv->session = g_object_ref (session);
+}
+
+static void
+folder_tree_set_property (GObject *object,
+                          guint property_id,
+                          const GValue *value,
+                          GParamSpec *pspec)
+{
+	switch (property_id) {
+		case PROP_SESSION:
+			folder_tree_set_session (
+				EM_FOLDER_TREE (object),
+				g_value_get_object (value));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
+folder_tree_get_property (GObject *object,
+                          guint property_id,
+                          GValue *value,
+                          GParamSpec *pspec)
+{
+	switch (property_id) {
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				em_folder_tree_get_session (
+				EM_FOLDER_TREE (object)));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
 folder_tree_dispose (GObject *object)
 {
 	EMFolderTreePrivate *priv;
@@ -725,6 +780,11 @@ folder_tree_dispose (GObject *object)
 		priv->autoexpand_id = 0;
 	}
 
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
 	if (priv->text_renderer != NULL) {
 		g_object_unref (priv->text_renderer);
 		priv->text_renderer = NULL;
@@ -959,6 +1019,8 @@ folder_tree_class_init (EMFolderTreeClass *class)
 	g_type_class_add_private (class, sizeof (EMFolderTreePrivate));
 
 	object_class = G_OBJECT_CLASS (class);
+	object_class->set_property = folder_tree_set_property;
+	object_class->get_property = folder_tree_get_property;
 	object_class->dispose = folder_tree_dispose;
 	object_class->finalize = folder_tree_finalize;
 
@@ -972,6 +1034,17 @@ folder_tree_class_init (EMFolderTreeClass *class)
 	tree_view_class->test_collapse_row = folder_tree_test_collapse_row;
 	tree_view_class->row_expanded = folder_tree_row_expanded;
 
+	g_object_class_install_property (
+		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			NULL,
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
+
 	signals[FOLDER_SELECTED] = g_signal_new (
 		"folder-selected",
 		G_OBJECT_CLASS_TYPE (object_class),
@@ -1451,9 +1524,20 @@ em_folder_tree_get_type (void)
 }
 
 GtkWidget *
-em_folder_tree_new (void)
+em_folder_tree_new (EMailSession *session)
+{
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
+	return g_object_new (
+		EM_TYPE_FOLDER_TREE, "session", session, NULL);
+}
+
+EMailSession *
+em_folder_tree_get_session (EMFolderTree *folder_tree)
 {
-	return g_object_new (EM_TYPE_FOLDER_TREE, NULL);
+	g_return_val_if_fail (EM_IS_FOLDER_TREE (folder_tree), NULL);
+
+	return folder_tree->priv->session;
 }
 
 static void
@@ -1597,6 +1681,7 @@ struct _DragDataReceivedAsync {
 	/* Only selection->data and selection->length are valid */
 	GtkSelectionData *selection;
 
+	EMailSession *session;
 	CamelStore *store;
 	gchar *full_name;
 	guint32 action;
@@ -1619,8 +1704,10 @@ folder_tree_drop_folder (struct _DragDataReceivedAsync *m)
 
 	d(printf(" * Drop folder '%s' onto '%s'\n", data, m->full_name));
 
-	if (!(folder = mail_tool_uri_to_folder (
-		(gchar *)data, 0, m->base.cancellable, &m->base.error)))
+	folder = e_mail_session_uri_to_folder_sync (
+		m->session, (gchar *) data, 0,
+		m->base.cancellable, &m->base.error);
+	if (folder == NULL)
 		return;
 
 	full_name = camel_folder_get_full_name (folder);
@@ -1690,7 +1777,7 @@ folder_tree_drop_async__exec (struct _DragDataReceivedAsync *m)
 		case DND_DROP_TYPE_UID_LIST:
 			/* import a list of uids from another evo folder */
 			em_utils_selection_get_uidlist (
-				m->selection, folder, m->move,
+				m->selection, m->session, folder, m->move,
 				m->base.cancellable, &m->base.error);
 			m->moved = m->move && (m->base.error == NULL);
 			break;
@@ -1712,6 +1799,7 @@ folder_tree_drop_async__exec (struct _DragDataReceivedAsync *m)
 static void
 folder_tree_drop_async__free (struct _DragDataReceivedAsync *m)
 {
+	g_object_unref (m->session);
 	g_object_unref (m->context);
 	g_object_unref (m->store);
 	g_free (m->full_name);
@@ -1747,6 +1835,7 @@ tree_drag_data_received (GtkWidget *widget,
 	GtkTreeModel *model;
 	GtkTreeView *tree_view;
 	GtkTreePath *dest_path;
+	EMailSession *session;
 	struct _DragDataReceivedAsync *m;
 	gboolean is_store;
 	CamelStore *store;
@@ -1756,6 +1845,8 @@ tree_drag_data_received (GtkWidget *widget,
 	tree_view = GTK_TREE_VIEW (folder_tree);
 	model = gtk_tree_view_get_model (tree_view);
 
+	session = em_folder_tree_get_session (folder_tree);
+
 	if (!gtk_tree_view_get_dest_row_at_pos (tree_view, x, y, &dest_path, &pos))
 		return;
 
@@ -1788,10 +1879,9 @@ tree_drag_data_received (GtkWidget *widget,
 	}
 
 	m = mail_msg_new (&folder_tree_drop_async_info);
-	m->context = context;
-	g_object_ref (context);
-	m->store = store;
-	g_object_ref (store);
+	m->session = g_object_ref (session);
+	m->context = g_object_ref (context);
+	m->store = g_object_ref (store);
 	m->full_name = full_name;
 	m->action = gdk_drag_context_get_selected_action (context);
 	m->info = info;
@@ -2416,8 +2506,11 @@ em_folder_tree_set_selected_list (EMFolderTree *folder_tree,
                                   gboolean expand_only)
 {
 	EMFolderTreePrivate *priv = folder_tree->priv;
+	EMailSession *session;
 	gint id = 0;
 
+	session = em_folder_tree_get_session (folder_tree);
+
 	/* FIXME: need to remove any currently selected stuff? */
 	if (!expand_only)
 		folder_tree_clear_selected_list (folder_tree);
@@ -2428,7 +2521,8 @@ em_folder_tree_set_selected_list (EMFolderTree *folder_tree,
 
 		u->uri = g_strdup (list->data);
 		u->store = (CamelStore *) camel_session_get_service (
-			session, u->uri, CAMEL_PROVIDER_STORE, NULL);
+			CAMEL_SESSION (session), u->uri,
+			CAMEL_PROVIDER_STORE, NULL);
 
 		url = camel_url_new (u->uri, NULL);
 		if (u->store == NULL || url == NULL) {
diff --git a/mail/em-folder-tree.h b/mail/em-folder-tree.h
index 18c644b..4918bdc 100644
--- a/mail/em-folder-tree.h
+++ b/mail/em-folder-tree.h
@@ -25,7 +25,7 @@
 #define EM_FOLDER_TREE_H
 
 #include <gtk/gtk.h>
-#include <camel/camel.h>
+#include <mail/e-mail-session.h>
 #include <mail/em-folder-tree-model.h>
 
 /* Standard GObject macros */
@@ -89,7 +89,8 @@ struct _EMFolderTreeClass {
 };
 
 GType		em_folder_tree_get_type		(void);
-GtkWidget *	em_folder_tree_new		(void);
+GtkWidget *	em_folder_tree_new		(EMailSession *session);
+EMailSession *	em_folder_tree_get_session	(EMFolderTree *folder_tree);
 void		em_folder_tree_enable_drag_and_drop
 						(EMFolderTree *folder_tree);
 void		em_folder_tree_set_excluded	(EMFolderTree *folder_tree,
diff --git a/mail/em-folder-utils.c b/mail/em-folder-utils.c
index dbd673c..e966f2b 100644
--- a/mail/em-folder-utils.c
+++ b/mail/em-folder-utils.c
@@ -60,12 +60,11 @@
 #include "em-folder-properties.h"
 
 #include "e-mail-local.h"
+#include "e-mail-session.h"
 #include "e-mail-store.h"
 
 #define d(x)
 
-extern CamelSession *session;
-
 static gboolean
 emfu_is_special_local_folder (const gchar *name)
 {
@@ -282,7 +281,9 @@ struct _copy_folder_data {
 };
 
 static void
-emfu_copy_folder_selected (const gchar *uri, gpointer data)
+emfu_copy_folder_selected (EMailSession *session,
+                           const gchar *uri,
+                           gpointer data)
 {
 	struct _copy_folder_data *cfd = data;
 	CamelStore *fromstore = NULL, *tostore = NULL;
@@ -299,7 +300,7 @@ emfu_copy_folder_selected (const gchar *uri, gpointer data)
 	local_store = e_mail_local_get_store ();
 
 	fromstore = camel_session_get_store (
-		session, cfd->fi->uri, &local_error);
+		CAMEL_SESSION (session), cfd->fi->uri, &local_error);
 	if (fromstore == NULL) {
 		e_alert_run_dialog_for_args (
 			e_shell_get_active_window (NULL),
@@ -320,7 +321,8 @@ emfu_copy_folder_selected (const gchar *uri, gpointer data)
 		goto fail;
 	}
 
-	tostore = camel_session_get_store (session, uri, &local_error);
+	tostore = camel_session_get_store (
+		CAMEL_SESSION (session), uri, &local_error);
 	if (tostore == NULL) {
 		e_alert_run_dialog_for_args (
 			e_shell_get_active_window (NULL),
@@ -387,6 +389,7 @@ emfu_copy_folder_exclude (EMFolderTree *tree, GtkTreeModel *model, GtkTreeIter *
 /* FIXME: these functions must be documented */
 void
 em_folder_utils_copy_folder (GtkWindow *parent,
+                             EMailSession *session,
                              CamelFolderInfo *folderinfo,
                              gint delete)
 {
@@ -403,7 +406,7 @@ em_folder_utils_copy_folder (GtkWindow *parent,
 	cfd->delete = delete;
 
 	/* XXX Do we leak this reference. */
-	emft = (EMFolderTree *) em_folder_tree_new ();
+	emft = (EMFolderTree *) em_folder_tree_new (session);
 	emu_restore_folder_tree_state (emft);
 
 	em_folder_tree_set_excluded_func (
@@ -422,7 +425,7 @@ em_folder_utils_copy_folder (GtkWindow *parent,
 
 		uri = em_folder_selector_get_selected_uri (
 			EM_FOLDER_SELECTOR (dialog));
-		emfu_copy_folder_selected (uri, cfd);
+		emfu_copy_folder_selected (session, uri, cfd);
 	}
 
 	gtk_widget_destroy (dialog);
@@ -635,9 +638,12 @@ new_folder_created_cb (CamelFolderInfo *fi, gpointer user_data)
 }
 
 static void
-emfu_popup_new_folder_response (EMFolderSelector *emfs, gint response, gpointer data)
+emfu_popup_new_folder_response (EMFolderSelector *emfs,
+                                gint response,
+                                EMFolderTree *folder_tree)
 {
 	EMFolderTreeModelStoreInfo *si;
+	EMailSession *session;
 	GtkTreeModel *model;
 	const gchar *uri, *path;
 	CamelStore *store;
@@ -655,8 +661,11 @@ emfu_popup_new_folder_response (EMFolderSelector *emfs, gint response, gpointer
 
 	g_print ("DEBUG: %s (%s)\n", path, uri);
 
+	session = em_folder_tree_get_session (folder_tree);
+
 	store = (CamelStore *) camel_session_get_service (
-		session, uri, CAMEL_PROVIDER_STORE, NULL);
+		CAMEL_SESSION (session), uri,
+		CAMEL_PROVIDER_STORE, NULL);
 	if (store == NULL)
 		return;
 
@@ -670,21 +679,21 @@ emfu_popup_new_folder_response (EMFolderSelector *emfs, gint response, gpointer
 
 	/* HACK: we need to create vfolders using the vfolder editor */
 	if (CAMEL_IS_VEE_STORE (store)) {
-		EMVFolderRule *rule;
+		EFilterRule *rule;
 
 		/* ensures vfolder is running */
-		vfolder_load_storage ();
+		vfolder_load_storage (session);
 
-		rule = em_vfolder_rule_new ();
-		e_filter_rule_set_name ((EFilterRule *)rule, path);
-		vfolder_gui_add_rule (rule);
+		rule = em_vfolder_rule_new (session);
+		e_filter_rule_set_name (rule, path);
+		vfolder_gui_add_rule (EM_VFOLDER_RULE (rule));
 		gtk_widget_destroy ((GtkWidget *)emfs);
 	} else {
 		/* Temp data to pass to create_folder_real function */
 		emcftd = (struct _EMCreateFolderTempData *) g_malloc (sizeof (struct _EMCreateFolderTempData));
 		emcftd->emfs = emfs;
 		emcftd->uri = g_strdup (uri);
-		emcftd->emft = (EMFolderTree *) data;
+		emcftd->emft = folder_tree;
 
 		g_object_ref (emfs);
 		emfu_create_folder_real (si->store, path, new_folder_created_cb, emcftd);
@@ -695,12 +704,19 @@ emfu_popup_new_folder_response (EMFolderSelector *emfs, gint response, gpointer
 
 /* FIXME: these functions must be documented */
 void
-em_folder_utils_create_folder (CamelFolderInfo *folderinfo, EMFolderTree *emft, GtkWindow *parent)
+em_folder_utils_create_folder (CamelFolderInfo *folderinfo,
+                               EMFolderTree *emft,
+                               GtkWindow *parent)
 {
 	EMFolderTree *folder_tree;
+	EMailSession *session;
 	GtkWidget *dialog;
 
-	folder_tree = (EMFolderTree *) em_folder_tree_new ();
+	g_return_if_fail (EM_IS_FOLDER_TREE (emft));
+
+	session = em_folder_tree_get_session (emft);
+
+	folder_tree = (EMFolderTree *) em_folder_tree_new (session);
 	emu_restore_folder_tree_state (folder_tree);
 
 	dialog = em_folder_selector_create_new (
@@ -719,6 +735,7 @@ em_folder_utils_create_folder (CamelFolderInfo *folderinfo, EMFolderTree *emft,
 
 struct _folder_unsub_t {
 	MailMsg base;
+	EMailSession *session;
 	gchar *folder_uri;
 };
 
@@ -738,7 +755,8 @@ emfu_unsubscribe_folder__exec (struct _folder_unsub_t *msg)
 	gint url_flags;
 
 	store = camel_session_get_store (
-		session, msg->folder_uri, &msg->base.error);
+		CAMEL_SESSION (msg->session),
+		msg->folder_uri, &msg->base.error);
 	if (store == NULL)
 		return;
 
@@ -761,6 +779,7 @@ emfu_unsubscribe_folder__exec (struct _folder_unsub_t *msg)
 static void
 emfu_unsubscribe_folder__free (struct _folder_unsub_t *msg)
 {
+	g_object_unref (msg->session);
 	g_free (msg->folder_uri);
 }
 
@@ -773,13 +792,16 @@ static MailMsgInfo unsubscribe_info = {
 };
 
 void
-em_folder_utils_unsubscribe_folder (const gchar *folder_uri)
+em_folder_utils_unsubscribe_folder (EMailSession *session,
+                                    const gchar *folder_uri)
 {
 	struct _folder_unsub_t *unsub;
 
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
 	g_return_if_fail (folder_uri != NULL);
 
 	unsub = mail_msg_new (&unsubscribe_info);
+	unsub->session = g_object_ref (session);
 	unsub->folder_uri = g_strdup (folder_uri);
 
 	mail_msg_unordered_push (unsub);
diff --git a/mail/em-folder-utils.h b/mail/em-folder-utils.h
index ec5882d..a6a2a21 100644
--- a/mail/em-folder-utils.h
+++ b/mail/em-folder-utils.h
@@ -26,6 +26,7 @@
 
 #include <gtk/gtk.h>
 #include <camel/camel.h>
+#include <mail/e-mail-session.h>
 #include <mail/em-folder-tree.h>
 
 G_BEGIN_DECLS
@@ -40,6 +41,7 @@ gint		em_folder_utils_copy_folders	(CamelStore *fromstore,
  *       most rely on the wrong data. */
 
 void		em_folder_utils_copy_folder	(GtkWindow *parent,
+						 EMailSession *session,
 						 CamelFolderInfo *folderinfo,
 						 gboolean delete);
 void		em_folder_utils_delete_folder	(CamelFolder *folder);
@@ -47,7 +49,8 @@ void		em_folder_utils_create_folder	(CamelFolderInfo *folderinfo,
 						 EMFolderTree *emft,
 						 GtkWindow *parent);
 void		em_folder_utils_unsubscribe_folder
-						(const gchar *folder_uri);
+						(EMailSession *session,
+						 const gchar *folder_uri);
 
 const gchar *	em_folder_utils_get_icon_name	(guint32 flags);
 
diff --git a/mail/em-utils.c b/mail/em-utils.c
index 971d74f..ab85007 100644
--- a/mail/em-utils.c
+++ b/mail/em-utils.c
@@ -74,7 +74,7 @@
 #include "em-composer-utils.h"
 #include "em-format-quote.h"
 #include "e-mail-local.h"
-#include "mail-session.h"
+#include "e-mail-session.h"
 
 /* XXX This is a dirty hack on a dirty hack.  We really need
  *     to rework or get rid of the functions that use this. */
@@ -266,18 +266,22 @@ static EMFilterSource em_filter_source_element_names[] = {
 /**
  * em_utils_edit_filters:
  * @parent: parent window
+ * @session: an #EMailSession
  *
  * Opens or raises the filters editor dialog so that the user may edit
  * his/her filters. If @parent is non-NULL, then the dialog will be
  * created as a child window of @parent's toplevel window.
  **/
 void
-em_utils_edit_filters (GtkWidget *parent)
+em_utils_edit_filters (GtkWidget *parent,
+                       EMailSession *session)
 {
 	const gchar *config_dir;
 	gchar *user, *system;
 	EMFilterContext *fc;
 
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+
 	if (filter_editor) {
 		gtk_window_present (GTK_WINDOW (filter_editor));
 		return;
@@ -285,7 +289,7 @@ em_utils_edit_filters (GtkWidget *parent)
 
 	config_dir = mail_session_get_config_dir ();
 
-	fc = em_filter_context_new ();
+	fc = em_filter_context_new (session);
 	user = g_build_filename (config_dir, "filters.xml", NULL);
 	system = g_build_filename (EVOLUTION_PRIVDATADIR, "filtertypes.xml", NULL);
 	e_rule_context_load ((ERuleContext *) fc, system, user);
@@ -366,6 +370,7 @@ em_utils_flag_for_followup (EMailReader *reader,
                             GPtrArray *uids)
 {
 	EShell *shell;
+	EMailBackend *backend;
 	EShellSettings *shell_settings;
 	EShellBackend *shell_backend;
 	EMFormatHTML *formatter;
@@ -378,11 +383,13 @@ em_utils_flag_for_followup (EMailReader *reader,
 	g_return_if_fail (CAMEL_IS_FOLDER (folder));
 	g_return_if_fail (uids != NULL);
 
-	editor = e_mail_tag_editor_new ();
 	window = e_mail_reader_get_window (reader);
+	backend = e_mail_reader_get_backend (reader);
+
+	editor = e_mail_tag_editor_new ();
 	gtk_window_set_transient_for (GTK_WINDOW (editor), window);
 
-	shell_backend = e_mail_reader_get_shell_backend (reader);
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 	shell_settings = e_shell_get_shell_settings (shell);
 
@@ -759,6 +766,7 @@ em_utils_selection_set_uidlist (GtkSelectionData *selection_data,
 /**
  * em_utils_selection_get_uidlist:
  * @data: selection data
+ * @session: an #EMailSession
  * @move: do we delete the messages.
  *
  * Convert a uid list into a copy/move operation.
@@ -767,6 +775,7 @@ em_utils_selection_set_uidlist (GtkSelectionData *selection_data,
  **/
 void
 em_utils_selection_get_uidlist (GtkSelectionData *selection_data,
+                                EMailSession *session,
                                 CamelFolder *dest,
                                 gint move,
                                 GCancellable *cancellable,
@@ -780,6 +789,7 @@ em_utils_selection_get_uidlist (GtkSelectionData *selection_data,
 	gint length;
 
 	g_return_if_fail (selection_data != NULL);
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
 
 	data = gtk_selection_data_get_data (selection_data);
 	length = gtk_selection_data_get_length (selection_data);
@@ -808,9 +818,9 @@ em_utils_selection_get_uidlist (GtkSelectionData *selection_data,
 		return;
 	}
 
-	/* FIXME mail_tool_uri_to_folder() may block. */
-	folder = mail_tool_uri_to_folder (
-		(gchar *) data, 0, cancellable, error);
+	/* FIXME e_mail_session_uri_to_folder_sync() may block. */
+	folder = e_mail_session_uri_to_folder_sync (
+		session, (gchar *) data, 0, cancellable, error);
 	if (folder) {
 		/* FIXME camel_folder_transfer_messages_to_sync() may block. */
 		camel_folder_transfer_messages_to_sync (
@@ -1274,17 +1284,21 @@ em_utils_expunge_folder (GtkWidget *parent, CamelFolder *folder)
 /**
  * em_utils_empty_trash:
  * @parent: parent window
+ * @session: an #EMailSession
  *
  * Empties all Trash folders.
  **/
 void
-em_utils_empty_trash (GtkWidget *parent)
+em_utils_empty_trash (GtkWidget *parent,
+                      EMailSession *session)
 {
 	CamelProvider *provider;
 	EAccountList *accounts;
 	EAccount *account;
 	EIterator *iter;
 
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+
 	if (!em_utils_prompt_user((GtkWindow *) parent, "/apps/evolution/mail/prompts/empty_trash", "mail:ask-empty-trash", NULL))
 		return;
 
@@ -1299,7 +1313,7 @@ em_utils_empty_trash (GtkWidget *parent)
 			if (provider) {
 				/* make sure this store is a remote store */
 				if (provider->flags & CAMEL_PROVIDER_IS_STORAGE) {
-					mail_empty_trash (account, NULL, NULL);
+					mail_empty_trash (session, account, NULL, NULL);
 				}
 			}
 		}
@@ -1310,7 +1324,7 @@ em_utils_empty_trash (GtkWidget *parent)
 	g_object_unref (iter);
 
 	/* Now empty the local trash folder */
-	mail_empty_trash (NULL, NULL, NULL);
+	mail_empty_trash (session, NULL, NULL, NULL);
 }
 
 gchar *
diff --git a/mail/em-utils.h b/mail/em-utils.h
index 36318ff..684d43c 100644
--- a/mail/em-utils.h
+++ b/mail/em-utils.h
@@ -28,6 +28,7 @@
 #include <camel/camel.h>
 
 #include <mail/e-mail-reader.h>
+#include <mail/e-mail-session.h>
 #include <mail/em-folder-tree.h>
 
 G_BEGIN_DECLS
@@ -42,7 +43,7 @@ void em_utils_uids_free (GPtrArray *uids);
 
 gboolean em_utils_check_user_can_send_mail (void);
 
-void em_utils_edit_filters (GtkWidget *parent);
+void em_utils_edit_filters (GtkWidget *parent, EMailSession *session);
 void em_filename_make_safe (gchar *string);
 void em_utils_edit_vfolders (GtkWidget *parent);
 
@@ -57,7 +58,7 @@ void em_utils_selection_get_mailbox (GtkSelectionData *data, CamelFolder *folder
 void em_utils_selection_get_message (GtkSelectionData *data, CamelFolder *folder);
 /* FIXME: be nice if these also worked on CamelFolder's, no easy way to get uri from folder yet tho */
 void em_utils_selection_set_uidlist (GtkSelectionData *data, const gchar *uri, GPtrArray *uids);
-void em_utils_selection_get_uidlist (GtkSelectionData *data, CamelFolder *dest, gint move, GCancellable *cancellable, GError **error);
+void em_utils_selection_get_uidlist (GtkSelectionData *data, EMailSession *session, CamelFolder *dest, gint move, GCancellable *cancellable, GError **error);
 void em_utils_selection_set_urilist (GtkSelectionData *data, CamelFolder *folder, GPtrArray *uids);
 void em_utils_selection_get_urilist (GtkSelectionData *data, CamelFolder *folder);
 
@@ -72,7 +73,7 @@ gchar *em_utils_get_proxy_uri (const gchar *uri);
 gchar *em_utils_message_to_html (CamelMimeMessage *msg, const gchar *credits, guint32 flags, gssize *len, struct _EMFormat *source, const gchar *append, guint32 *validity_found);
 
 void em_utils_expunge_folder (GtkWidget *parent, CamelFolder *folder);
-void em_utils_empty_trash (GtkWidget *parent);
+void em_utils_empty_trash (GtkWidget *parent, EMailSession *session);
 
 /* returns the folder name portion of an URI */
 gchar *em_utils_folder_name_from_uri (const gchar *uri);
diff --git a/mail/em-vfolder-context.c b/mail/em-vfolder-context.c
index 6d5604a..4e7d401 100644
--- a/mail/em-vfolder-context.c
+++ b/mail/em-vfolder-context.c
@@ -35,89 +35,175 @@
 
 #include "em-filter-folder-element.h"
 
-static EFilterElement *vfolder_new_element (ERuleContext *rc, const gchar *type);
+#define EM_VFOLDER_CONTEXT_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE \
+	((obj), EM_TYPE_VFOLDER_CONTEXT, EMVFolderContextPrivate))
 
-static ERuleContextClass *parent_class = NULL;
+struct _EMVFolderContextPrivate {
+	EMailSession *session;
+};
+
+enum {
+	PROP_0,
+	PROP_SESSION
+};
+
+G_DEFINE_TYPE (
+	EMVFolderContext,
+	em_vfolder_context,
+	E_TYPE_RULE_CONTEXT)
 
 static void
-em_vfolder_context_class_init (EMVFolderContextClass *klass)
+vfolder_context_set_session (EMVFolderContext *context,
+                             EMailSession *session)
 {
-	parent_class = g_type_class_ref (E_TYPE_RULE_CONTEXT);
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+	g_return_if_fail (context->priv->session == NULL);
 
-	((ERuleContextClass *)klass)->new_element = vfolder_new_element;
+	context->priv->session = g_object_ref (session);
 }
 
 static void
-em_vfolder_context_init (EMVFolderContext *vc)
+vfolder_context_set_property (GObject *object,
+                              guint property_id,
+                              const GValue *value,
+                              GParamSpec *pspec)
 {
-	e_rule_context_add_part_set (
-		(ERuleContext *) vc, "partset", E_TYPE_FILTER_PART,
-		e_rule_context_add_part, e_rule_context_next_part);
-
-	e_rule_context_add_rule_set (
-		(ERuleContext *) vc, "ruleset", em_vfolder_rule_get_type(),
-		e_rule_context_add_rule, e_rule_context_next_rule);
+	switch (property_id) {
+		case PROP_SESSION:
+			vfolder_context_set_session (
+				EM_VFOLDER_CONTEXT (object),
+				g_value_get_object (value));
+			return;
+	}
 
-	((ERuleContext *)vc)->flags =
-		E_RULE_CONTEXT_THREADING | E_RULE_CONTEXT_GROUPING;
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 }
 
-GType
-em_vfolder_context_get_type (void)
+static void
+vfolder_context_get_property (GObject *object,
+                              guint property_id,
+                              GValue *value,
+                              GParamSpec *pspec)
 {
-	static GType type = 0;
-
-	if (G_UNLIKELY (type == 0)) {
-		static const GTypeInfo type_info = {
-			sizeof (EMVFolderContextClass),
-			(GBaseInitFunc) NULL,
-			(GBaseFinalizeFunc) NULL,
-			(GClassInitFunc) em_vfolder_context_class_init,
-			(GClassFinalizeFunc) NULL,
-			NULL,  /* class_data */
-			sizeof (EMVFolderContext),
-			0,     /* n_preallocs */
-			(GInstanceInitFunc) em_vfolder_context_init,
-			NULL   /* value_table */
-		};
-
-		type = g_type_register_static (
-			E_TYPE_RULE_CONTEXT, "EMVFolderContext",
-			&type_info, 0);
+	switch (property_id) {
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				em_vfolder_context_get_session (
+				EM_VFOLDER_CONTEXT (object)));
+			return;
 	}
 
-	return type;
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 }
 
-/**
- * em_vfolder_context_new:
- *
- * Create a new EMVFolderContext object.
- *
- * Return value: A new #EMVFolderContext object.
- **/
-EMVFolderContext *
-em_vfolder_context_new (void)
+static void
+vfolder_context_dispose (GObject *object)
 {
-	return g_object_new (em_vfolder_context_get_type (), NULL, NULL);
+	EMVFolderContextPrivate *priv;
+
+	priv = EM_VFOLDER_CONTEXT_GET_PRIVATE (object);
+
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
+	/* Chain up to parent's dispose() method. */
+	G_OBJECT_CLASS (em_vfolder_context_parent_class)->dispose (object);
 }
 
 static EFilterElement *
-vfolder_new_element (ERuleContext *rc, const gchar *type)
+vfolder_context_new_element (ERuleContext *context,
+                             const gchar *type)
 {
-	if (!strcmp(type, "system-flag")) {
-		return (EFilterElement *) e_filter_option_new ();
-	} else if (!strcmp(type, "score")) {
-		return (EFilterElement *) e_filter_int_new_type("score", -3, 3);
-	} else if (!strcmp(type, "folder-curi")) {
-		EMFilterFolderElement *ff = em_filter_folder_element_new ();
-		if (ff)
-			ff->store_camel_uri = TRUE;
-		return (EFilterElement *) ff;
-	} else if (!strcmp(type, "folder")) {
-		return (EFilterElement *) em_filter_folder_element_new ();
-	} else {
-		return parent_class->new_element (rc, type);
+	EMVFolderContextPrivate *priv;
+
+	priv = EM_VFOLDER_CONTEXT_GET_PRIVATE (context);
+
+	if (strcmp (type, "system-flag") == 0)
+		return e_filter_option_new ();
+
+	if (strcmp (type, "score") == 0)
+		return e_filter_int_new_type("score", -3, 3);
+
+	if (strcmp (type, "folder-curi") == 0) {
+		EFilterElement *element;
+
+		element = em_filter_folder_element_new (priv->session);
+		((EMFilterFolderElement *) element)->store_camel_uri = TRUE;
+
+		return element;
 	}
+
+	if (strcmp (type, "folder") == 0)
+		return em_filter_folder_element_new (priv->session);
+
+	return E_RULE_CONTEXT_CLASS (em_vfolder_context_parent_class)->
+		new_element (context, type);
+}
+
+static void
+em_vfolder_context_class_init (EMVFolderContextClass *class)
+{
+	GObjectClass *object_class;
+	ERuleContextClass *rule_context_class;
+
+	g_type_class_add_private (class, sizeof (EMVFolderContextPrivate));
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->set_property = vfolder_context_set_property;
+	object_class->get_property = vfolder_context_get_property;
+	object_class->dispose = vfolder_context_dispose;
+
+	rule_context_class = E_RULE_CONTEXT_CLASS (class);
+	rule_context_class->new_element = vfolder_context_new_element;
+
+	g_object_class_install_property (
+		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			NULL,
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
 }
 
+static void
+em_vfolder_context_init (EMVFolderContext *context)
+{
+	context->priv = EM_VFOLDER_CONTEXT_GET_PRIVATE (context);
+
+	e_rule_context_add_part_set (
+		E_RULE_CONTEXT (context), "partset", E_TYPE_FILTER_PART,
+		(ERuleContextPartFunc) e_rule_context_add_part,
+		(ERuleContextNextPartFunc) e_rule_context_next_part);
+
+	e_rule_context_add_rule_set (
+		E_RULE_CONTEXT (context), "ruleset", EM_TYPE_VFOLDER_RULE,
+		(ERuleContextRuleFunc) e_rule_context_add_rule,
+		(ERuleContextNextRuleFunc) e_rule_context_next_rule);
+
+	E_RULE_CONTEXT (context)->flags =
+		E_RULE_CONTEXT_THREADING | E_RULE_CONTEXT_GROUPING;
+}
+
+EMVFolderContext *
+em_vfolder_context_new (EMailSession *session)
+{
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
+	return g_object_new (
+		EM_TYPE_VFOLDER_CONTEXT, "session", session, NULL);
+}
+
+EMailSession *
+em_vfolder_context_get_session (EMVFolderContext *context)
+{
+	g_return_val_if_fail (EM_IS_VFOLDER_CONTEXT (context), NULL);
+
+	return context->priv->session;
+}
diff --git a/mail/em-vfolder-context.h b/mail/em-vfolder-context.h
index 59ff08b..c0fd041 100644
--- a/mail/em-vfolder-context.h
+++ b/mail/em-vfolder-context.h
@@ -22,31 +22,51 @@
  *
  */
 
-#ifndef _EM_VFOLDER_CONTEXT_H
-#define _EM_VFOLDER_CONTEXT_H
+#ifndef EM_VFOLDER_CONTEXT_H
+#define EM_VFOLDER_CONTEXT_H
 
-#include "filter/e-rule-context.h"
+#include <mail/e-mail-session.h>
+#include <filter/e-rule-context.h>
 
-#define EM_VFOLDER_CONTEXT(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), em_vfolder_context_get_type(), EMVFolderContext))
-#define EM_VFOLDER_CONTEXT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), em_vfolder_context_get_type(), EMVFolderContextClass))
-#define EM_IS_VFOLDER_CONTEXT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), em_vfolder_context_get_type()))
-#define EM_IS_VFOLDER_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), em_vfolder_context_get_type()))
-#define EM_VFOLDER_CONTEXT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), em_vfolder_context_get_type(), EMVFolderContextClass))
+/* Standard GObject macros */
+#define EM_TYPE_VFOLDER_CONTEXT \
+	(em_vfolder_context_get_type ())
+#define EM_VFOLDER_CONTEXT(obj) \
+	(G_TYPE_CHECK_INSTANCE_CAST \
+	((obj), EM_TYPE_VFOLDER_CONTEXT, EMVFolderContext))
+#define EM_VFOLDER_CONTEXT_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_CAST \
+	((cls), EM_TYPE_VFOLDER_CONTEXT, EMVFolderContextClass))
+#define EM_IS_VFOLDER_CONTEXT(obj) \
+	(G_TYPE_CHECK_INSTANCE_TYPE \
+	((obj), EM_TYPE_VFOLDER_CONTEXT))
+#define EM_IS_VFOLDER_CONTEXT_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_TYPE \
+	((cls), EM_TYPE_VFOLDER_CONTEXT))
+#define EM_VFOLDER_CONTEXT_GET_CLASS(obj) \
+	(G_TYPE_INSTANCE_GET_CLASS \
+	((obj), EM_TYPE_VFOLDER_CONTEXT, EMVFolderContextClass))
+
+G_BEGIN_DECLS
 
 typedef struct _EMVFolderContext EMVFolderContext;
 typedef struct _EMVFolderContextClass EMVFolderContextClass;
+typedef struct _EMVFolderContextPrivate EMVFolderContextPrivate;
 
 struct _EMVFolderContext {
-	ERuleContext parent_object;
-
+	ERuleContext parent;
+	EMVFolderContextPrivate *priv;
 };
 
 struct _EMVFolderContextClass {
 	ERuleContextClass parent_class;
 };
 
-GType em_vfolder_context_get_type (void);
+GType		em_vfolder_context_get_type	(void);
+EMVFolderContext *
+		em_vfolder_context_new		(EMailSession *session);
+EMailSession *	em_vfolder_context_get_session	(EMVFolderContext *context);
 
-EMVFolderContext *em_vfolder_context_new (void);
+G_END_DECLS
 
-#endif /* _EM_VFOLDER_CONTEXT_H */
+#endif /* EM_VFOLDER_CONTEXT_H */
diff --git a/mail/em-vfolder-editor.c b/mail/em-vfolder-editor.c
index 192319c..b1f58ed 100644
--- a/mail/em-vfolder-editor.c
+++ b/mail/em-vfolder-editor.c
@@ -37,16 +37,24 @@
 #include "em-vfolder-editor.h"
 #include "em-vfolder-rule.h"
 
-static gpointer parent_class;
+G_DEFINE_TYPE (
+	EMVFolderEditor,
+	em_vfolder_editor,
+	E_TYPE_RULE_EDITOR)
 
 static EFilterRule *
 vfolder_editor_create_rule (ERuleEditor *rule_editor)
 {
+	EMVFolderContext *context;
+	EMailSession *session;
 	EFilterRule *rule;
 	EFilterPart *part;
 
+	context = EM_VFOLDER_CONTEXT (rule_editor->context);
+	session = em_vfolder_context_get_session (context);
+
 	/* create a rule with 1 part in it */
-	rule = (EFilterRule *) em_vfolder_rule_new ();
+	rule = em_vfolder_rule_new (session);
 	part = e_rule_context_next_part (rule_editor->context, NULL);
 	e_filter_rule_add_part (rule, e_filter_part_clone (part));
 
@@ -54,18 +62,16 @@ vfolder_editor_create_rule (ERuleEditor *rule_editor)
 }
 
 static void
-vfolder_editor_class_init (EMVFolderEditorClass *class)
+em_vfolder_editor_class_init (EMVFolderEditorClass *class)
 {
 	ERuleEditorClass *rule_editor_class;
 
-	parent_class = g_type_class_peek_parent (class);
-
 	rule_editor_class = E_RULE_EDITOR_CLASS (class);
 	rule_editor_class->create_rule = vfolder_editor_create_rule;
 }
 
 static void
-vfolder_editor_init (EMVFolderEditor *vfolder_editor)
+em_vfolder_editor_init (EMVFolderEditor *vfolder_editor)
 {
 	GConfBridge *bridge;
 	const gchar *key_prefix;
@@ -77,56 +83,32 @@ vfolder_editor_init (EMVFolderEditor *vfolder_editor)
 		bridge, key_prefix, GTK_WINDOW (vfolder_editor));
 }
 
-GType
-em_vfolder_editor_get_type (void)
-{
-	static GType type = 0;
-
-	if (G_UNLIKELY (type == 0)) {
-		static const GTypeInfo type_info = {
-			sizeof (EMVFolderEditorClass),
-			(GBaseInitFunc) NULL,
-			(GBaseFinalizeFunc) NULL,
-			(GClassInitFunc) vfolder_editor_class_init,
-			(GClassFinalizeFunc) NULL,
-			NULL,  /* class_data */
-			sizeof (EMVFolderEditor),
-			0,     /* n_preallocs */
-			(GInstanceInitFunc) vfolder_editor_init,
-			NULL   /* value_table */
-		};
-
-		type = g_type_register_static (
-			E_TYPE_RULE_EDITOR, "EMVFolderEditor", &type_info, 0);
-	}
-
-	return type;
-}
-
 /**
  * em_vfolder_editor_new:
  *
  * Create a new EMVFolderEditor object.
  *
- * Return value: A new #EMVFolderEditor object.
+ * Returns: a new #EMVFolderEditor
  **/
 GtkWidget *
-em_vfolder_editor_new (EMVFolderContext *vc)
+em_vfolder_editor_new (EMVFolderContext *context)
 {
-	EMVFolderEditor *ve;
+	EMVFolderEditor *editor;
 	GtkBuilder *builder;
 
-	ve = g_object_new (EM_TYPE_VFOLDER_EDITOR, NULL);
+	g_return_val_if_fail (EM_IS_VFOLDER_CONTEXT (context), NULL);
+
+	editor = g_object_new (EM_TYPE_VFOLDER_EDITOR, NULL);
 
 	builder = gtk_builder_new ();
 	e_load_ui_builder_definition (builder, "filter.ui");
 
 	e_rule_editor_construct (
-		(ERuleEditor *) ve, (ERuleContext *) vc,
+		E_RULE_EDITOR (editor), E_RULE_CONTEXT (context),
 		builder, "incoming", _("Search _Folders"));
 	gtk_widget_hide (e_builder_get_widget (builder, "label17"));
 	gtk_widget_hide (e_builder_get_widget (builder, "filter_source_combobox"));
 	g_object_unref (builder);
 
-	return GTK_WIDGET (ve);
+	return GTK_WIDGET (editor);
 }
diff --git a/mail/em-vfolder-rule.c b/mail/em-vfolder-rule.c
index 073bcec..36027c1 100644
--- a/mail/em-vfolder-rule.c
+++ b/mail/em-vfolder-rule.c
@@ -41,20 +41,26 @@
 #include "e-util/e-alert.h"
 #include "e-util/e-util-private.h"
 
-#define d(x)
+#define EM_VFOLDER_RULE_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE \
+	((obj), EM_TYPE_VFOLDER_RULE, EMVFolderRulePrivate))
+
+struct _EMVFolderRulePrivate {
+	EMailSession *session;
+};
+
+enum {
+	PROP_0,
+	PROP_SESSION
+};
 
 static gint validate (EFilterRule *, EAlert **alert);
 static gint vfolder_eq (EFilterRule *fr, EFilterRule *cm);
 static xmlNodePtr xml_encode (EFilterRule *);
 static gint xml_decode (EFilterRule *, xmlNodePtr, ERuleContext *f);
 static void rule_copy (EFilterRule *dest, EFilterRule *src);
-/*static void build_code(EFilterRule *, GString *out);*/
 static GtkWidget *get_widget (EFilterRule *fr, ERuleContext *f);
 
-static void em_vfolder_rule_class_init (EMVFolderRuleClass *klass);
-static void em_vfolder_rule_init (EMVFolderRule *vr);
-static void em_vfolder_rule_finalise (GObject *obj);
-
 /* DO NOT internationalise these strings */
 static const gchar *with_names[] = {
 	"specific",
@@ -63,104 +69,167 @@ static const gchar *with_names[] = {
 	"local"
 };
 
-static EFilterRuleClass *parent_class = NULL;
+G_DEFINE_TYPE (
+	EMVFolderRule,
+	em_vfolder_rule,
+	E_TYPE_FILTER_RULE)
+
+static void
+vfolder_rule_set_session (EMVFolderRule *rule,
+                          EMailSession *session)
+{
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+	g_return_if_fail (rule->priv->session == NULL);
+
+	rule->priv->session = g_object_ref (session);
+}
 
-GType
-em_vfolder_rule_get_type (void)
+static void
+vfolder_rule_set_property (GObject *object,
+                           guint property_id,
+                           const GValue *value,
+                           GParamSpec *pspec)
 {
-	static GType type = 0;
-
-	if (!type) {
-		static const GTypeInfo info = {
-			sizeof (EMVFolderRuleClass),
-			NULL, /* base_class_init */
-			NULL, /* base_class_finalize */
-			(GClassInitFunc)em_vfolder_rule_class_init,
-			NULL, /* class_finalize */
-			NULL, /* class_data */
-			sizeof (EMVFolderRule),
-			0,    /* n_preallocs */
-			(GInstanceInitFunc)em_vfolder_rule_init,
-		};
-
-		type = g_type_register_static(E_TYPE_FILTER_RULE, "EMVFolderRule", &info, 0);
+	switch (property_id) {
+		case PROP_SESSION:
+			vfolder_rule_set_session (
+				EM_VFOLDER_RULE (object),
+				g_value_get_object (value));
+			return;
 	}
 
-	return type;
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 }
 
 static void
-em_vfolder_rule_class_init (EMVFolderRuleClass *klass)
+vfolder_rule_get_property (GObject *object,
+                           guint property_id,
+                           GValue *value,
+                           GParamSpec *pspec)
 {
-	GObjectClass *object_class = G_OBJECT_CLASS (klass);
-	EFilterRuleClass *fr_class =(EFilterRuleClass *)klass;
+	switch (property_id) {
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				em_vfolder_rule_get_session (
+				EM_VFOLDER_RULE (object)));
+			return;
+	}
 
-	parent_class = g_type_class_ref (E_TYPE_FILTER_RULE);
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
 
-	object_class->finalize = em_vfolder_rule_finalise;
+static void
+vfolder_rule_dispose (GObject *object)
+{
+	EMVFolderRulePrivate *priv;
 
-	/* override methods */
-	fr_class->validate = validate;
-	fr_class->eq = vfolder_eq;
-	fr_class->xml_encode = xml_encode;
-	fr_class->xml_decode = xml_decode;
-	fr_class->copy = rule_copy;
-	/*fr_class->build_code = build_code;*/
-	fr_class->get_widget = get_widget;
+	priv = EM_VFOLDER_RULE_GET_PRIVATE (object);
+
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
+	/* Chain up to parent's dispose() method. */
+	G_OBJECT_CLASS (em_vfolder_rule_parent_class)->dispose (object);
 }
 
 static void
-em_vfolder_rule_init (EMVFolderRule *vr)
+vfolder_rule_finalize (GObject *object)
 {
-	vr->with = EM_VFOLDER_RULE_WITH_SPECIFIC;
-	vr->rule.source = g_strdup("incoming");
+	EMVFolderRule *rule = EM_VFOLDER_RULE (object);
+
+	g_list_foreach (rule->sources, (GFunc) g_free, NULL);
+	g_list_free (rule->sources);
+
+	/* Chain up to parent's finalize() method. */
+	G_OBJECT_CLASS (em_vfolder_rule_parent_class)->finalize (object);
 }
 
 static void
-em_vfolder_rule_finalise (GObject *obj)
+em_vfolder_rule_class_init (EMVFolderRuleClass *class)
 {
-	EMVFolderRule *vr =(EMVFolderRule *)obj;
+	GObjectClass *object_class;
+	EFilterRuleClass *filter_rule_class;
+
+	g_type_class_add_private (class, sizeof (EMVFolderRulePrivate));
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->set_property = vfolder_rule_set_property;
+	object_class->get_property = vfolder_rule_get_property;
+	object_class->dispose = vfolder_rule_dispose;
+	object_class->finalize = vfolder_rule_finalize;
+
+	filter_rule_class = E_FILTER_RULE_CLASS (class);
+	filter_rule_class->validate = validate;
+	filter_rule_class->eq = vfolder_eq;
+	filter_rule_class->xml_encode = xml_encode;
+	filter_rule_class->xml_decode = xml_decode;
+	filter_rule_class->copy = rule_copy;
+	filter_rule_class->get_widget = get_widget;
+
+	g_object_class_install_property (
+		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			NULL,
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
+}
 
-	g_list_foreach (vr->sources, (GFunc)g_free, NULL);
-	g_list_free (vr->sources);
+static void
+em_vfolder_rule_init (EMVFolderRule *rule)
+{
+	rule->priv = EM_VFOLDER_RULE_GET_PRIVATE (rule);
 
-        G_OBJECT_CLASS (parent_class)->finalize (obj);
+	rule->with = EM_VFOLDER_RULE_WITH_SPECIFIC;
+	rule->rule.source = g_strdup ("incoming");
 }
 
-/**
- * em_vfolder_rule_new:
- *
- * Create a new EMVFolderRule object.
- *
- * Return value: A new #EMVFolderRule object.
- **/
-EMVFolderRule *
-em_vfolder_rule_new (void)
+EFilterRule *
+em_vfolder_rule_new (EMailSession *session)
+{
+	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
+
+	return g_object_new (
+		EM_TYPE_VFOLDER_RULE, "session", session, NULL);
+}
+
+EMailSession *
+em_vfolder_rule_get_session (EMVFolderRule *rule)
 {
-	return (EMVFolderRule *)g_object_new (em_vfolder_rule_get_type (), NULL, NULL);
+	g_return_val_if_fail (EM_IS_VFOLDER_RULE (rule), NULL);
+
+	return rule->priv->session;
 }
 
 void
-em_vfolder_rule_add_source (EMVFolderRule *vr, const gchar *uri)
+em_vfolder_rule_add_source (EMVFolderRule *rule,
+                            const gchar *uri)
 {
-	g_return_if_fail (EM_IS_VFOLDER_RULE (vr));
+	g_return_if_fail (EM_IS_VFOLDER_RULE (rule));
 	g_return_if_fail (uri);
 
-	vr->sources = g_list_append (vr->sources, g_strdup (uri));
+	rule->sources = g_list_append (rule->sources, g_strdup (uri));
 
-	e_filter_rule_emit_changed ((EFilterRule *)vr);
+	e_filter_rule_emit_changed (E_FILTER_RULE (rule));
 }
 
 const gchar *
-em_vfolder_rule_find_source (EMVFolderRule *vr, const gchar *uri)
+em_vfolder_rule_find_source (EMVFolderRule *rule,
+                             const gchar *uri)
 {
 	GList *l;
 
-	g_return_val_if_fail (EM_IS_VFOLDER_RULE (vr), NULL);
+	g_return_val_if_fail (EM_IS_VFOLDER_RULE (rule), NULL);
 
 	/* only does a simple string or address comparison, should
 	   probably do a decoded url comparison */
-	l = vr->sources;
+	l = rule->sources;
 	while (l) {
 		if (l->data == uri || !strcmp (l->data, uri))
 			return l->data;
@@ -171,31 +240,33 @@ em_vfolder_rule_find_source (EMVFolderRule *vr, const gchar *uri)
 }
 
 void
-em_vfolder_rule_remove_source (EMVFolderRule *vr, const gchar *uri)
+em_vfolder_rule_remove_source (EMVFolderRule *rule,
+                               const gchar *uri)
 {
 	gchar *found;
 
-	g_return_if_fail (EM_IS_VFOLDER_RULE (vr));
+	g_return_if_fail (EM_IS_VFOLDER_RULE (rule));
 
-	found =(gchar *)em_vfolder_rule_find_source (vr, uri);
+	found =(gchar *)em_vfolder_rule_find_source (rule, uri);
 	if (found) {
-		vr->sources = g_list_remove (vr->sources, found);
+		rule->sources = g_list_remove (rule->sources, found);
 		g_free (found);
-		e_filter_rule_emit_changed ((EFilterRule *)vr);
+		e_filter_rule_emit_changed (E_FILTER_RULE (rule));
 	}
 }
 
 const gchar *
-em_vfolder_rule_next_source (EMVFolderRule *vr, const gchar *last)
+em_vfolder_rule_next_source (EMVFolderRule *rule,
+                             const gchar *last)
 {
 	GList *node;
 
 	if (last == NULL) {
-		node = vr->sources;
+		node = rule->sources;
 	} else {
-		node = g_list_find (vr->sources, (gchar *)last);
+		node = g_list_find (rule->sources, (gchar *)last);
 		if (node == NULL)
-			node = vr->sources;
+			node = rule->sources;
 		else
 			node = g_list_next (node);
 	}
@@ -227,7 +298,7 @@ validate (EFilterRule *fr, EAlert **alert)
 		return 0;
 	}
 
-	return E_FILTER_RULE_CLASS (parent_class)->validate (fr, alert);
+	return E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->validate (fr, alert);
 }
 
 static gint
@@ -249,7 +320,7 @@ list_eq (GList *al, GList *bl)
 static gint
 vfolder_eq (EFilterRule *fr, EFilterRule *cm)
 {
-        return E_FILTER_RULE_CLASS (parent_class)->eq (fr, cm)
+        return E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->eq (fr, cm)
 		&& list_eq (((EMVFolderRule *)fr)->sources, ((EMVFolderRule *)cm)->sources);
 }
 
@@ -260,7 +331,7 @@ xml_encode (EFilterRule *fr)
 	xmlNodePtr node, set, work;
 	GList *l;
 
-        node = E_FILTER_RULE_CLASS (parent_class)->xml_encode (fr);
+        node = E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->xml_encode (fr);
 	g_return_val_if_fail (node != NULL, NULL);
 	g_return_val_if_fail (vr->with < G_N_ELEMENTS (with_names), NULL);
 
@@ -301,7 +372,7 @@ xml_decode (EFilterRule *fr, xmlNodePtr node, struct _ERuleContext *f)
 	EMVFolderRule *vr =(EMVFolderRule *)fr;
 	gchar *tmp;
 
-        result = E_FILTER_RULE_CLASS (parent_class)->xml_decode (fr, node, f);
+        result = E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->xml_decode (fr, node, f);
 	if (result != 0)
 		return result;
 
@@ -362,7 +433,7 @@ rule_copy (EFilterRule *dest, EFilterRule *src)
 
 	vdest->with = vsrc->with;
 
-	E_FILTER_RULE_CLASS (parent_class)->copy (dest, src);
+	E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->copy (dest, src);
 }
 
 enum {
@@ -510,13 +581,16 @@ static void
 source_add (GtkWidget *widget, struct _source_data *data)
 {
 	EMFolderTree *emft;
+	EMailSession *session;
 	GtkWidget *dialog;
 	gpointer parent;
 
 	parent = gtk_widget_get_toplevel (widget);
 	parent = gtk_widget_is_toplevel (parent) ? parent : NULL;
 
-	emft =(EMFolderTree *) em_folder_tree_new ();
+	session = em_vfolder_rule_get_session (data->vr);
+
+	emft = (EMFolderTree *) em_folder_tree_new (session);
 	emu_restore_folder_tree_state (emft);
 	em_folder_tree_set_excluded (emft, EMFT_EXCLUDE_NOSELECT);
 
@@ -590,7 +664,7 @@ get_widget (EFilterRule *fr, ERuleContext *rc)
 	GObject *object;
 	gint i;
 
-        widget = E_FILTER_RULE_CLASS (parent_class)->get_widget (fr, rc);
+        widget = E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->get_widget (fr, rc);
 
 	data = g_malloc0 (sizeof (*data));
 	data->rc = rc;
diff --git a/mail/em-vfolder-rule.h b/mail/em-vfolder-rule.h
index 8e77986..136c29a 100644
--- a/mail/em-vfolder-rule.h
+++ b/mail/em-vfolder-rule.h
@@ -21,16 +21,32 @@
  *
  */
 
-#ifndef _EM_VFOLDER_RULE_H
-#define _EM_VFOLDER_RULE_H
+#ifndef EM_VFOLDER_RULE_H
+#define EM_VFOLDER_RULE_H
 
-#include "filter/e-filter-rule.h"
+#include <mail/e-mail-session.h>
+#include <filter/e-filter-rule.h>
 
-#define EM_VFOLDER_RULE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), em_vfolder_rule_get_type(), EMVFolderRule))
-#define EM_VFOLDER_RULE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), em_vfolder_rule_get_type(), EMVFolderRuleClass))
-#define EM_IS_VFOLDER_RULE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), em_vfolder_rule_get_type()))
-#define EM_IS_VFOLDER_RULE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), em_vfolder_rule_get_type()))
-#define EM_VFOLDER_RULE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), em_vfolder_rule_get_type(), EMVFolderRuleClass))
+/* Standard GObject macros */
+#define EM_TYPE_VFOLDER_RULE \
+	(em_vfolder_rule_get_type ())
+#define EM_VFOLDER_RULE(obj) \
+	(G_TYPE_CHECK_INSTANCE_CAST \
+	((obj), EM_TYPE_VFOLDER_RULE, EMVFolderRule))
+#define EM_VFOLDER_RULE_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_CAST \
+	((cls), EM_TYPE_VFOLDER_RULE, EMVFolderRuleClass))
+#define EM_IS_VFOLDER_RULE(obj) \
+	(G_TYPE_CHECK_INSTANCE_TYPE \
+	((obj), EM_TYPE_VFOLDER_RULE))
+#define EM_IS_VFOLDER_RULE_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_TYPE \
+	((cls), EM_TYPE_VFOLDER_RULE))
+#define EM_VFOLDER_RULE_GET_CLASS(obj) \
+	(G_TYPE_INSTANCE_GET_CLASS \
+	((obj), EM_TYPE_VFOLDER_RULE, EMVFolderRuleClass))
+
+G_BEGIN_DECLS
 
 /* perhaps should be bits? */
 enum _em_vfolder_rule_with_t {
@@ -42,11 +58,13 @@ enum _em_vfolder_rule_with_t {
 
 typedef struct _EMVFolderRule EMVFolderRule;
 typedef struct _EMVFolderRuleClass EMVFolderRuleClass;
+typedef struct _EMVFolderRulePrivate EMVFolderRulePrivate;
 
 typedef enum _em_vfolder_rule_with_t em_vfolder_rule_with_t;
 
 struct _EMVFolderRule {
 	EFilterRule rule;
+	EMVFolderRulePrivate *priv;
 
 	em_vfolder_rule_with_t with;
 	GList *sources;		/* uri's of the source folders */
@@ -56,13 +74,18 @@ struct _EMVFolderRuleClass {
 	EFilterRuleClass parent_class;
 };
 
-GType        em_vfolder_rule_get_type (void);
-EMVFolderRule *em_vfolder_rule_new      (void);
+GType		em_vfolder_rule_get_type	(void);
+EFilterRule *	em_vfolder_rule_new		(EMailSession *session);
+EMailSession *	em_vfolder_rule_get_session	(EMVFolderRule *rule);
+void		em_vfolder_rule_add_source	(EMVFolderRule *rule,
+						 const gchar *uri);
+void		em_vfolder_rule_remove_source	(EMVFolderRule *rule,
+						 const gchar *uri);
+const gchar *	em_vfolder_rule_find_source	(EMVFolderRule *rule,
+						 const gchar *uri);
+const gchar *	em_vfolder_rule_next_source	(EMVFolderRule *rule,
+						 const gchar *last);
 
-/* methods */
-void         em_vfolder_rule_add_source    (EMVFolderRule *vr, const gchar *uri);
-void         em_vfolder_rule_remove_source (EMVFolderRule *vr, const gchar *uri);
-const gchar  *em_vfolder_rule_find_source   (EMVFolderRule *vr, const gchar *uri);
-const gchar  *em_vfolder_rule_next_source   (EMVFolderRule *vr, const gchar *last);
+G_END_DECLS
 
-#endif /* _EM_VFOLDER_RULE_H */
+#endif /* EM_VFOLDER_RULE_H */
diff --git a/mail/importers/elm-importer.c b/mail/importers/elm-importer.c
index 0164378..18548fc 100644
--- a/mail/importers/elm-importer.c
+++ b/mail/importers/elm-importer.c
@@ -41,7 +41,9 @@
 #include "mail-importer.h"
 
 #include "mail/mail-mt.h"
+#include "mail/e-mail-backend.h"
 #include "e-util/e-import.h"
+#include "shell/e-shell.h"
 
 #define d(x)
 
@@ -184,9 +186,19 @@ static MailImporterSpecial elm_special_folders[] = {
 static void
 elm_import_exec (struct _elm_import_msg *m)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	const gchar *maildir;
 	gchar *elmdir;
 
+	/* XXX Dig up the EMailSession from the default EShell.
+	 *     Since the EImport framework doesn't allow for user
+	 *     data, I don't see how else to get to it. */
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	maildir = elm_get_rc(m->import, "maildir");
 	if (maildir == NULL)
 		maildir = "Mail";
@@ -197,7 +209,7 @@ elm_import_exec (struct _elm_import_msg *m)
 		elmdir = g_strdup (maildir);
 
 	mail_importer_import_folders_sync (
-		elmdir, elm_special_folders, 0, m->status);
+		session, elmdir, elm_special_folders, 0, m->status);
 	g_free (elmdir);
 }
 
diff --git a/mail/importers/evolution-mbox-importer.c b/mail/importers/evolution-mbox-importer.c
index 4607770..aba9801 100644
--- a/mail/importers/evolution-mbox-importer.c
+++ b/mail/importers/evolution-mbox-importer.c
@@ -44,6 +44,7 @@
 #include "shell/e-shell-view.h"
 #include "shell/e-shell-sidebar.h"
 
+#include "mail/e-mail-backend.h"
 #include "mail/e-mail-local.h"
 #include "mail/e-mail-store.h"
 #include "mail/em-folder-selection-button.h"
@@ -79,13 +80,23 @@ folder_selected (EMFolderSelectionButton *button, EImportTargetURI *target)
 static GtkWidget *
 mbox_getwidget (EImport *ei, EImportTarget *target, EImportImporter *im)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	GtkWindow *window;
 	GtkWidget *hbox, *w;
 	GtkLabel *label;
 	gchar *select_uri = NULL;
 
+	/* XXX Dig up the EMailSession from the default EShell.
+	 *     Since the EImport framework doesn't allow for user
+	 *     data, I don't see how else to get to it. */
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	/* preselect the folder selected in a mail view */
-	window = e_shell_get_active_window (e_shell_get_default ());
+	window = e_shell_get_active_window (shell);
 	if (E_IS_SHELL_WINDOW (window)) {
 		EShellWindow *shell_window;
 		const gchar *view;
@@ -119,7 +130,8 @@ mbox_getwidget (EImport *ei, EImportTarget *target, EImportImporter *im)
 	label = GTK_LABEL (w);
 
 	w = em_folder_selection_button_new (
-		_("Select folder"), _("Select folder to import into"));
+		session, _("Select folder"),
+		_("Select folder to import into"));
 	gtk_label_set_mnemonic_widget (label, w);
 	em_folder_selection_button_set_selection ((EMFolderSelectionButton *)w, select_uri);
 	folder_selected (EM_FOLDER_SELECTION_BUTTON (w), (EImportTargetURI *)target);
@@ -215,9 +227,19 @@ mbox_import_done (gpointer data, GError **error)
 static void
 mbox_import (EImport *ei, EImportTarget *target, EImportImporter *im)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	MboxImporter *importer;
 	gchar *filename;
 
+	/* XXX Dig up the EMailSession from the default EShell.
+	 *     Since the EImport framework doesn't allow for user
+	 *     data, I don't see how else to get to it. */
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	/* TODO: do we validate target? */
 
 	importer = g_malloc0 (sizeof (*importer));
@@ -234,7 +256,7 @@ mbox_import (EImport *ei, EImportTarget *target, EImportImporter *im)
 
 	filename = g_filename_from_uri (((EImportTargetURI *)target)->uri_src, NULL, NULL);
 	mail_importer_import_mbox (
-		filename, ((EImportTargetURI *)target)->uri_dest,
+		session, filename, ((EImportTargetURI *)target)->uri_dest,
 		importer->cancellable, mbox_import_done, importer);
 	g_free (filename);
 }
diff --git a/mail/importers/mail-importer.c b/mail/importers/mail-importer.c
index 08d0141..ff987f4 100644
--- a/mail/importers/mail-importer.c
+++ b/mail/importers/mail-importer.c
@@ -43,13 +43,14 @@
 #include "mail-mt.h"
 #include "mail-tools.h"
 #include "e-mail-local.h"
-#include "mail-session.h"
+#include "e-mail-session.h"
 
 #include "mail-importer.h"
 
 struct _import_mbox_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	gchar *path;
 	gchar *uri;
 	GCancellable *cancellable;
@@ -122,8 +123,8 @@ import_mbox_exec (struct _import_mbox_msg *m)
 	if (m->uri == NULL || m->uri[0] == 0)
 		folder = e_mail_local_get_folder (E_MAIL_FOLDER_INBOX);
 	else
-		folder = mail_tool_uri_to_folder (
-			m->uri, CAMEL_STORE_FOLDER_CREATE,
+		folder = e_mail_session_uri_to_folder_sync (
+			m->session, m->uri, CAMEL_STORE_FOLDER_CREATE,
 			m->base.cancellable, &m->base.error);
 
 	if (folder == NULL)
@@ -212,6 +213,7 @@ import_mbox_done (struct _import_mbox_msg *m)
 static void
 import_mbox_free (struct _import_mbox_msg *m)
 {
+	g_object_unref (m->session);
 	if (m->cancellable)
 		g_object_unref (m->cancellable);
 	g_free (m->uri);
@@ -227,12 +229,18 @@ static MailMsgInfo import_mbox_info = {
 };
 
 gint
-mail_importer_import_mbox (const gchar *path, const gchar *folderuri, GCancellable *cancellable, void (*done)(gpointer data, GError **), gpointer data)
+mail_importer_import_mbox (EMailSession *session,
+                           const gchar *path,
+                           const gchar *folderuri,
+                           GCancellable *cancellable,
+                           void (*done)(gpointer data, GError **),
+                           gpointer data)
 {
 	struct _import_mbox_msg *m;
 	gint id;
 
 	m = mail_msg_new (&import_mbox_info);
+	m->session = g_object_ref (session);
 	m->path = g_strdup (path);
 	m->uri = g_strdup (folderuri);
 	m->done = done;
@@ -247,11 +255,15 @@ mail_importer_import_mbox (const gchar *path, const gchar *folderuri, GCancellab
 }
 
 void
-mail_importer_import_mbox_sync (const gchar *path, const gchar *folderuri, GCancellable *cancellable)
+mail_importer_import_mbox_sync (EMailSession *session,
+                                const gchar *path,
+                                const gchar *folderuri,
+                                GCancellable *cancellable)
 {
 	struct _import_mbox_msg *m;
 
 	m = mail_msg_new (&import_mbox_info);
+	m->session = g_object_ref (session);
 	m->path = g_strdup (path);
 	m->uri = g_strdup (folderuri);
 	if (cancellable)
@@ -264,13 +276,16 @@ mail_importer_import_mbox_sync (const gchar *path, const gchar *folderuri, GCanc
 
 struct _import_folders_data {
 	MailImporterSpecial *special_folders;
+	EMailSession *session;
 	GCancellable *cancellable;
 
 	guint elmfmt:1;
 };
 
 static void
-import_folders_rec (struct _import_folders_data *m, const gchar *filepath, const gchar *folderparent)
+import_folders_rec (struct _import_folders_data *m,
+                    const gchar *filepath,
+                    const gchar *folderparent)
 {
 	GDir *dir;
 	const gchar *d;
@@ -319,7 +334,8 @@ import_folders_rec (struct _import_folders_data *m, const gchar *filepath, const
 		}
 
 		printf("importing to uri %s\n", uri);
-		mail_importer_import_mbox_sync (filefull, uri, m->cancellable);
+		mail_importer_import_mbox_sync (
+			m->session, filefull, uri, m->cancellable);
 		g_free (uri);
 
 		/* This little gem re-uses the stat buffer and filefull to automagically scan mozilla-format folders */
@@ -363,12 +379,17 @@ import_folders_rec (struct _import_folders_data *m, const gchar *filepath, const
  * standard unix directories.
  **/
 void
-mail_importer_import_folders_sync (const gchar *filepath, MailImporterSpecial special_folders[], gint flags, GCancellable *cancellable)
+mail_importer_import_folders_sync (EMailSession *session,
+                                   const gchar *filepath,
+                                   MailImporterSpecial special_folders[],
+                                   gint flags,
+                                   GCancellable *cancellable)
 {
 	struct _import_folders_data m;
 
 	m.special_folders = special_folders;
 	m.elmfmt = (flags & MAIL_IMPORTER_MOZFMT) == 0;
+	m.session = g_object_ref (session);
 	m.cancellable = cancellable;
 
 	import_folders_rec (&m, filepath, NULL);
diff --git a/mail/importers/mail-importer.h b/mail/importers/mail-importer.h
index 12caada..93d1897 100644
--- a/mail/importers/mail-importer.h
+++ b/mail/importers/mail-importer.h
@@ -26,6 +26,7 @@
 
 #include <e-util/e-import.h>
 #include <camel/camel.h>
+#include <mail/e-mail-session.h>
 
 EImportImporter *mbox_importer_peek (void);
 
@@ -46,8 +47,16 @@ EImportImporter *pine_importer_peek (void);
 #define MSG_FLAG_MARKED 0x0004
 #define MSG_FLAG_EXPUNGED 0x0008
 
-gint mail_importer_import_mbox (const gchar *path, const gchar *folderuri, GCancellable *cancellable, void (*done)(gpointer data, GError **), gpointer data);
-void mail_importer_import_mbox_sync (const gchar *path, const gchar *folderuri, GCancellable *cancellable);
+gint		mail_importer_import_mbox	(EMailSession *session,
+						 const gchar *path,
+						 const gchar *folderuri,
+						 GCancellable *cancellable,
+						 void (*done)(gpointer data, GError **),
+						 gpointer data);
+void		mail_importer_import_mbox_sync	(EMailSession *session,
+						 const gchar *path,
+						 const gchar *folderuri,
+						 GCancellable *cancellable);
 
 struct _MailImporterSpecial {
 	const gchar *orig, *new;
@@ -58,6 +67,11 @@ typedef struct _MailImporterSpecial MailImporterSpecial;
 #define MAIL_IMPORTER_MOZFMT (1<<0)
 
 /* api in flux */
-void mail_importer_import_folders_sync (const gchar *filepath, MailImporterSpecial special_folders[], gint flags, GCancellable *cancellable);
+void		mail_importer_import_folders_sync
+						(EMailSession *session,
+						 const gchar *filepath,
+						 MailImporterSpecial special_folders[],
+						 gint flags,
+						 GCancellable *cancellable);
 
 #endif
diff --git a/mail/importers/pine-importer.c b/mail/importers/pine-importer.c
index ad9b7d3..26a37b8 100644
--- a/mail/importers/pine-importer.c
+++ b/mail/importers/pine-importer.c
@@ -44,7 +44,9 @@
 #include "mail-importer.h"
 
 #include "mail/mail-mt.h"
+#include "mail/e-mail-backend.h"
 #include "e-util/e-import.h"
+#include "shell/e-shell.h"
 
 #define d(x)
 
@@ -230,6 +232,17 @@ static MailImporterSpecial pine_special_folders[] = {
 static void
 pine_import_exec (struct _pine_import_msg *m)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
+
+	/* XXX Dig up the EMailSession from the default EShell.
+	 *     Since the EImport framework doesn't allow for user
+	 *     data, I don't see how else to get to it. */
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	if (GPOINTER_TO_INT(g_datalist_get_data(&m->target->data, "pine-do-addr")))
 		import_contacts ();
 
@@ -238,7 +251,7 @@ pine_import_exec (struct _pine_import_msg *m)
 
 		path = g_build_filename(g_get_home_dir(), "mail", NULL);
 		mail_importer_import_folders_sync (
-			path, pine_special_folders, 0, m->cancellable);
+			session, path, pine_special_folders, 0, m->cancellable);
 		g_free (path);
 	}
 }
diff --git a/mail/mail-autofilter.c b/mail/mail-autofilter.c
index 5fa343e..566369a 100644
--- a/mail/mail-autofilter.c
+++ b/mail/mail-autofilter.c
@@ -30,9 +30,9 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 
+#include "e-mail-session.h"
 #include "mail-vfolder.h"
 #include "mail-autofilter.h"
-#include "mail-session.h"
 #include "em-utils.h"
 #include "e-util/e-alert-dialog.h"
 #include "e-util/e-util-private.h"
@@ -297,50 +297,69 @@ rule_from_message (EFilterRule *rule, ERuleContext *context, CamelMimeMessage *m
 }
 
 EFilterRule *
-em_vfolder_rule_from_message (EMVFolderContext *context, CamelMimeMessage *msg, gint flags, const gchar *source)
+em_vfolder_rule_from_message (EMVFolderContext *context,
+                              CamelMimeMessage *msg,
+                              gint flags,
+                              const gchar *source)
 {
-	EMVFolderRule *rule;
+	EFilterRule *rule;
+	EMailSession *session;
 	gchar *euri = em_uri_from_camel (source);
 
-	rule = em_vfolder_rule_new ();
-	em_vfolder_rule_add_source (rule, euri);
-	rule_from_message ((EFilterRule *)rule, (ERuleContext *)context, msg, flags);
+	session = em_vfolder_context_get_session (context);
+
+	rule = em_vfolder_rule_new (session);
+	em_vfolder_rule_add_source (EM_VFOLDER_RULE (rule), euri);
+	rule_from_message (rule, E_RULE_CONTEXT (context), msg, flags);
 	g_free (euri);
 
-	return (EFilterRule *)rule;
+	return rule;
 }
 
 EFilterRule *
-em_vfolder_rule_from_address (EMVFolderContext *context, CamelInternetAddress *addr, gint flags, const gchar *source)
+em_vfolder_rule_from_address (EMVFolderContext *context,
+                              CamelInternetAddress *addr,
+                              gint flags,
+                              const gchar *source)
 {
-	EMVFolderRule *rule;
+	EFilterRule *rule;
+	EMailSession *session;
 	gchar *euri = em_uri_from_camel (source);
 
-	rule = em_vfolder_rule_new ();
-	em_vfolder_rule_add_source (rule, euri);
-	rule_from_address ((EFilterRule *)rule, (ERuleContext *)context, addr, flags);
+	session = em_vfolder_context_get_session (context);
+
+	rule = em_vfolder_rule_new (session);
+	em_vfolder_rule_add_source (EM_VFOLDER_RULE (rule), euri);
+	rule_from_address (rule, E_RULE_CONTEXT (context), addr, flags);
 	g_free (euri);
 
-	return (EFilterRule *)rule;
+	return rule;
 }
 
 EFilterRule *
-filter_rule_from_message (EMFilterContext *context, CamelMimeMessage *msg, gint flags)
+filter_rule_from_message (EMFilterContext *context,
+                          CamelMimeMessage *msg,
+                          gint flags)
 {
-	EMFilterRule *rule;
+	EFilterRule *rule;
 	EFilterPart *part;
 
 	rule = em_filter_rule_new ();
-	rule_from_message ((EFilterRule *)rule, (ERuleContext *)context, msg, flags);
+	rule_from_message (rule, E_RULE_CONTEXT (context), msg, flags);
 
 	part = em_filter_context_next_action (context, NULL);
-	em_filter_rule_add_action (rule, e_filter_part_clone (part));
 
-	return (EFilterRule *)rule;
+	em_filter_rule_add_action (
+		EM_FILTER_RULE (rule), e_filter_part_clone (part));
+
+	return rule;
 }
 
 void
-filter_gui_add_from_message (CamelMimeMessage *msg, const gchar *source, gint flags)
+filter_gui_add_from_message (EMailSession *session,
+                             CamelMimeMessage *msg,
+                             const gchar *source,
+                             gint flags)
 {
 	EMFilterContext *fc;
 	const gchar *config_dir;
@@ -349,7 +368,7 @@ filter_gui_add_from_message (CamelMimeMessage *msg, const gchar *source, gint fl
 
 	g_return_if_fail (msg != NULL);
 
-	fc = em_filter_context_new ();
+	fc = em_filter_context_new (session);
 	config_dir = mail_session_get_config_dir ();
 	user = g_build_filename (config_dir, "filters.xml", NULL);
 	system = g_build_filename (EVOLUTION_PRIVDATADIR, "filtertypes.xml", NULL);
@@ -366,7 +385,10 @@ filter_gui_add_from_message (CamelMimeMessage *msg, const gchar *source, gint fl
 }
 
 void
-mail_filter_rename_uri (CamelStore *store, const gchar *olduri, const gchar *newuri)
+mail_filter_rename_uri (EMailSession *session,
+                        CamelStore *store,
+                        const gchar *olduri,
+                        const gchar *newuri)
 {
 	EMFilterContext *fc;
 	const gchar *config_dir;
@@ -377,7 +399,7 @@ mail_filter_rename_uri (CamelStore *store, const gchar *olduri, const gchar *new
 	eolduri = em_uri_from_camel (olduri);
 	enewuri = em_uri_from_camel (newuri);
 
-	fc = em_filter_context_new ();
+	fc = em_filter_context_new (session);
 	config_dir = mail_session_get_config_dir ();
 	user = g_build_filename (config_dir, "filters.xml", NULL);
 	system = g_build_filename (EVOLUTION_PRIVDATADIR, "filtertypes.xml", NULL);
@@ -400,7 +422,9 @@ mail_filter_rename_uri (CamelStore *store, const gchar *olduri, const gchar *new
 }
 
 void
-mail_filter_delete_uri (CamelStore *store, const gchar *uri)
+mail_filter_delete_uri (EMailSession *session,
+                        CamelStore *store,
+                        const gchar *uri)
 {
 	EMFilterContext *fc;
 	const gchar *config_dir;
@@ -410,7 +434,7 @@ mail_filter_delete_uri (CamelStore *store, const gchar *uri)
 
 	euri = em_uri_from_camel (uri);
 
-	fc = em_filter_context_new ();
+	fc = em_filter_context_new (session);
 	config_dir = mail_session_get_config_dir ();
 	user = g_build_filename (config_dir, "filters.xml", NULL);
 	system = g_build_filename (EVOLUTION_PRIVDATADIR, "filtertypes.xml", NULL);
diff --git a/mail/mail-autofilter.h b/mail/mail-autofilter.h
index 7e385eb..35e5c3a 100644
--- a/mail/mail-autofilter.h
+++ b/mail/mail-autofilter.h
@@ -21,8 +21,8 @@
  *
  */
 
-#ifndef _MAIL_AUTOFILTER_H
-#define _MAIL_AUTOFILTER_H
+#ifndef MAIL_AUTOFILTER_H
+#define MAIL_AUTOFILTER_H
 
 #include <camel/camel.h>
 
@@ -37,15 +37,32 @@ enum {
 	AUTO_MLIST = 8
 };
 
-EFilterRule *em_vfolder_rule_from_message (EMVFolderContext *context, CamelMimeMessage *msg, gint flags, const gchar *source);
-EFilterRule *filter_rule_from_message (EMFilterContext *context, CamelMimeMessage *msg, gint flags);
-EFilterRule *em_vfolder_rule_from_address (EMVFolderContext *context, CamelInternetAddress *addr, gint flags, const gchar *source);
+EFilterRule *	em_vfolder_rule_from_message	(EMVFolderContext *context,
+						 CamelMimeMessage *msg,
+						 gint flags,
+						 const gchar *source);
+EFilterRule *	filter_rule_from_message	(EMFilterContext *context,
+						 CamelMimeMessage *msg,
+						 gint flags);
+EFilterRule *	em_vfolder_rule_from_address	(EMVFolderContext *context,
+						 CamelInternetAddress *addr,
+						 gint flags,
+						 const gchar *source);
 
 /* easiest place to put this */
-void  filter_gui_add_from_message (CamelMimeMessage *msg, const gchar *source, gint flags);
+void		filter_gui_add_from_message	(EMailSession *session,
+						 CamelMimeMessage *msg,
+						 const gchar *source,
+						 gint flags);
 
-/* Also easiest place for these, we should really share a global rule context for this stuff ... */
-void mail_filter_rename_uri (CamelStore *store, const gchar *olduri, const gchar *newuri);
-void mail_filter_delete_uri (CamelStore *store, const gchar *uri);
+/* Also easiest place for these, we should really
+ * share a global rule context for this stuff ... */
+void		mail_filter_rename_uri		(EMailSession *session,
+						 CamelStore *store,
+						 const gchar *olduri,
+						 const gchar *newuri);
+void		mail_filter_delete_uri		(EMailSession *session,
+						 CamelStore *store,
+						 const gchar *uri);
 
-#endif
+#endif /* MAIL_AUTOFILTER_H */
diff --git a/mail/mail-config.c b/mail/mail-config.c
index 730aeba..a89722d 100644
--- a/mail/mail-config.c
+++ b/mail/mail-config.c
@@ -38,9 +38,9 @@
 #include <gconf/gconf-client.h>
 
 #include "e-mail-local.h"
+#include "e-mail-session.h"
 #include "mail-config.h"
 #include "mail-folder-cache.h"
-#include "mail-session.h"
 #include "mail-tools.h"
 
 typedef struct {
@@ -147,8 +147,10 @@ gconf_outlook_filenames_changed (GConfClient *client, guint cnxn_id,
 }
 
 static void
-gconf_jh_headers_changed (GConfClient *client, guint cnxn_id,
-                          GConfEntry *entry, gpointer user_data)
+gconf_jh_headers_changed (GConfClient *client,
+                          guint cnxn_id,
+                          GConfEntry *entry,
+                          CamelSession *session)
 {
 	GSList *node;
 	GPtrArray *name, *value;
@@ -168,7 +170,9 @@ gconf_jh_headers_changed (GConfClient *client, guint cnxn_id,
 		node = node->next;
 		g_strfreev (tok);
 	}
-	mail_session_set_junk_headers ((const gchar **)name->pdata, (const gchar **)value->pdata, name->len);
+	camel_session_set_junk_headers (
+		session, (const gchar **) name->pdata,
+		(const gchar **) value->pdata, name->len);
 
 	g_ptr_array_foreach (name, (GFunc) g_free, NULL);
 	g_ptr_array_foreach (value, (GFunc) g_free, NULL);
@@ -177,14 +181,17 @@ gconf_jh_headers_changed (GConfClient *client, guint cnxn_id,
 }
 
 static void
-gconf_jh_check_changed (GConfClient *client, guint cnxn_id,
-		     GConfEntry *entry, gpointer user_data)
+gconf_jh_check_changed (GConfClient *client,
+                        guint cnxn_id,
+                        GConfEntry *entry,
+                        CamelSession *session)
 {
-	config->jh_check = gconf_client_get_bool (config->gconf, "/apps/evolution/mail/junk/check_custom_header", NULL);
+	config->jh_check = gconf_client_get_bool (
+		config->gconf, "/apps/evolution/mail/junk/check_custom_header", NULL);
 	if (!config->jh_check) {
-		mail_session_set_junk_headers (NULL, NULL, 0);
+		camel_session_set_junk_headers (session, NULL, NULL, 0);
 	} else {
-		gconf_jh_headers_changed (NULL, 0, NULL, NULL);
+		gconf_jh_headers_changed (NULL, 0, NULL, session);
 	}
 }
 
@@ -240,8 +247,7 @@ mail_config_write (void)
 GConfClient *
 mail_config_get_gconf_client (void)
 {
-	if (!config)
-		mail_config_init ();
+	g_return_val_if_fail (config != NULL, NULL);
 
 	return config->gconf;
 }
@@ -543,22 +549,22 @@ mail_config_folder_to_cachename (CamelFolder *folder, const gchar *prefix)
 }
 
 void
-mail_config_reload_junk_headers (void)
+mail_config_reload_junk_headers (CamelSession *session)
 {
+	g_return_if_fail (CAMEL_IS_SESSION (session));
+
 	/* It automatically sets in the session */
 	if (config == NULL)
-		mail_config_init ();
+		mail_config_init (session);
 	else
-		gconf_jh_check_changed (config->gconf, 0, NULL, config);
+		gconf_jh_check_changed (config->gconf, 0, NULL, session);
 
 }
 
 gboolean
 mail_config_get_lookup_book (void)
 {
-	/* It automatically sets in the session */
-	if (config == NULL)
-		mail_config_init ();
+	g_return_val_if_fail (config != NULL, FALSE);
 
 	return config->book_lookup;
 }
@@ -566,9 +572,7 @@ mail_config_get_lookup_book (void)
 gboolean
 mail_config_get_lookup_book_local_only (void)
 {
-	/* It automatically sets in the session */
-	if (config == NULL)
-		mail_config_init ();
+	g_return_val_if_fail (config != NULL, FALSE);
 
 	return config->book_lookup_local_only;
 }
@@ -588,11 +592,13 @@ folder_renamed_cb (MailFolderCache *cache, CamelStore *store, const gchar *oldur
 
 /* Config struct routines */
 void
-mail_config_init (void)
+mail_config_init (CamelSession *session)
 {
 	GConfClientNotifyFunc func;
 	const gchar *key;
 
+	g_return_if_fail (CAMEL_IS_SESSION (session));
+
 	if (config)
 		return;
 
@@ -677,14 +683,14 @@ mail_config_init (void)
 	key = "/apps/evolution/mail/junk/check_custom_header";
 	func = (GConfClientNotifyFunc) gconf_jh_check_changed;
 	gconf_client_notify_add (
-		config->gconf, key, func, NULL, NULL, NULL);
+		config->gconf, key, func, session, NULL, NULL);
 	config->jh_check =
 		gconf_client_get_bool (config->gconf, key, NULL);
 
 	key = "/apps/evolution/mail/junk/custom_header";
 	func = (GConfClientNotifyFunc) gconf_jh_headers_changed;
 	gconf_client_notify_add (
-		config->gconf, key, func, NULL, NULL, NULL);
+		config->gconf, key, func, session, NULL, NULL);
 
 	key = "/apps/evolution/mail/junk/lookup_addressbook";
 	func = (GConfClientNotifyFunc) gconf_bool_value_changed;
@@ -710,7 +716,7 @@ mail_config_init (void)
 	config->scripts_disabled =
 		gconf_client_get_bool (config->gconf, key, NULL);
 
-	gconf_jh_check_changed (config->gconf, 0, NULL, config);
+	gconf_jh_check_changed (config->gconf, 0, NULL, session);
 
 	g_signal_connect (mail_folder_cache_get_default (), "folder-deleted",
 			  (GCallback) folder_deleted_cb, NULL);
diff --git a/mail/mail-config.h b/mail/mail-config.h
index eee2958..8fa7359 100644
--- a/mail/mail-config.h
+++ b/mail/mail-config.h
@@ -69,7 +69,7 @@ typedef enum {
 GType		evolution_mail_config_get_type	(void);
 
 /* Configuration */
-void		mail_config_init		(void);
+void		mail_config_init		(CamelSession *session);
 void		mail_config_write		(void);
 
 GConfClient *	mail_config_get_gconf_client	(void);
@@ -98,7 +98,7 @@ gchar *		mail_config_folder_to_safe_url	(CamelFolder *folder);
 
 gint		mail_config_get_sync_timeout	(void);
 
-void		mail_config_reload_junk_headers	(void);
+void		mail_config_reload_junk_headers	(CamelSession *session);
 gboolean	mail_config_get_lookup_book	(void);
 gboolean	mail_config_get_lookup_book_local_only (void);
 
diff --git a/mail/mail-folder-cache.c b/mail/mail-folder-cache.c
index f1c6fbd..8aed1b5 100644
--- a/mail/mail-folder-cache.c
+++ b/mail/mail-folder-cache.c
@@ -47,11 +47,11 @@
 #include "mail-mt.h"
 #include "mail-folder-cache.h"
 #include "mail-ops.h"
-#include "mail-session.h"
 #include "mail-tools.h"
 
 #include "em-utils.h"
 #include "e-mail-local.h"
+#include "e-mail-session.h"
 
 #define w(x)
 #define d(x)
@@ -1093,6 +1093,7 @@ mail_folder_cache_get_default (void)
  */
 void
 mail_folder_cache_note_store (MailFolderCache *self,
+                              CamelSession *session,
                               CamelStore *store,
                               GCancellable *cancellable,
                               NoteDoneFunc done,
diff --git a/mail/mail-folder-cache.h b/mail/mail-folder-cache.h
index 870b7fe..1705923 100644
--- a/mail/mail-folder-cache.h
+++ b/mail/mail-folder-cache.h
@@ -82,6 +82,7 @@ GType		mail_folder_cache_get_type	(void) G_GNUC_CONST;
 MailFolderCache *
 		mail_folder_cache_get_default	(void);
 void		mail_folder_cache_note_store	(MailFolderCache *self,
+						 CamelSession *session,
 						 CamelStore *store,
 						 GCancellable *cancellable,
 						 NoteDoneFunc done,
diff --git a/mail/mail-ops.c b/mail/mail-ops.c
index 4fd6ab2..d0db1c8 100644
--- a/mail/mail-ops.c
+++ b/mail/mail-ops.c
@@ -40,10 +40,10 @@
 #include "em-utils.h"
 #include "mail-mt.h"
 #include "mail-ops.h"
-#include "mail-session.h"
 #include "mail-tools.h"
 
 #include "e-mail-local.h"
+#include "e-mail-session.h"
 
 #define w(x)
 #define d(x)
@@ -56,6 +56,7 @@ const gchar *x_mailer = "Evolution " VERSION SUB_VERSION " " VERSION_COMMENT;
 struct _filter_mail_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	CamelFolder *source_folder; /* where they come from */
 	GPtrArray *source_uids;	/* uids to copy, or NULL == copy all */
 	CamelUIDCache *cache;  /* UID cache if we are to cache the uids, NULL otherwise */
@@ -140,6 +141,11 @@ em_filter_folder_element_done (struct _filter_mail_msg *m)
 static void
 em_filter_folder_element_free (struct _filter_mail_msg *m)
 {
+	mail_session_flush_filter_log (m->session);
+
+	if (m->session)
+		g_object_unref (m->session);
+
 	if (m->source_folder)
 		g_object_unref (m->source_folder);
 
@@ -151,8 +157,6 @@ em_filter_folder_element_free (struct _filter_mail_msg *m)
 
 	if (m->driver)
 		g_object_unref (m->driver);
-
-	mail_session_flush_filter_log ();
 }
 
 static MailMsgInfo em_filter_folder_element_info = {
@@ -164,19 +168,23 @@ static MailMsgInfo em_filter_folder_element_info = {
 };
 
 void
-mail_filter_folder (CamelFolder *source_folder, GPtrArray *uids,
-		    const gchar *type, gboolean notify)
+mail_filter_folder (EMailSession *session,
+                    CamelFolder *source_folder,
+                    GPtrArray *uids,
+                    const gchar *type,
+                    gboolean notify)
 {
 	struct _filter_mail_msg *m;
 
 	m = mail_msg_new (&em_filter_folder_element_info);
-	m->source_folder = source_folder;
-	g_object_ref (source_folder);
+	m->session = g_object_ref (session);
+	m->source_folder = g_object_ref (source_folder);
 	m->source_uids = uids;
 	m->cache = NULL;
 	m->delete = FALSE;
 
-	m->driver = camel_session_get_filter_driver (session, type, NULL);
+	m->driver = camel_session_get_filter_driver (
+		CAMEL_SESSION (session), type, NULL);
 
 	if (!notify) {
 		/* FIXME: have a #define NOTIFY_FILTER_NAME macro? */
@@ -187,19 +195,6 @@ mail_filter_folder (CamelFolder *source_folder, GPtrArray *uids,
 	mail_msg_unordered_push (m);
 }
 
-/* convenience functions for it */
-void
-mail_filter_on_demand (CamelFolder *folder, GPtrArray *uids)
-{
-	mail_filter_folder (folder, uids, E_FILTER_SOURCE_DEMAND, FALSE);
-}
-
-void
-mail_filter_junk (CamelFolder *folder, GPtrArray *uids)
-{
-	mail_filter_folder (folder, uids, E_FILTER_SOURCE_JUNKTEST, FALSE);
-}
-
 /* ********************************************************************** */
 
 /* Temporary workaround for various issues. Gone before 0.11 */
@@ -262,10 +257,9 @@ fetch_mail_exec (struct _fetch_mail_msg *m)
 		CamelFolder *folder;
 
 		folder = fm->source_folder =
-			mail_tool_get_inbox (
-				m->source_uri,
-				fm->base.cancellable,
-				&fm->base.error);
+			e_mail_session_get_inbox_sync (
+				fm->session, m->source_uri,
+				fm->base.cancellable, &fm->base.error);
 
 		if (folder) {
 			/* this handles 'keep on server' stuff, if we have any new uid's to copy
@@ -353,7 +347,7 @@ fetch_mail_free (struct _fetch_mail_msg *m)
 {
 	g_free (m->source_uri);
 
-	if (m->cancellable)
+	if (m->cancellable != NULL)
 		g_object_unref (m->cancellable);
 
 	em_filter_folder_element_free ((struct _filter_mail_msg *) m);
@@ -369,7 +363,8 @@ static MailMsgInfo fetch_mail_info = {
 
 /* ouch, a 'do everything' interface ... */
 void
-mail_fetch_mail (const gchar *source,
+mail_fetch_mail (EMailSession *session,
+                 const gchar *source,
                  gint keep,
                  const gchar *type,
                  GCancellable *cancellable,
@@ -385,6 +380,7 @@ mail_fetch_mail (const gchar *source,
 
 	m = mail_msg_new (&fetch_mail_info);
 	fm = (struct _filter_mail_msg *)m;
+	fm->session = g_object_ref (session);
 	m->source_uri = g_strdup (source);
 	fm->delete = !keep;
 	fm->cache = NULL;
@@ -393,7 +389,8 @@ mail_fetch_mail (const gchar *source,
 	m->done = done;
 	m->data = data;
 
-	fm->driver = camel_session_get_filter_driver (session, type, NULL);
+	fm->driver = camel_session_get_filter_driver (
+		CAMEL_SESSION (session), type, NULL);
 	camel_filter_driver_set_folder_func (fm->driver, get_folder, get_data);
 	if (status)
 		camel_filter_driver_set_status_func (fm->driver, status, status_data);
@@ -439,7 +436,24 @@ static const gchar *resent_recipients[] = {
 	CAMEL_RECIPIENT_TYPE_RESENT_BCC
 };
 
-struct _send_queue_msg;
+struct _send_queue_msg {
+	MailMsg base;
+
+	EMailSession *session;
+	CamelFolder *queue;
+	gchar *destination;
+
+	CamelFilterDriver *driver;
+	GCancellable *cancellable;
+
+	/* we use camelfilterstatusfunc, even though its not the filter doing it */
+	CamelFilterStatusFunc *status;
+	gpointer status_data;
+
+	void (*done)(const gchar *destination, gpointer data);
+	gpointer data;
+};
+
 static void report_status (struct _send_queue_msg *m, enum camel_filter_status_t status, gint pc, const gchar *desc, ...);
 
 /* send 1 message to a specific transport */
@@ -534,7 +548,8 @@ mail_send_message (struct _send_queue_msg *m,
 
 	if (camel_address_length (recipients) > 0) {
 		xport = camel_session_get_transport (
-			session, transport_url ? transport_url :
+			CAMEL_SESSION (m->session),
+			transport_url ? transport_url :
 			destination, error);
 		if (xport == NULL)
 			goto exit;
@@ -558,7 +573,8 @@ mail_send_message (struct _send_queue_msg *m,
 
 		uri = g_strstrip (g_strdup (header->value));
 		/* FIXME Not passing a GCancellable or GError here. */
-		folder = mail_tool_uri_to_folder (uri, 0, NULL, NULL);
+		folder = e_mail_session_uri_to_folder_sync (
+			m->session, uri, 0, NULL, NULL);
 		if (folder) {
 			/* FIXME Not passing a GCancellable or GError here. */
 			camel_folder_append_message_sync (
@@ -595,8 +611,9 @@ mail_send_message (struct _send_queue_msg *m,
 		GError *local_error = NULL;
 
 		if (sent_folder_uri) {
-			folder = mail_tool_uri_to_folder (
-				sent_folder_uri, 0, cancellable, &local_error);
+			folder = e_mail_session_uri_to_folder_sync (
+				m->session, sent_folder_uri, 0,
+				cancellable, &local_error);
 			if (folder == NULL) {
 				g_string_append_printf (
 					err, _("Failed to append to %s: %s\n"
@@ -699,23 +716,6 @@ exit:
 
 /* ** SEND MAIL QUEUE ***************************************************** */
 
-struct _send_queue_msg {
-	MailMsg base;
-
-	CamelFolder *queue;
-	gchar *destination;
-
-	CamelFilterDriver *driver;
-	GCancellable *cancellable;
-
-	/* we use camelfilterstatusfunc, even though its not the filter doing it */
-	CamelFilterStatusFunc *status;
-	gpointer status_data;
-
-	void (*done)(const gchar *destination, gpointer data);
-	gpointer data;
-};
-
 static void
 report_status (struct _send_queue_msg *m, enum camel_filter_status_t status, gint pc, const gchar *desc, ...)
 {
@@ -865,7 +865,9 @@ send_queue_desc (struct _send_queue_msg *m)
 static void
 send_queue_free (struct _send_queue_msg *m)
 {
-	if (m->driver)
+	if (m->session != NULL)
+		g_object_unref (m->session);
+	if (m->driver != NULL)
 		g_object_unref (m->driver);
 	g_object_unref (m->queue);
 	g_free (m->destination);
@@ -883,7 +885,8 @@ static MailMsgInfo send_queue_info = {
 
 /* same interface as fetch_mail, just 'cause i'm lazy today (and we need to run it from the same spot?) */
 void
-mail_send_queue (CamelFolder *queue,
+mail_send_queue (EMailSession *session,
+                 CamelFolder *queue,
                  const gchar *destination,
                  const gchar *type,
                  GCancellable *cancellable,
@@ -897,6 +900,7 @@ mail_send_queue (CamelFolder *queue,
 	struct _send_queue_msg *m;
 
 	m = mail_msg_new (&send_queue_info);
+	m->session = g_object_ref (session);
 	m->queue = g_object_ref (queue);
 	m->destination = g_strdup (destination);
 	if (G_IS_CANCELLABLE (cancellable)) {
@@ -910,7 +914,8 @@ mail_send_queue (CamelFolder *queue,
 	m->done = done;
 	m->data = data;
 
-	m->driver = camel_session_get_filter_driver (session, type, NULL);
+	m->driver = camel_session_get_filter_driver (
+		CAMEL_SESSION (session), type, NULL);
 	camel_filter_driver_set_folder_func (m->driver, get_folder, get_data);
 
 	mail_msg_unordered_push (m);
@@ -1005,6 +1010,7 @@ mail_append_mail (CamelFolder *folder, CamelMimeMessage *message, CamelMessageIn
 struct _transfer_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	CamelFolder *source;
 	GPtrArray *uids;
 	gboolean delete;
@@ -1028,8 +1034,8 @@ transfer_messages_exec (struct _transfer_msg *m)
 {
 	CamelFolder *dest;
 
-	dest = mail_tool_uri_to_folder (
-		m->dest_uri, m->dest_flags,
+	dest = e_mail_session_uri_to_folder_sync (
+		m->session, m->dest_uri, m->dest_flags,
 		m->base.cancellable, &m->base.error);
 	if (dest == NULL)
 		return;
@@ -1076,6 +1082,7 @@ transfer_messages_done (struct _transfer_msg *m)
 static void
 transfer_messages_free (struct _transfer_msg *m)
 {
+	g_object_unref (m->session);
 	g_object_unref (m->source);
 	g_free (m->dest_uri);
 	em_utils_uids_free (m->uids);
@@ -1090,12 +1097,14 @@ static MailMsgInfo transfer_messages_info = {
 };
 
 void
-mail_transfer_messages (CamelFolder *source, GPtrArray *uids,
-			gboolean delete_from_source,
-			const gchar *dest_uri,
-			guint32 dest_flags,
-			void (*done) (gboolean ok, gpointer data),
-			gpointer data)
+mail_transfer_messages (EMailSession *session,
+                        CamelFolder *source,
+                        GPtrArray *uids,
+                        gboolean delete_from_source,
+                        const gchar *dest_uri,
+                        guint32 dest_flags,
+                        void (*done) (gboolean ok, gpointer data),
+                        gpointer data)
 {
 	struct _transfer_msg *m;
 
@@ -1104,8 +1113,8 @@ mail_transfer_messages (CamelFolder *source, GPtrArray *uids,
 	g_return_if_fail (dest_uri != NULL);
 
 	m = mail_msg_new (&transfer_messages_info);
-	m->source = source;
-	g_object_ref (source);
+	m->session = g_object_ref (session);
+	m->source = g_object_ref (source);
 	m->uids = uids;
 	m->delete = delete_from_source;
 	m->dest_uri = g_strdup (dest_uri);
@@ -1277,6 +1286,7 @@ mail_build_attachment (CamelFolder *folder, GPtrArray *uids,
 struct _get_folder_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	gchar *uri;
 	guint32 flags;
 	CamelFolder *folder;
@@ -1293,8 +1303,9 @@ get_folder_desc (struct _get_folder_msg *m)
 static void
 get_folder_exec (struct _get_folder_msg *m)
 {
-	m->folder = mail_tool_uri_to_folder (
-		m->uri, m->flags, m->base.cancellable, &m->base.error);
+	m->folder = e_mail_session_uri_to_folder_sync (
+		m->session, m->uri, m->flags,
+		m->base.cancellable, &m->base.error);
 }
 
 static void
@@ -1307,6 +1318,7 @@ get_folder_done (struct _get_folder_msg *m)
 static void
 get_folder_free (struct _get_folder_msg *m)
 {
+	g_object_unref (m->session);
 	g_free (m->uri);
 	if (m->folder)
 		g_object_unref (m->folder);
@@ -1321,14 +1333,18 @@ static MailMsgInfo get_folder_info = {
 };
 
 gint
-mail_get_folder (const gchar *uri, guint32 flags,
-		 void (*done)(gchar *uri, CamelFolder *folder, gpointer data),
-		 gpointer data, MailMsgDispatchFunc dispatch)
+mail_get_folder (EMailSession *session,
+                 const gchar *uri,
+                 guint32 flags,
+                 void (*done)(gchar *uri, CamelFolder *folder, gpointer data),
+                 gpointer data,
+                 MailMsgDispatchFunc dispatch)
 {
 	struct _get_folder_msg *m;
 	gint id;
 
 	m = mail_msg_new (&get_folder_info);
+	m->session = g_object_ref (session);
 	m->uri = g_strdup (uri);
 	m->flags = flags;
 	m->data = data;
@@ -1417,6 +1433,7 @@ mail_get_folder_quota (CamelFolder *folder,
 struct _get_store_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	gchar *uri;
 	CamelStore *store;
 	void (*done) (gchar *uri, CamelStore *store, gpointer data);
@@ -1434,9 +1451,9 @@ get_store_exec (struct _get_store_msg *m)
 {
 	/*camel_session_get_store connects us, which we don't want to do on startup. */
 
-	m->store = (CamelStore *) camel_session_get_service (session, m->uri,
-							     CAMEL_PROVIDER_STORE,
-							     &m->base.error);
+	m->store = (CamelStore *) camel_session_get_service (
+		CAMEL_SESSION (m->session), m->uri,
+		CAMEL_PROVIDER_STORE, &m->base.error);
 }
 
 static void
@@ -1449,6 +1466,7 @@ get_store_done (struct _get_store_msg *m)
 static void
 get_store_free (struct _get_store_msg *m)
 {
+	g_object_unref (m->session);
 	g_free (m->uri);
 	if (m->store)
 		g_object_unref (m->store);
@@ -1463,7 +1481,8 @@ static MailMsgInfo get_store_info = {
 };
 
 gint
-mail_get_store (const gchar *uri,
+mail_get_store (EMailSession *session,
+                const gchar *uri,
                 GCancellable *cancellable,
                 void (*done) (gchar *uri, CamelStore *store, gpointer data),
                 gpointer data)
@@ -1476,6 +1495,7 @@ mail_get_store (const gchar *uri,
 		g_object_unref (m->base.cancellable);
 		m->base.cancellable = g_object_ref (cancellable);
 	}
+	m->session = g_object_ref (session);
 	m->uri = g_strdup (uri);
 	m->data = data;
 	m->done = done;
@@ -1833,6 +1853,7 @@ mail_expunge_folder (CamelFolder *folder, void (*done) (CamelFolder *folder, gpo
 struct _empty_trash_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	EAccount *account;
 	void (*done) (EAccount *account, gpointer data);
 	gpointer data;
@@ -1853,14 +1874,15 @@ empty_trash_exec (struct _empty_trash_msg *m)
 	gchar *uri;
 
 	if (m->account) {
-		trash = mail_tool_get_trash (
-			m->account->source->url, FALSE,
+		trash = e_mail_session_get_trash_sync (
+			m->session, m->account->source->url,
 			m->base.cancellable, &m->base.error);
 	} else {
 		data_dir = mail_session_get_data_dir ();
 		uri = g_strdup_printf ("mbox:%s/local", data_dir);
-		trash = mail_tool_get_trash (
-			uri, TRUE, m->base.cancellable, &m->base.error);
+		trash = e_mail_session_get_trash_sync (
+			m->session, uri,
+			m->base.cancellable, &m->base.error);
 		g_free (uri);
 	}
 
@@ -1881,6 +1903,8 @@ empty_trash_done (struct _empty_trash_msg *m)
 static void
 empty_trash_free (struct _empty_trash_msg *m)
 {
+	if (m->session)
+		g_object_unref (m->session);
 	if (m->account)
 		g_object_unref (m->account);
 }
@@ -1894,11 +1918,15 @@ static MailMsgInfo empty_trash_info = {
 };
 
 void
-mail_empty_trash (EAccount *account, void (*done) (EAccount *account, gpointer data), gpointer data)
+mail_empty_trash (EMailSession *session,
+                  EAccount *account,
+                  void (*done) (EAccount *account, gpointer data),
+                  gpointer data)
 {
 	struct _empty_trash_msg *m;
 
 	m = mail_msg_new (&empty_trash_info);
+	m->session = g_object_ref (session);
 	m->account = account;
 	if (account)
 		g_object_ref (account);
@@ -2308,6 +2336,7 @@ mail_save_messages (CamelFolder *folder, GPtrArray *uids, const gchar *path,
 struct _prep_offline_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	GCancellable *cancel;
 	gchar *uri;
 	void (*done)(const gchar *uri, gpointer data);
@@ -2319,8 +2348,9 @@ prep_offline_exec (struct _prep_offline_msg *m)
 {
 	CamelFolder *folder;
 
-	folder = mail_tool_uri_to_folder (
-		m->uri, 0, m->base.cancellable, &m->base.error);
+	folder = e_mail_session_uri_to_folder_sync (
+		m->session, m->uri, 0,
+		m->base.cancellable, &m->base.error);
 	if (folder) {
 		if (CAMEL_IS_DISCO_FOLDER (folder)) {
 			camel_disco_folder_prepare_for_offline (
@@ -2349,6 +2379,7 @@ prep_offline_done (struct _prep_offline_msg *m)
 static void
 prep_offline_free (struct _prep_offline_msg *m)
 {
+	g_object_unref (m->session);
 	if (m->cancel)
 		g_object_unref (m->cancel);
 	g_free (m->uri);
@@ -2363,14 +2394,16 @@ static MailMsgInfo prep_offline_info = {
 };
 
 void
-mail_prep_offline (const gchar *uri,
-		  CamelOperation *cancel,
-		  void (*done)(const gchar *, gpointer data),
-		  gpointer data)
+mail_prep_offline (EMailSession *session,
+                   const gchar *uri,
+                   CamelOperation *cancel,
+                   void (*done)(const gchar *, gpointer data),
+                   gpointer data)
 {
 	struct _prep_offline_msg *m;
 
 	m = mail_msg_new (&prep_offline_info);
+	m->session = g_object_ref (session);
 	if (G_IS_CANCELLABLE (cancel))
 		m->cancel = g_object_ref (cancel);
 	m->uri = g_strdup (uri);
@@ -2568,6 +2601,7 @@ mail_execute_shell_command (CamelFilterDriver *driver, gint argc, gchar **argv,
 struct _check_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	gchar *url;
 	CamelProviderType type;
 	GList *authtypes;
@@ -2587,7 +2621,9 @@ check_service_exec (struct _check_msg *m)
 {
 	CamelService *service;
 
-	service = camel_session_get_service (session, m->url, m->type, &m->base.error);
+	service = camel_session_get_service (
+		CAMEL_SESSION (m->session), m->url,
+		m->type, &m->base.error);
 	if (!service)
 		return;
 
@@ -2606,6 +2642,7 @@ check_service_done (struct _check_msg *m)
 static void
 check_service_free (struct _check_msg *m)
 {
+	g_object_unref (m->session);
 	g_free (m->url);
 	g_list_free (m->authtypes);
 }
@@ -2619,12 +2656,17 @@ static MailMsgInfo check_service_info = {
 };
 
 gint
-mail_check_service (const gchar *url, CamelProviderType type, void (*done)(const gchar *url, CamelProviderType type, GList *authtypes, gpointer data), gpointer data)
+mail_check_service (EMailSession *session,
+                    const gchar *url,
+                    CamelProviderType type,
+                    void (*done)(const gchar *url, CamelProviderType type, GList *authtypes, gpointer data),
+                    gpointer data)
 {
 	struct _check_msg *m;
 	gint id;
 
 	m = mail_msg_new (&check_service_info);
+	m->session = g_object_ref (session);
 	m->url = g_strdup (url);
 	m->type = type;
 	m->done = done;
diff --git a/mail/mail-ops.h b/mail/mail-ops.h
index 4bea1b0..751ac9d 100644
--- a/mail/mail-ops.h
+++ b/mail/mail-ops.h
@@ -29,19 +29,22 @@ G_BEGIN_DECLS
 #include <camel/camel.h>
 #include <libedataserver/e-account.h>
 
-#include "mail-mt.h"
+#include <mail/mail-mt.h>
+#include <mail/e-mail-session.h>
 
 void mail_append_mail (CamelFolder *folder, CamelMimeMessage *message, CamelMessageInfo *info,
 		       void (*done)(CamelFolder *folder, CamelMimeMessage *msg, CamelMessageInfo *info, gint ok,
 				    const gchar *appended_uid, gpointer data),
 		       gpointer data);
 
-void mail_transfer_messages (CamelFolder *source, GPtrArray *uids,
-			     gboolean delete_from_source,
-			     const gchar *dest_uri,
-			     guint32 dest_flags,
-			     void (*done) (gboolean ok, gpointer data),
-			     gpointer data);
+void		mail_transfer_messages		(EMailSession *session,
+						 CamelFolder *source,
+						 GPtrArray *uids,
+						 gboolean delete_from_source,
+						 const gchar *dest_uri,
+						 guint32 dest_flags,
+						 void (*done) (gboolean ok, gpointer data),
+						 gpointer data);
 
 /* get a single message, asynchronously */
 gint mail_get_message (CamelFolder *folder, const gchar *uid,
@@ -60,9 +63,12 @@ gint mail_get_messages (CamelFolder *folder, GPtrArray *uids,
 			gpointer data);
 
 /* same for a folder */
-gint mail_get_folder (const gchar *uri, guint32 flags,
-		     void (*done) (gchar *uri, CamelFolder *folder, gpointer data), gpointer data,
-		     MailMsgDispatchFunc dispatch);
+gint		mail_get_folder			(EMailSession *session,
+						 const gchar *uri,
+						 guint32 flags,
+						 void (*done) (gchar *uri, CamelFolder *folder, gpointer data),
+						 gpointer data,
+						 MailMsgDispatchFunc dispatch);
 
 /* get quota information for a folder */
 gint mail_get_folder_quota (CamelFolder *folder,
@@ -71,8 +77,11 @@ gint mail_get_folder_quota (CamelFolder *folder,
 		 gpointer data, MailMsgDispatchFunc dispatch);
 
 /* and for a store */
-gint mail_get_store (const gchar *uri, GCancellable *cancellable,
-		    void (*done) (gchar *uri, CamelStore *store, gpointer data), gpointer data);
+gint		mail_get_store			(EMailSession *session,
+						 const gchar *uri,
+						 GCancellable *cancellable,
+						 void (*done) (gchar *uri, CamelStore *store, gpointer data),
+						 gpointer data);
 
 /* build an attachment */
 void mail_build_attachment (CamelFolder *folder, GPtrArray *uids,
@@ -94,9 +103,10 @@ void mail_expunge_folder (CamelFolder *folder,
 			  void (*done) (CamelFolder *folder, gpointer data),
 			  gpointer data);
 
-void mail_empty_trash (EAccount *account,
-		       void (*done) (EAccount *account, gpointer data),
-		       gpointer data);
+void		mail_empty_trash		(EMailSession *session,
+						 EAccount *account,
+						 void (*done) (EAccount *account, gpointer data),
+						 gpointer data);
 
 /* get folder info asynchronously */
 gint		mail_get_folderinfo		(CamelStore *store,
@@ -121,7 +131,8 @@ gint mail_save_messages (CamelFolder *folder, GPtrArray *uids, const gchar *path
 			gpointer data);
 
 /* yeah so this is messy, but it does a lot, maybe i can consolidate all user_data's to be the one */
-void		mail_send_queue			(CamelFolder *queue,
+void		mail_send_queue			(EMailSession *session,
+						 CamelFolder *queue,
 						 const gchar *destination,
 						 const gchar *type,
 						 GCancellable *cancellable,
@@ -132,7 +143,8 @@ void		mail_send_queue			(CamelFolder *queue,
 						 void (*done)(const gchar *destination, gpointer data),
 						 gpointer data);
 
-void		mail_fetch_mail			(const gchar *source,
+void		mail_fetch_mail			(EMailSession *session,
+						 const gchar *source,
 						 gint keep,
 						 const gchar *type,
 						 GCancellable *cancellable,
@@ -143,19 +155,18 @@ void		mail_fetch_mail			(const gchar *source,
 						 void (*done)(const gchar *source, gpointer data),
 						 gpointer data);
 
-void		mail_filter_folder		(CamelFolder *source_folder,
+void		mail_filter_folder		(EMailSession *session,
+						 CamelFolder *source_folder,
 						 GPtrArray *uids,
 						 const gchar *type,
 						 gboolean notify);
 
-/* convenience functions for above */
-void mail_filter_on_demand (CamelFolder *folder, GPtrArray *uids);
-void mail_filter_junk (CamelFolder *folder, GPtrArray *uids);
-
 /* Work Offline */
-void mail_prep_offline (const gchar *uri, CamelOperation *cancel,
-		       void (*done)(const gchar *, gpointer data),
-		       gpointer data);
+void		mail_prep_offline		(EMailSession *session,
+						 const gchar *uri,
+						 CamelOperation *cancel,
+						 void (*done)(const gchar *, gpointer data),
+						 gpointer data);
 gint mail_store_set_offline (CamelStore *store, gboolean offline,
 			   void (*done)(CamelStore *, gpointer data),
 			   gpointer data);
@@ -164,8 +175,11 @@ gint mail_store_prepare_offline (CamelStore *store);
 /* filter driver execute shell command async callback */
 void mail_execute_shell_command (CamelFilterDriver *driver, gint argc, gchar **argv, gpointer data);
 
-gint mail_check_service (const gchar *url, CamelProviderType type,
-		       void (*done)(const gchar *url, CamelProviderType type, GList *authtypes, gpointer data), gpointer data);
+gint		mail_check_service		(EMailSession *session,
+						 const gchar *url,
+						 CamelProviderType type,
+						 void (*done)(const gchar *url, CamelProviderType type, GList *authtypes, gpointer data),
+						 gpointer data);
 
 G_END_DECLS
 
diff --git a/mail/mail-send-recv.c b/mail/mail-send-recv.c
index f83c6da..c2584cf 100644
--- a/mail/mail-send-recv.c
+++ b/mail/mail-send-recv.c
@@ -36,6 +36,7 @@
 #include "e-util/gconf-bridge.h"
 
 #include "e-mail-local.h"
+#include "e-mail-session.h"
 #include "em-event.h"
 #include "em-filter-rule.h"
 #include "mail-config.h"
@@ -43,7 +44,6 @@
 #include "mail-mt.h"
 #include "mail-ops.h"
 #include "mail-send-recv.h"
-#include "mail-session.h"
 #include "mail-tools.h"
 
 #define d(x)
@@ -101,6 +101,7 @@ typedef enum {
 
 struct _send_info {
 	send_info_t type;		/* 0 = fetch, 1 = send */
+	EMailSession *session;
 	GCancellable *cancellable;
 	gchar *uri;
 	gboolean keep_on_server;
@@ -144,9 +145,11 @@ free_folder_info (struct _folder_info *info)
 static void
 free_send_info (struct _send_info *info)
 {
-	g_free (info->uri);
+	if (info->session)
+		g_object_unref (info->session);
 	if (info->cancellable)
 		g_object_unref (info->cancellable);
+	g_free (info->uri);
 	if (info->timeout_id != 0)
 		g_source_remove (info->timeout_id);
 	g_free (info->what);
@@ -404,6 +407,7 @@ get_receive_type (const gchar *url)
 
 static struct _send_data *
 build_dialog (GtkWindow *parent,
+              EMailSession *session,
               EAccountList *accounts,
               CamelFolder *outbox,
               const gchar *destination)
@@ -524,6 +528,7 @@ build_dialog (GtkWindow *parent,
 
 			info = g_malloc0 (sizeof (*info));
 			info->type = type;
+			info->session = g_object_ref (session);
 
 			d(printf("adding source %s\n", source->url));
 
@@ -762,13 +767,14 @@ receive_done (const gchar *uri, gpointer data)
 		local_outbox = e_mail_local_get_folder (E_MAIL_FOLDER_OUTBOX);
 
 		info->again = 0;
-		mail_send_queue (local_outbox,
-				 info->uri,
-				 E_FILTER_SOURCE_OUTGOING,
-				 info->cancellable,
-				 receive_get_folder, info,
-				 receive_status, info,
-				 receive_done, info);
+		mail_send_queue (
+			info->session,
+			local_outbox, info->uri,
+			E_FILTER_SOURCE_OUTGOING,
+			info->cancellable,
+			receive_get_folder, info,
+			receive_status, info,
+			receive_done, info);
 		return;
 	}
 
@@ -831,7 +837,8 @@ receive_get_folder (CamelFilterDriver *d,
 		return oldinfo->folder;
 	}
 	/* FIXME Not passing a GCancellable here. */
-	folder = mail_tool_uri_to_folder (uri, 0, NULL, error);
+	folder = e_mail_session_uri_to_folder_sync (
+		info->session, uri, 0, NULL, error);
 	if (!folder)
 		return NULL;
 
@@ -906,7 +913,8 @@ refresh_folders_exec (struct _refresh_folders_msg *m)
 	get_folders (m->store, m->folders, m->finfo);
 
 	for (i=0;i<m->folders->len;i++) {
-		folder = mail_tool_uri_to_folder (
+		folder = e_mail_session_uri_to_folder_sync (
+			m->info->session,
 			m->folders->pdata[i], 0,
 			m->base.cancellable, &local_error);
 		if (folder) {
@@ -986,6 +994,7 @@ receive_update_got_store (gchar *uri, CamelStore *store, gpointer data)
 	if (store) {
 		mail_folder_cache_note_store (
 			mail_folder_cache_get_default (),
+			CAMEL_SESSION (info->session),
 			store, info->cancellable,
 			receive_update_got_folderinfo, info);
 	} else {
@@ -994,7 +1003,8 @@ receive_update_got_store (gchar *uri, CamelStore *store, gpointer data)
 }
 
 GtkWidget *
-mail_send_receive (GtkWindow *parent)
+mail_send_receive (GtkWindow *parent,
+                   EMailSession *session)
 {
 	CamelFolder *local_outbox;
 	struct _send_data *data;
@@ -1009,7 +1019,7 @@ mail_send_receive (GtkWindow *parent)
 		return send_recv_dialog;
 	}
 
-	if (!camel_session_get_online (session))
+	if (!camel_session_get_online (CAMEL_SESSION (session)))
 		return send_recv_dialog;
 
 	account = e_get_default_account ();
@@ -1020,7 +1030,8 @@ mail_send_receive (GtkWindow *parent)
 
 	local_outbox = e_mail_local_get_folder (E_MAIL_FOLDER_OUTBOX);
 	data = build_dialog (
-		parent, accounts, local_outbox, account->transport->url);
+		parent, session, accounts,
+		local_outbox, account->transport->url);
 	scan = data->infos;
 	while (scan) {
 		struct _send_info *info = scan->data;
@@ -1028,7 +1039,7 @@ mail_send_receive (GtkWindow *parent)
 		switch (info->type) {
 		case SEND_RECEIVE:
 			mail_fetch_mail (
-				info->uri,
+				session, info->uri,
 				info->keep_on_server,
 				E_FILTER_SOURCE_INCOMING,
 				info->cancellable,
@@ -1039,7 +1050,7 @@ mail_send_receive (GtkWindow *parent)
 		case SEND_SEND:
 			/* todo, store the folder in info? */
 			mail_send_queue (
-				local_outbox, info->uri,
+				session, local_outbox, info->uri,
 				E_FILTER_SOURCE_OUTGOING,
 				info->cancellable,
 				receive_get_folder, info,
@@ -1048,7 +1059,7 @@ mail_send_receive (GtkWindow *parent)
 			break;
 		case SEND_UPDATE:
 			mail_get_store (
-				info->uri, info->cancellable,
+				session, info->uri, info->cancellable,
 				receive_update_got_store, info);
 			break;
 		default:
@@ -1062,6 +1073,7 @@ mail_send_receive (GtkWindow *parent)
 
 struct _auto_data {
 	EAccount *account;
+	EMailSession *session;
 	gint period;		/* in seconds */
 	gint timeout_id;
 };
@@ -1073,7 +1085,7 @@ auto_timeout (gpointer data)
 {
 	struct _auto_data *info = data;
 
-	if (camel_session_get_online (session)) {
+	if (camel_session_get_online (CAMEL_SESSION (info->session))) {
 		const gchar *uri;
 		gboolean keep_on_server;
 
@@ -1081,7 +1093,7 @@ auto_timeout (gpointer data)
 			info->account, E_ACCOUNT_SOURCE_URL);
 		keep_on_server = e_account_get_bool (
 			info->account, E_ACCOUNT_SOURCE_KEEP_ON_SERVER);
-		mail_receive_uri (uri, keep_on_server);
+		mail_receive_uri (info->session, uri, keep_on_server);
 	}
 
 	return TRUE;
@@ -1103,6 +1115,8 @@ auto_account_removed (EAccountList *eal, EAccount *ea, gpointer dummy)
 static void
 auto_account_finalised (struct _auto_data *info)
 {
+	if (info->session != NULL)
+		g_object_unref (info->session);
 	if (info->timeout_id)
 		g_source_remove (info->timeout_id);
 	g_free (info);
@@ -1131,12 +1145,15 @@ auto_account_commit (struct _auto_data *info)
 }
 
 static void
-auto_account_added (EAccountList *eal, EAccount *ea, gpointer dummy)
+auto_account_added (EAccountList *eal,
+                    EAccount *ea,
+                    EMailSession *session)
 {
 	struct _auto_data *info;
 
 	info = g_malloc0 (sizeof (*info));
 	info->account = ea;
+	info->session = g_object_ref (session);
 	g_object_set_data_full (
 		G_OBJECT (ea), "mail-autoreceive", info,
 		(GDestroyNotify) auto_account_finalised);
@@ -1178,15 +1195,15 @@ auto_online (EShell *shell)
 /* call to setup initial, and after changes are made to the config */
 /* FIXME: Need a cleanup funciton for when object is deactivated */
 void
-mail_autoreceive_init (EShellBackend *shell_backend,
-                       CamelSession *session)
+mail_autoreceive_init (EMailBackend *backend)
 {
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	EAccountList *accounts;
 	EIterator *iter;
 	EShell *shell;
 
-	g_return_if_fail (E_IS_SHELL_BACKEND (shell_backend));
-	g_return_if_fail (CAMEL_IS_SESSION (session));
+	g_return_if_fail (E_IS_MAIL_BACKEND (backend));
 
 	if (auto_active)
 		return;
@@ -1194,9 +1211,11 @@ mail_autoreceive_init (EShellBackend *shell_backend,
 	accounts = e_get_account_list ();
 	auto_active = g_hash_table_new (g_str_hash, g_str_equal);
 
+	session = e_mail_backend_get_session (backend);
+
 	g_signal_connect (
 		accounts, "account-added",
-		G_CALLBACK (auto_account_added), NULL);
+		G_CALLBACK (auto_account_added), session);
 	g_signal_connect (
 		accounts, "account-removed",
 		G_CALLBACK (auto_account_removed), NULL);
@@ -1208,8 +1227,10 @@ mail_autoreceive_init (EShellBackend *shell_backend,
 	     e_iterator_is_valid (iter);
 	     e_iterator_next (iter))
 		auto_account_added (
-			accounts, (EAccount *) e_iterator_get (iter), NULL);
+			accounts, (EAccount *)
+			e_iterator_get (iter), session);
 
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 
 	auto_online (shell);
@@ -1222,7 +1243,9 @@ mail_autoreceive_init (EShellBackend *shell_backend,
 /* We setup the download info's in a hashtable, if we later
  * need to build the gui, we insert them in to add them. */
 void
-mail_receive_uri (const gchar *uri, gboolean keep_on_server)
+mail_receive_uri (EMailSession *session,
+                  const gchar *uri,
+                  gboolean keep_on_server)
 {
 	struct _send_info *info;
 	struct _send_data *data;
@@ -1246,6 +1269,7 @@ mail_receive_uri (const gchar *uri, gboolean keep_on_server)
 
 	info = g_malloc0 (sizeof (*info));
 	info->type = type;
+	info->session = g_object_ref (session);
 	info->progress_bar = NULL;
 	info->status_label = NULL;
 	info->uri = g_strdup (uri);
@@ -1267,7 +1291,8 @@ mail_receive_uri (const gchar *uri, gboolean keep_on_server)
 	switch (info->type) {
 	case SEND_RECEIVE:
 		mail_fetch_mail (
-			info->uri, info->keep_on_server,
+			info->session, info->uri,
+			info->keep_on_server,
 			E_FILTER_SOURCE_INCOMING,
 			info->cancellable,
 			receive_get_folder, info,
@@ -1278,6 +1303,7 @@ mail_receive_uri (const gchar *uri, gboolean keep_on_server)
 		/* todo, store the folder in info? */
 		local_outbox = e_mail_local_get_folder (E_MAIL_FOLDER_OUTBOX);
 		mail_send_queue (
+			info->session,
 			local_outbox, info->uri,
 			E_FILTER_SOURCE_OUTGOING,
 			info->cancellable,
@@ -1287,6 +1313,7 @@ mail_receive_uri (const gchar *uri, gboolean keep_on_server)
 		break;
 	case SEND_UPDATE:
 		mail_get_store (
+			info->session,
 			info->uri, info->cancellable,
 			receive_update_got_store, info);
 		break;
@@ -1296,7 +1323,7 @@ mail_receive_uri (const gchar *uri, gboolean keep_on_server)
 }
 
 void
-mail_send (void)
+mail_send (EMailSession *session)
 {
 	CamelFolder *local_outbox;
 	EAccountService *transport;
@@ -1326,6 +1353,7 @@ mail_send (void)
 
 	info = g_malloc0 (sizeof (*info));
 	info->type = SEND_SEND;
+	info->session = g_object_ref (session);
 	info->progress_bar = NULL;
 	info->status_label = NULL;
 	info->uri = g_strdup (transport->url);
@@ -1342,10 +1370,12 @@ mail_send (void)
 
 	/* todo, store the folder in info? */
 	local_outbox = e_mail_local_get_folder (E_MAIL_FOLDER_OUTBOX);
-	mail_send_queue (local_outbox, info->uri,
-			 E_FILTER_SOURCE_OUTGOING,
-			 info->cancellable,
-			 receive_get_folder, info,
-			 receive_status, info,
-			 receive_done, info);
+	mail_send_queue (
+		session,
+		local_outbox, info->uri,
+		E_FILTER_SOURCE_OUTGOING,
+		info->cancellable,
+		receive_get_folder, info,
+		receive_status, info,
+		receive_done, info);
 }
diff --git a/mail/mail-send-recv.h b/mail/mail-send-recv.h
index db144e2..67b0647 100644
--- a/mail/mail-send-recv.h
+++ b/mail/mail-send-recv.h
@@ -25,22 +25,23 @@
 
 #include <gtk/gtk.h>
 #include <camel/camel.h>
-#include <shell/e-shell-backend.h>
+#include <mail/e-mail-backend.h>
 
 G_BEGIN_DECLS
 
 /* send/receive all uri's */
-GtkWidget *	mail_send_receive		(GtkWindow *parent);
+GtkWidget *	mail_send_receive		(GtkWindow *parent,
+						 EMailSession *session);
 
 /* receive a single uri */
-void		mail_receive_uri		(const gchar *uri,
+void		mail_receive_uri		(EMailSession *session,
+						 const gchar *uri,
 						 gboolean keep_on_server);
 
-void		mail_send			(void);
+void		mail_send			(EMailSession *session);
 
 /* setup auto receive stuff */
-void		mail_autoreceive_init		(EShellBackend *shell_backend,
-						 CamelSession *session);
+void		mail_autoreceive_init		(EMailBackend *backend);
 
 G_END_DECLS
 
diff --git a/mail/mail-tools.c b/mail/mail-tools.c
index 3a77da2..c005c27 100644
--- a/mail/mail-tools.c
+++ b/mail/mail-tools.c
@@ -36,76 +36,13 @@
 
 #include <glib/gi18n.h>
 
+#include "e-mail-session.h"
 #include "em-utils.h"
 #include "mail-folder-cache.h"
-#include "mail-session.h"
 #include "mail-tools.h"
 
 /* **************************************** */
 
-CamelFolder *
-mail_tool_get_inbox (const gchar *url,
-                     GCancellable *cancellable,
-                     GError **error)
-{
-	CamelStore *store;
-	CamelFolder *folder;
-
-	store = camel_session_get_store (session, url, error);
-	if (!store)
-		return NULL;
-
-	folder = camel_store_get_inbox_folder_sync (store, cancellable, error);
-	g_object_unref (store);
-
-	return folder;
-}
-
-static gboolean
-is_local_provider (CamelStore *store)
-{
-	CamelProvider *provider;
-
-	g_return_val_if_fail (store != NULL, FALSE);
-
-	provider = camel_service_get_provider (CAMEL_SERVICE (store));
-
-	g_return_val_if_fail (provider != NULL, FALSE);
-
-	return (provider->flags & CAMEL_PROVIDER_IS_LOCAL) != 0;
-}
-
-CamelFolder *
-mail_tool_get_trash (const gchar *url,
-                     gint connect,
-                     GCancellable *cancellable,
-                     GError **error)
-{
-	CamelStore *store;
-	CamelFolder *trash;
-
-	if (connect)
-		store = camel_session_get_store (session, url, error);
-	else
-		store = (CamelStore *) camel_session_get_service (
-			session, url, CAMEL_PROVIDER_STORE, error);
-
-	if (!store)
-		return NULL;
-
-	if (connect ||
-		(CAMEL_SERVICE (store)->status == CAMEL_SERVICE_CONNECTED ||
-		is_local_provider (store)))
-		trash = camel_store_get_trash_folder_sync (
-			store, cancellable, error);
-	else
-		trash = NULL;
-
-	g_object_unref (store);
-
-	return trash;
-}
-
 #ifndef G_OS_WIN32
 
 static gchar *
@@ -296,82 +233,6 @@ mail_tool_make_message_attachment (CamelMimeMessage *message)
 	return part;
 }
 
-CamelFolder *
-mail_tool_uri_to_folder (const gchar *uri,
-                         guint32 flags,
-                         GCancellable *cancellable,
-                         GError **error)
-{
-	CamelURL *url;
-	CamelStore *store = NULL;
-	CamelFolder *folder = NULL;
-	gint offset = 0;
-	gchar *curi = NULL;
-
-	g_return_val_if_fail (uri != NULL, NULL);
-
-	/* TODO: vtrash and vjunk are no longer used for these uri's */
-	if (!strncmp (uri, "vtrash:", 7))
-		offset = 7;
-	else if (!strncmp (uri, "vjunk:", 6))
-		offset = 6;
-	else if (!strncmp(uri, "email:", 6)) {
-		/* FIXME?: the filter:get_folder callback should do this itself? */
-		curi = em_uri_to_camel (uri);
-		if (uri == NULL) {
-			g_set_error (
-				error,
-				CAMEL_ERROR, CAMEL_ERROR_GENERIC,
-				_("Invalid folder: '%s'"), uri);
-			return NULL;
-		}
-		uri = curi;
-	}
-
-	url = camel_url_new (uri + offset, error);
-	if (!url) {
-		g_free (curi);
-		return NULL;
-	}
-
-	store = (CamelStore *) camel_session_get_service (
-		session, uri + offset, CAMEL_PROVIDER_STORE, error);
-	if (store) {
-		const gchar *name;
-
-		/* if we have a fragment, then the path is actually used by the store,
-		   so the fragment is the path to the folder instead */
-		if (url->fragment) {
-			name = url->fragment;
-		} else {
-			if (url->path && *url->path)
-				name = url->path + 1;
-			else
-				name = "";
-		}
-
-		if (offset) {
-			if (offset == 7)
-				folder = camel_store_get_trash_folder_sync (
-					store, cancellable, error);
-			else if (offset == 6)
-				folder = camel_store_get_junk_folder_sync (
-					store, cancellable, error);
-		} else
-			folder = camel_store_get_folder_sync (
-				store, name, flags, cancellable, error);
-		g_object_unref (store);
-	}
-
-	if (folder)
-		mail_folder_cache_note_folder (mail_folder_cache_get_default (), folder);
-
-	camel_url_free (url);
-	g_free (curi);
-
-	return folder;
-}
-
 /* FIXME: This should be a property on CamelFolder */
 gchar *
 mail_tools_folder_to_url (CamelFolder *folder)
diff --git a/mail/mail-tools.h b/mail/mail-tools.h
index 21d3510..096ec7f 100644
--- a/mail/mail-tools.h
+++ b/mail/mail-tools.h
@@ -26,17 +26,6 @@
 #include <glib.h>
 #include <camel/camel.h>
 
-/* Get the "inbox" for a url (uses global session) */
-CamelFolder *	mail_tool_get_inbox		(const gchar *url,
-						 GCancellable *cancellable,
-						 GError **error);
-
-/* Get the "trash" for a url (uses global session) */
-CamelFolder *	mail_tool_get_trash		(const gchar *url,
-						 gint connect,
-						 GCancellable *cancellable,
-						 GError **error);
-
 /* Does a camel_movemail into the local movemail folder
  * and returns the path to the new movemail folder that was created. which shoudl be freed later */
 gchar *mail_tool_do_movemail (const gchar *source_url, GError **error);
@@ -50,14 +39,6 @@ gchar *mail_tool_generate_forward_subject (CamelMimeMessage *msg);
 /* Make a message into an attachment */
 CamelMimePart *mail_tool_make_message_attachment (CamelMimeMessage *message);
 
-/* Parse the URI into a real CamelFolder any way we know how. */
-CamelFolder *	mail_tool_uri_to_folder		(const gchar *uri,
-						 guint32 flags,
-						 GCancellable *cancellable,
-						 GError **error);
-
-GHashTable *mail_lookup_url_table (CamelMimeMessage *mime_message);
-
 gchar *mail_tools_folder_to_url (CamelFolder *folder);
 
 #endif
diff --git a/mail/mail-vfolder.c b/mail/mail-vfolder.c
index cd9db55..0d8f127 100644
--- a/mail/mail-vfolder.c
+++ b/mail/mail-vfolder.c
@@ -33,6 +33,8 @@
 #include "e-util/e-util-private.h"
 #include "e-util/e-account-utils.h"
 
+#include "e-mail-backend.h"
+#include "e-mail-session.h"
 #include "em-folder-tree-model.h"
 #include "em-utils.h"
 #include "em-vfolder-context.h"
@@ -43,7 +45,6 @@
 #include "mail-folder-cache.h"
 #include "mail-mt.h"
 #include "mail-ops.h"
-#include "mail-session.h"
 #include "mail-tools.h"
 #include "mail-vfolder.h"
 
@@ -72,6 +73,7 @@ static void rule_changed (EFilterRule *rule, CamelFolder *folder);
 struct _setup_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	CamelFolder *folder;
 	gchar *query;
 	GList *sources_uri;
@@ -99,7 +101,8 @@ vfolder_setup_exec (struct _setup_msg *m)
 		d(printf(" Adding uri: %s\n", (gchar *)l->data));
 
 		/* FIXME Not passing a GCancellable or GError here. */
-		folder = mail_tool_uri_to_folder (l->data, 0, NULL, NULL);
+		folder = e_mail_session_uri_to_folder_sync (
+			m->session, l->data, 0, NULL, NULL);
 		if (folder != NULL)
 			list = g_list_append (list, folder);
 		l = l->next;
@@ -133,6 +136,7 @@ vfolder_setup_free (struct _setup_msg *m)
 {
 	GList *l;
 
+	g_object_unref (m->session);
 	g_object_unref (m->folder);
 	g_free (m->query);
 
@@ -161,14 +165,18 @@ static MailMsgInfo vfolder_setup_info = {
 
 /* sources_uri should be camel uri's */
 static gint
-vfolder_setup (CamelFolder *folder, const gchar *query, GList *sources_uri, GList *sources_folder)
+vfolder_setup (EMailSession *session,
+               CamelFolder *folder,
+               const gchar *query,
+               GList *sources_uri,
+               GList *sources_folder)
 {
 	struct _setup_msg *m;
 	gint id;
 
 	m = mail_msg_new (&vfolder_setup_info);
-	m->folder = folder;
-	g_object_ref (folder);
+	m->session = g_object_ref (session);
+	m->folder = g_object_ref (folder);
 	m->query = g_strdup (query);
 	m->sources_uri = sources_uri;
 	m->sources_folder = sources_folder;
@@ -184,6 +192,7 @@ vfolder_setup (CamelFolder *folder, const gchar *query, GList *sources_uri, GLis
 struct _adduri_msg {
 	MailMsg base;
 
+	EMailSession *session;
 	gchar *uri;
 	GList *folders;
 	gint remove;
@@ -255,8 +264,9 @@ vfolder_adduri_exec (struct _adduri_msg *m)
 	}
 
 	if (folder == NULL)
-		folder = mail_tool_uri_to_folder (
-			m->uri, 0, m->base.cancellable, &m->base.error);
+		folder = e_mail_session_uri_to_folder_sync (
+			m->session, m->uri, 0,
+			m->base.cancellable, &m->base.error);
 
 	if (folder != NULL) {
 		l = m->folders;
@@ -280,6 +290,7 @@ vfolder_adduri_done (struct _adduri_msg *m)
 static void
 vfolder_adduri_free (struct _adduri_msg *m)
 {
+	g_object_unref (m->session);
 	g_list_foreach (m->folders, (GFunc)g_object_unref, NULL);
 	g_list_free (m->folders);
 	g_free (m->uri);
@@ -295,12 +306,16 @@ static MailMsgInfo vfolder_adduri_info = {
 
 /* uri should be a camel uri */
 static gint
-vfolder_adduri (const gchar *uri, GList *folders, gint remove)
+vfolder_adduri (EMailSession *session,
+                const gchar *uri,
+                GList *folders,
+                gint remove)
 {
 	struct _adduri_msg *m;
 	gint id;
 
 	m = mail_msg_new (&vfolder_adduri_info);
+	m->session = g_object_ref (session);
 	m->folders = folders;
 	m->uri = g_strdup (uri);
 	m->remove = remove;
@@ -421,7 +436,7 @@ uri_is_spethal (CamelStore *store, const gchar *uri)
 
 /**
  * mail_vfolder_add_uri:
- *
+ * @session: an #EMailSession
  * @store: a #CamelStore containing the uri
  * @curi: an email uri to be added/removed
  * @remove: Whether the uri should be removed or added
@@ -437,7 +452,10 @@ uri_is_spethal (CamelStore *store, const gchar *uri)
  * NOTE: This function must be called from the main thread.
  */
 static void
-mail_vfolder_add_uri (CamelStore *store, const gchar *curi, gint remove)
+mail_vfolder_add_uri (EMailSession *session,
+                      CamelStore *store,
+                      const gchar *curi,
+                      gint remove)
 {
 	EFilterRule *rule;
 	const gchar *source;
@@ -530,13 +548,14 @@ done:
 	G_UNLOCK (vfolder);
 
 	if (folders != NULL)
-		vfolder_adduri (curi, folders, remove);
+		vfolder_adduri (session, curi, folders, remove);
 
 	g_free (uri);
 }
 
 /**
  * mail_vfolder_uri_available:
+ * @session: an #EMailSession
  * @store: a #CamelStore containing the uri
  * @uri: uri of a folder that became available
  *
@@ -545,13 +564,16 @@ done:
  * the vfolder filter rules on disk.
  */
 static void
-mail_vfolder_notify_uri_available (CamelStore *store, const gchar *uri)
+mail_vfolder_notify_uri_available (EMailSession *session,
+                                   CamelStore *store,
+                                   const gchar *uri)
 {
-	mail_vfolder_add_uri (store, uri, FALSE);
+	mail_vfolder_add_uri (session, store, uri, FALSE);
 }
 
 /**
  * mail_vfolder_uri_available:
+ * @session: an #EMailSession
  * @store: a #CamelStore containing the uri
  * @uri: uri of a folder that became unavailable
  *
@@ -560,9 +582,11 @@ mail_vfolder_notify_uri_available (CamelStore *store, const gchar *uri)
  * the vfolder filter rules on disk.
  */
 static void
-mail_vfolder_notify_uri_unavailable (CamelStore *store, const gchar *uri)
+mail_vfolder_notify_uri_unavailable (EMailSession *session,
+                                     CamelStore *store,
+                                     const gchar *uri)
 {
-	mail_vfolder_add_uri (store, uri, TRUE);
+	mail_vfolder_add_uri (session, store, uri, TRUE);
 }
 
 /**
@@ -806,6 +830,7 @@ rule_add_sources (GList *l, GList **sources_folderp, GList **sources_urip)
 static void
 rule_changed (EFilterRule *rule, CamelFolder *folder)
 {
+	EMailSession *session;
 	GList *sources_uri = NULL, *sources_folder = NULL;
 	GString *query;
 	const gchar *full_name;
@@ -853,7 +878,8 @@ rule_changed (EFilterRule *rule, CamelFolder *folder)
 	query = g_string_new("");
 	e_filter_rule_build_code (rule, query);
 
-	vfolder_setup (folder, query->str, sources_uri, sources_folder);
+	session = em_vfolder_rule_get_session (EM_VFOLDER_RULE (rule));
+	vfolder_setup (session, folder, query->str, sources_uri, sources_folder);
 
 	g_string_free (query, TRUE);
 }
@@ -992,15 +1018,21 @@ store_folder_renamed_cb (CamelStore *store,
 }
 
 static void
-folder_available_cb (MailFolderCache *cache, CamelStore *store, const gchar *uri, gpointer user_data)
+folder_available_cb (MailFolderCache *cache,
+                     CamelStore *store,
+                     const gchar *uri,
+                     EMailSession *session)
 {
-	mail_vfolder_notify_uri_available (store, uri);
+	mail_vfolder_notify_uri_available (session, store, uri);
 }
 
 static void
-folder_unavailable_cb (MailFolderCache *cache, CamelStore *store, const gchar *uri, gpointer user_data)
+folder_unavailable_cb (MailFolderCache *cache,
+                       CamelStore *store,
+                       const gchar *uri,
+                       EMailSession *session)
 {
-	mail_vfolder_notify_uri_unavailable (store, uri);
+	mail_vfolder_notify_uri_unavailable (session, store, uri);
 }
 
 static void
@@ -1016,7 +1048,7 @@ folder_renamed_cb (MailFolderCache *cache, CamelStore *store, const gchar *oldur
 }
 
 void
-vfolder_load_storage (void)
+vfolder_load_storage (EMailSession *session)
 {
 	/* lock for loading storage, it is safe to call it more than once */
 	G_LOCK_DEFINE_STATIC (vfolder_hash);
@@ -1045,7 +1077,8 @@ vfolder_load_storage (void)
 
 	/* first, create the vfolder store, and set it up */
 	storeuri = g_strdup_printf("vfolder:%s/vfolder", data_dir);
-	vfolder_store = camel_session_get_store (session, storeuri, NULL);
+	vfolder_store = camel_session_get_store (
+		CAMEL_SESSION (session), storeuri, NULL);
 	if (vfolder_store == NULL) {
 		g_warning("Cannot open vfolder store - no vfolders available");
 		return;
@@ -1063,7 +1096,7 @@ vfolder_load_storage (void)
 
 	/* load our rules */
 	user = g_build_filename (config_dir, "vfolders.xml", NULL);
-	context = em_vfolder_context_new ();
+	context = em_vfolder_context_new (session);
 
 	xmlfile = g_build_filename (EVOLUTION_PRIVDATADIR, "vfoldertypes.xml", NULL);
 	if (e_rule_context_load ((ERuleContext *)context,
@@ -1077,7 +1110,7 @@ vfolder_load_storage (void)
 	g_signal_connect(context, "rule_removed", G_CALLBACK(context_rule_removed), context);
 
 	/* load store to mail component */
-	e_mail_store_add_by_uri (storeuri, _("Search Folders"));
+	e_mail_store_add_by_uri (session, storeuri, _("Search Folders"));
 
 	/* and setup the rules we have */
 	rule = NULL;
@@ -1097,14 +1130,18 @@ vfolder_load_storage (void)
 	if (!gconf_client_get_bool (gconf, "/apps/evolution/mail/display/enable_vfolders", NULL))
 		gconf_client_set_bool (gconf, "/apps/evolution/mail/display/enable_vfolders", TRUE, NULL);
 
-	g_signal_connect (mail_folder_cache_get_default (), "folder-available",
-			  (GCallback) folder_available_cb, NULL);
-	g_signal_connect (mail_folder_cache_get_default (), "folder-unavailable",
-			  (GCallback) folder_unavailable_cb, NULL);
-	g_signal_connect (mail_folder_cache_get_default (), "folder-deleted",
-			  (GCallback) folder_deleted_cb, NULL);
-	g_signal_connect (mail_folder_cache_get_default (), "folder-renamed",
-			  (GCallback) folder_renamed_cb, NULL);
+	g_signal_connect (
+		mail_folder_cache_get_default (), "folder-available",
+		G_CALLBACK (folder_available_cb), session);
+	g_signal_connect (
+		mail_folder_cache_get_default (), "folder-unavailable",
+		G_CALLBACK (folder_unavailable_cb), session);
+	g_signal_connect (
+		mail_folder_cache_get_default (), "folder-deleted",
+		G_CALLBACK (folder_deleted_cb), NULL);
+	g_signal_connect (
+		mail_folder_cache_get_default (), "folder-renamed",
+		G_CALLBACK (folder_renamed_cb), NULL);
 }
 
 void
@@ -1125,6 +1162,7 @@ vfolder_edit (EShellView *shell_view)
 {
 	EShellBackend *shell_backend;
 	EShellWindow *shell_window;
+	EMailSession *session;
 	GtkWidget *dialog;
 	const gchar *config_dir;
 	gchar *filename;
@@ -1137,8 +1175,10 @@ vfolder_edit (EShellView *shell_view)
 	config_dir = e_shell_backend_get_config_dir (shell_backend);
 	filename = g_build_filename (config_dir, "vfolders.xml", NULL);
 
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	/* ensures vfolder is running */
-	vfolder_load_storage ();
+	vfolder_load_storage (session);
 
 	dialog = em_vfolder_editor_new (context);
 	gtk_window_set_title (
@@ -1276,9 +1316,13 @@ vfolder_create_part (const gchar *name)
 EFilterRule *
 vfolder_clone_rule (EFilterRule *in)
 {
-	EFilterRule *rule = (EFilterRule *)em_vfolder_rule_new ();
+	EMailSession *session;
+	EFilterRule *rule;
 	xmlNodePtr xml;
 
+	session = em_vfolder_rule_get_session (EM_VFOLDER_RULE (in));
+	rule = em_vfolder_rule_new (session);
+
 	xml = e_filter_rule_xml_encode (in);
 	e_filter_rule_xml_decode (rule, xml, (ERuleContext *)context);
 	xmlFreeNodeList (xml);
@@ -1290,12 +1334,15 @@ vfolder_clone_rule (EFilterRule *in)
 void
 vfolder_gui_add_rule (EMVFolderRule *rule)
 {
+	EMailSession *session;
 	GtkWidget *w;
 	GtkDialog *gd;
 	GtkWidget *container;
 
+	session = em_vfolder_rule_get_session (rule);
+
 	/* this should be done before we call this function */
-	vfolder_load_storage ();
+	vfolder_load_storage (session);
 
 	w = e_filter_rule_get_widget ((EFilterRule *)rule, (ERuleContext *)context);
 
@@ -1324,28 +1371,34 @@ vfolder_gui_add_rule (EMVFolderRule *rule)
 }
 
 void
-vfolder_gui_add_from_message (CamelMimeMessage *msg, gint flags, const gchar *source)
+vfolder_gui_add_from_message (EMailSession *session,
+                              CamelMimeMessage *msg,
+                              gint flags,
+                              const gchar *source)
 {
 	EMVFolderRule *rule;
 
 	g_return_if_fail (msg != NULL);
 
 	/* ensures vfolder is running */
-	vfolder_load_storage ();
+	vfolder_load_storage (session);
 
 	rule = (EMVFolderRule*)em_vfolder_rule_from_message (context, msg, flags, source);
 	vfolder_gui_add_rule (rule);
 }
 
 void
-vfolder_gui_add_from_address (CamelInternetAddress *addr, gint flags, const gchar *source)
+vfolder_gui_add_from_address (EMailSession *session,
+                              CamelInternetAddress *addr,
+                              gint flags,
+                              const gchar *source)
 {
 	EMVFolderRule *rule;
 
 	g_return_if_fail (addr != NULL);
 
 	/* ensures vfolder is running */
-	vfolder_load_storage ();
+	vfolder_load_storage (session);
 
 	rule = (EMVFolderRule*)em_vfolder_rule_from_address (context, addr, flags, source);
 	vfolder_gui_add_rule (rule);
diff --git a/mail/mail-vfolder.h b/mail/mail-vfolder.h
index 89610f4..187a68d 100644
--- a/mail/mail-vfolder.h
+++ b/mail/mail-vfolder.h
@@ -29,21 +29,26 @@
 #include <mail/em-vfolder-rule.h>
 #include <shell/e-shell-view.h>
 
-void vfolder_load_storage (void);
-void vfolder_revert (void);
-
-void vfolder_edit (EShellView *shell_view);
-void vfolder_edit_rule (const gchar *name);
-EFilterPart *vfolder_create_part (const gchar *name);
-EFilterRule *vfolder_clone_rule (EFilterRule *in);
-void vfolder_gui_add_rule (EMVFolderRule *rule);
-void vfolder_gui_add_from_message (CamelMimeMessage *msg, gint flags, const gchar *source);
-void vfolder_gui_add_from_address (CamelInternetAddress *addr, gint flags, const gchar *source);
-
-GList * mail_vfolder_get_sources_local (void);
-GList * mail_vfolder_get_sources_remote (void);
+void		vfolder_load_storage		(EMailSession *session);
+void		vfolder_revert			(void);
+void		vfolder_edit			(EShellView *shell_view);
+void		vfolder_edit_rule		(const gchar *name);
+EFilterPart *	vfolder_create_part		(const gchar *name);
+EFilterRule *	vfolder_clone_rule		(EFilterRule *in);
+void		vfolder_gui_add_rule		(EMVFolderRule *rule);
+void		vfolder_gui_add_from_message	(EMailSession *session,
+						 CamelMimeMessage *msg,
+						 gint flags,
+						 const gchar *source);
+void		vfolder_gui_add_from_address	(EMailSession *session,
+						 CamelInternetAddress *addr,
+						 gint flags,
+						 const gchar *source);
+
+GList *		mail_vfolder_get_sources_local	(void);
+GList *		mail_vfolder_get_sources_remote	(void);
 
 /* close up, clean up */
-void mail_vfolder_shutdown (void);
+void		mail_vfolder_shutdown		(void);
 
 #endif
diff --git a/mail/message-list.c b/mail/message-list.c
index 681cc53..5a9af96 100644
--- a/mail/message-list.c
+++ b/mail/message-list.c
@@ -112,7 +112,7 @@ struct _MLSelection {
 struct _MessageListPrivate {
 	GtkWidget *invisible;	/* 4 selection */
 
-	EShellBackend *shell_backend;
+	EMailBackend *backend;
 
 	struct _MLSelection clipboard;
 	gboolean destroyed;
@@ -137,9 +137,9 @@ struct _MessageListPrivate {
 
 enum {
 	PROP_0,
+	PROP_BACKEND,
 	PROP_COPY_TARGET_LIST,
-	PROP_PASTE_TARGET_LIST,
-	PROP_SHELL_BACKEND
+	PROP_PASTE_TARGET_LIST
 };
 
 static gpointer parent_class;
@@ -1403,17 +1403,16 @@ add_all_labels_foreach (ETreeModel *etm, ETreePath node, gpointer data)
 static EMailLabelListStore *
 ml_get_label_list_store (MessageList *message_list)
 {
-	EShellBackend *shell_backend;
 	EShell *shell;
 	EShellSettings *shell_settings;
-	EMailLabelListStore *store;
+	EMailBackend *backend;
 
-	shell_backend = message_list_get_shell_backend (message_list);
-	shell = e_shell_backend_get_shell (shell_backend);
+	backend = message_list_get_backend (message_list);
+	shell = e_shell_backend_get_shell (E_SHELL_BACKEND (backend));
 	shell_settings = e_shell_get_shell_settings (shell);
-	store = e_shell_settings_get_object (shell_settings, "mail-label-list-store");
 
-	return store;
+	return e_shell_settings_get_object (
+		shell_settings, "mail-label-list-store");
 }
 
 static const gchar *
@@ -2100,8 +2099,10 @@ static void
 ml_selection_received (GtkWidget *widget,
                        GtkSelectionData *selection_data,
                        guint time,
-                       MessageList *ml)
+                       MessageList *message_list)
 {
+	EMailBackend *backend;
+	EMailSession *session;
 	GdkAtom target;
 
 	target = gtk_selection_data_get_target (selection_data);
@@ -2111,9 +2112,13 @@ ml_selection_received (GtkWidget *widget,
 		return;
 	}
 
+	backend = message_list_get_backend (message_list);
+	session = e_mail_backend_get_session (backend);
+
 	/* FIXME Not passing a GCancellable or GError here. */
 	em_utils_selection_get_uidlist (
-		selection_data, ml->folder, FALSE, NULL, NULL);
+		selection_data, session, message_list->folder,
+		FALSE, NULL, NULL);
 }
 
 static void
@@ -2150,6 +2155,7 @@ struct _drop_msg {
 	GtkSelectionData *selection;
 
 	CamelFolder *folder;
+	MessageList *message_list;
 
 	guint32 action;
 	guint info;
@@ -2175,10 +2181,16 @@ ml_drop_async_desc (struct _drop_msg *m)
 static void
 ml_drop_async_exec (struct _drop_msg *m)
 {
+	EMailBackend *backend;
+	EMailSession *session;
+
+	backend = message_list_get_backend (m->message_list);
+	session = e_mail_backend_get_session (backend);
+
 	switch (m->info) {
 	case DND_X_UID_LIST:
 		em_utils_selection_get_uidlist (
-			m->selection, m->folder,
+			m->selection, session, m->folder,
 			m->action == GDK_ACTION_MOVE,
 			m->base.cancellable,
 			&m->base.error);
@@ -2214,6 +2226,7 @@ ml_drop_async_free (struct _drop_msg *m)
 {
 	g_object_unref (m->context);
 	g_object_unref (m->folder);
+	g_object_unref (m->message_list);
 	gtk_selection_data_free (m->selection);
 }
 
@@ -2257,10 +2270,9 @@ ml_tree_drag_data_received (ETree *tree,
 		return;
 
 	m = mail_msg_new (&ml_drop_async_info);
-	m->context = context;
-	g_object_ref (context);
-	m->folder = ml->folder;
-	g_object_ref (m->folder);
+	m->context = g_object_ref (context);
+	m->folder = g_object_ref (ml->folder);
+	m->message_list = g_object_ref (ml);
 	m->action = gdk_drag_context_get_selected_action (context);
 	m->info = info;
 
@@ -2368,12 +2380,13 @@ ml_tree_sorting_changed (ETreeTableAdapter *adapter, MessageList *ml)
  */
 
 static void
-message_list_set_shell_backend (MessageList *message_list,
-                               EShellBackend *shell_backend)
+message_list_set_backend (MessageList *message_list,
+                          EMailBackend *backend)
 {
-	g_return_if_fail (message_list->priv->shell_backend == NULL);
+	g_return_if_fail (E_IS_MAIL_BACKEND (backend));
+	g_return_if_fail (message_list->priv->backend == NULL);
 
-	message_list->priv->shell_backend = g_object_ref (shell_backend);
+	message_list->priv->backend = g_object_ref (backend);
 }
 
 static void
@@ -2436,8 +2449,8 @@ message_list_set_property (GObject *object,
                            GParamSpec *pspec)
 {
 	switch (property_id) {
-		case PROP_SHELL_BACKEND:
-			message_list_set_shell_backend (
+		case PROP_BACKEND:
+			message_list_set_backend (
 				MESSAGE_LIST (object),
 				g_value_get_object (value));
 			return;
@@ -2453,6 +2466,12 @@ message_list_get_property (GObject *object,
                            GParamSpec *pspec)
 {
 	switch (property_id) {
+		case PROP_BACKEND:
+			g_value_set_object (
+				value, message_list_get_backend (
+				MESSAGE_LIST (object)));
+			return;
+
 		case PROP_COPY_TARGET_LIST:
 			g_value_set_boxed (
 				value, message_list_get_copy_target_list (
@@ -2464,12 +2483,6 @@ message_list_get_property (GObject *object,
 				value, message_list_get_paste_target_list (
 				MESSAGE_LIST (object)));
 			return;
-
-		case PROP_SHELL_BACKEND:
-			g_value_set_object (
-				value, message_list_get_shell_backend (
-				MESSAGE_LIST (object)));
-			return;
 	}
 
 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -2483,9 +2496,9 @@ message_list_dispose (GObject *object)
 
 	priv = MESSAGE_LIST_GET_PRIVATE (message_list);
 
-	if (priv->shell_backend != NULL) {
-		g_object_unref (priv->shell_backend);
-		priv->shell_backend = NULL;
+	if (priv->backend != NULL) {
+		g_object_unref (priv->backend);
+		priv->backend = NULL;
 	}
 
 	if (priv->copy_target_list != NULL) {
@@ -2617,6 +2630,17 @@ message_list_class_init (MessageListClass *class)
 
 	class->message_list_built = NULL;
 
+	g_object_class_install_property (
+		object_class,
+		PROP_BACKEND,
+		g_param_spec_object (
+			"backend",
+			"Mail Backend",
+			"The mail backend",
+			E_TYPE_MAIL_BACKEND,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
+
 	/* Inherited from ESelectableInterface */
 	g_object_class_override_property (
 		object_class,
@@ -2629,17 +2653,6 @@ message_list_class_init (MessageListClass *class)
 		PROP_PASTE_TARGET_LIST,
 		"paste-target-list");
 
-	g_object_class_install_property (
-		object_class,
-		PROP_SHELL_BACKEND,
-		g_param_spec_object (
-			"shell-backend",
-			"Shell Backend",
-			"The mail shell backend",
-			E_TYPE_SHELL_BACKEND,
-			G_PARAM_READWRITE |
-			G_PARAM_CONSTRUCT_ONLY));
-
 	message_list_signals[MESSAGE_SELECTED] =
 		g_signal_new ("message_selected",
 			      MESSAGE_LIST_TYPE,
@@ -2833,27 +2846,27 @@ message_list_get_type (void)
  * Returns a new message-list widget.
  **/
 GtkWidget *
-message_list_new (EShellBackend *shell_backend)
+message_list_new (EMailBackend *backend)
 {
 	GtkWidget *message_list;
 
-	g_return_val_if_fail (E_IS_SHELL_BACKEND (shell_backend), NULL);
+	g_return_val_if_fail (E_IS_MAIL_BACKEND (backend), NULL);
 
 	message_list = g_object_new (
 		message_list_get_type (),
-		"shell-backend", shell_backend, NULL);
+		"backend", backend, NULL);
 
 	message_list_construct (MESSAGE_LIST (message_list));
 
 	return message_list;
 }
 
-EShellBackend *
-message_list_get_shell_backend (MessageList *message_list)
+EMailBackend *
+message_list_get_backend (MessageList *message_list)
 {
 	g_return_val_if_fail (IS_MESSAGE_LIST (message_list), NULL);
 
-	return message_list->priv->shell_backend;
+	return message_list->priv->backend;
 }
 
 static void
diff --git a/mail/message-list.h b/mail/message-list.h
index f8ac551..3e455a6 100644
--- a/mail/message-list.h
+++ b/mail/message-list.h
@@ -27,7 +27,7 @@
 #include <camel/camel.h>
 
 #include <table/e-tree.h>
-#include <shell/e-shell-backend.h>
+#include <mail/e-mail-backend.h>
 
 /* Standard GObject macros */
 #define MESSAGE_LIST_TYPE \
@@ -178,8 +178,8 @@ typedef enum {
 } MessageListSelectDirection;
 
 GType		message_list_get_type		(void);
-GtkWidget *	message_list_new		(EShellBackend *shell_backend);
-EShellBackend *	message_list_get_shell_backend	(MessageList *message_list);
+GtkWidget *	message_list_new		(EMailBackend *backend);
+EMailBackend *	message_list_get_backend	(MessageList *message_list);
 void		message_list_set_folder		(MessageList *message_list,
 						 CamelFolder *camel_folder,
 						 const gchar *uri,
diff --git a/modules/mail/e-mail-attachment-handler.c b/modules/mail/e-mail-attachment-handler.c
index 55ffc80..2b74b2a 100644
--- a/modules/mail/e-mail-attachment-handler.c
+++ b/modules/mail/e-mail-attachment-handler.c
@@ -24,15 +24,16 @@
 #include <glib/gi18n.h>
 
 #include "e-util/e-alert-dialog.h"
+#include "mail/e-mail-backend.h"
 #include "mail/em-composer-utils.h"
-#include "mail/mail-tools.h"
 
 #define E_MAIL_ATTACHMENT_HANDLER_GET_PRIVATE(obj) \
 	(G_TYPE_INSTANCE_GET_PRIVATE \
 	((obj), E_TYPE_MAIL_ATTACHMENT_HANDLER, EMailAttachmentHandlerPrivate))
 
 struct _EMailAttachmentHandlerPrivate {
-	gint placeholder;
+	EShell *shell;
+	EMailSession *session;
 };
 
 static gpointer parent_class;
@@ -57,16 +58,17 @@ static GtkTargetEntry target_table[] = {
 
 static void
 mail_attachment_handler_forward (GtkAction *action,
-                                 EAttachmentView *view)
+                                 EAttachmentHandler *handler)
 {
-	EShell *shell;
+	EMailAttachmentHandlerPrivate *priv;
 	EAttachment *attachment;
+	EAttachmentView *view;
 	CamelMimePart *mime_part;
 	CamelDataWrapper *wrapper;
 	GList *selected;
 
-	/* FIXME Pass this in somehow. */
-	shell = e_shell_get_default ();
+	view = e_attachment_handler_get_view (handler);
+	priv = E_MAIL_ATTACHMENT_HANDLER_GET_PRIVATE (handler);
 
 	selected = e_attachment_view_get_selected_attachments (view);
 	g_return_if_fail (g_list_length (selected) == 1);
@@ -75,7 +77,8 @@ mail_attachment_handler_forward (GtkAction *action,
 	mime_part = e_attachment_get_mime_part (attachment);
 	wrapper = camel_medium_get_content (CAMEL_MEDIUM (mime_part));
 
-	em_utils_forward_message (shell, CAMEL_MIME_MESSAGE (wrapper), NULL);
+	em_utils_forward_message (
+		priv->shell, CAMEL_MIME_MESSAGE (wrapper), NULL);
 
 	g_list_foreach (selected, (GFunc) g_object_unref, NULL);
 	g_list_free (selected);
@@ -83,16 +86,17 @@ mail_attachment_handler_forward (GtkAction *action,
 
 static void
 mail_attachment_handler_reply_all (GtkAction *action,
-                                   EAttachmentView *view)
+                                   EAttachmentHandler *handler)
 {
-	EShell *shell;
+	EMailAttachmentHandlerPrivate *priv;
 	EAttachment *attachment;
+	EAttachmentView *view;
 	CamelMimePart *mime_part;
 	CamelDataWrapper *wrapper;
 	GList *selected;
 
-	/* FIXME Pass this in somehow. */
-	shell = e_shell_get_default ();
+	view = e_attachment_handler_get_view (handler);
+	priv = E_MAIL_ATTACHMENT_HANDLER_GET_PRIVATE (handler);
 
 	selected = e_attachment_view_get_selected_attachments (view);
 	g_return_if_fail (g_list_length (selected) == 1);
@@ -102,7 +106,7 @@ mail_attachment_handler_reply_all (GtkAction *action,
 	wrapper = camel_medium_get_content (CAMEL_MEDIUM (mime_part));
 
 	em_utils_reply_to_message (
-		shell, NULL, NULL, CAMEL_MIME_MESSAGE (wrapper),
+		priv->shell, NULL, NULL, CAMEL_MIME_MESSAGE (wrapper),
 		REPLY_MODE_ALL, NULL);
 
 	g_list_foreach (selected, (GFunc) g_object_unref, NULL);
@@ -111,16 +115,17 @@ mail_attachment_handler_reply_all (GtkAction *action,
 
 static void
 mail_attachment_handler_reply_sender (GtkAction *action,
-                                      EAttachmentView *view)
+                                      EAttachmentHandler *handler)
 {
-	EShell *shell;
+	EMailAttachmentHandlerPrivate *priv;
 	EAttachment *attachment;
+	EAttachmentView *view;
 	CamelMimePart *mime_part;
 	CamelDataWrapper *wrapper;
 	GList *selected;
 
-	/* FIXME Pass this in somehow. */
-	shell = e_shell_get_default ();
+	view = e_attachment_handler_get_view (handler);
+	priv = E_MAIL_ATTACHMENT_HANDLER_GET_PRIVATE (handler);
 
 	selected = e_attachment_view_get_selected_attachments (view);
 	g_return_if_fail (g_list_length (selected) == 1);
@@ -130,7 +135,7 @@ mail_attachment_handler_reply_sender (GtkAction *action,
 	wrapper = camel_medium_get_content (CAMEL_MEDIUM (mime_part));
 
 	em_utils_reply_to_message (
-		shell, NULL, NULL, CAMEL_MIME_MESSAGE (wrapper),
+		priv->shell, NULL, NULL, CAMEL_MIME_MESSAGE (wrapper),
 		REPLY_MODE_SENDER, NULL);
 
 	g_list_foreach (selected, (GFunc) g_object_unref, NULL);
@@ -168,7 +173,8 @@ mail_attachment_handler_message_rfc822 (EAttachmentView *view,
                                         gint y,
                                         GtkSelectionData *selection_data,
                                         guint info,
-                                        guint time)
+                                        guint time,
+                                        EAttachmentHandler *handler)
 {
 	static GdkAtom atom = GDK_NONE;
 	EAttachmentStore *store;
@@ -231,9 +237,11 @@ mail_attachment_handler_x_uid_list (EAttachmentView *view,
                                     gint y,
                                     GtkSelectionData *selection_data,
                                     guint info,
-                                    guint time)
+                                    guint time,
+                                    EAttachmentHandler *handler)
 {
 	static GdkAtom atom = GDK_NONE;
+	EMailAttachmentHandlerPrivate *priv;
 	CamelDataWrapper *wrapper;
 	CamelMimeMessage *message;
 	CamelMultipart *multipart;
@@ -257,6 +265,7 @@ mail_attachment_handler_x_uid_list (EAttachmentView *view,
 		return;
 
 	store = e_attachment_view_get_store (view);
+	priv = E_MAIL_ATTACHMENT_HANDLER_GET_PRIVATE (handler);
 
 	parent = gtk_widget_get_toplevel (GTK_WIDGET (view));
 	parent = gtk_widget_is_toplevel (parent) ? parent : NULL;
@@ -290,7 +299,8 @@ mail_attachment_handler_x_uid_list (EAttachmentView *view,
 
 	/* The first string is the folder URI. */
 	/* FIXME Not passing a GCancellable here. */
-	folder = mail_tool_uri_to_folder (data, 0, NULL, &local_error);
+	folder = e_mail_session_uri_to_folder_sync (
+		priv->session, data, 0, NULL, &local_error);
 	if (folder == NULL)
 		goto exit;
 
@@ -384,7 +394,8 @@ exit:
 }
 
 static void
-mail_attachment_handler_update_actions (EAttachmentView *view)
+mail_attachment_handler_update_actions (EAttachmentView *view,
+                                        EAttachmentHandler *handler)
 {
 	EAttachment *attachment;
 	CamelMimePart *mime_part;
@@ -417,25 +428,58 @@ exit:
 }
 
 static void
+mail_attachment_handler_dispose (GObject *object)
+{
+	EMailAttachmentHandlerPrivate *priv;
+
+	priv = E_MAIL_ATTACHMENT_HANDLER_GET_PRIVATE (object);
+
+	if (priv->shell != NULL) {
+		g_object_unref (priv->shell);
+		priv->shell = NULL;
+	}
+
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
+	/* Chain up to parent's dispose() method. */
+	G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
 mail_attachment_handler_constructed (GObject *object)
 {
+	EMailAttachmentHandlerPrivate *priv;
+	EShell *shell;
+	EShellBackend *shell_backend;
 	EAttachmentHandler *handler;
 	EAttachmentView *view;
+	EMailSession *session;
 	GtkActionGroup *action_group;
 	GtkUIManager *ui_manager;
 	GError *error = NULL;
 
 	handler = E_ATTACHMENT_HANDLER (object);
+	priv = E_MAIL_ATTACHMENT_HANDLER_GET_PRIVATE (object);
 
 	/* Chain up to parent's constructed() method. */
 	G_OBJECT_CLASS (parent_class)->constructed (object);
 
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
+	priv->shell = g_object_ref (shell);
+	priv->session = g_object_ref (session);
+
 	view = e_attachment_handler_get_view (handler);
 
 	action_group = e_attachment_view_add_action_group (view, "mail");
 	gtk_action_group_add_actions (
 		action_group, standard_entries,
-		G_N_ELEMENTS (standard_entries), view);
+		G_N_ELEMENTS (standard_entries), handler);
 
 	ui_manager = e_attachment_view_get_ui_manager (view);
 	gtk_ui_manager_add_ui_from_string (ui_manager, ui, -1, &error);
@@ -448,17 +492,17 @@ mail_attachment_handler_constructed (GObject *object)
 	g_signal_connect (
 		view, "update-actions",
 		G_CALLBACK (mail_attachment_handler_update_actions),
-		NULL);
+		handler);
 
 	g_signal_connect (
 		view, "drag-data-received",
 		G_CALLBACK (mail_attachment_handler_message_rfc822),
-		NULL);
+		handler);
 
 	g_signal_connect (
 		view, "drag-data-received",
 		G_CALLBACK (mail_attachment_handler_x_uid_list),
-		NULL);
+		handler);
 }
 
 static GdkDragAction
@@ -487,6 +531,7 @@ mail_attachment_handler_class_init (EMailAttachmentHandlerClass *class)
 	g_type_class_add_private (class, sizeof (EMailAttachmentHandlerPrivate));
 
 	object_class = G_OBJECT_CLASS (class);
+	object_class->dispose = mail_attachment_handler_dispose;
 	object_class->constructed = mail_attachment_handler_constructed;
 
 	handler_class = E_ATTACHMENT_HANDLER_CLASS (class);
diff --git a/modules/mail/e-mail-junk-hook.c b/modules/mail/e-mail-junk-hook.c
index ae87df1..ac88f1e 100644
--- a/modules/mail/e-mail-junk-hook.c
+++ b/modules/mail/e-mail-junk-hook.c
@@ -28,7 +28,7 @@
 
 #include "mail/em-junk.h"
 #include "mail/em-utils.h"
-#include "mail/mail-session.h"
+#include "mail/e-mail-session.h"
 
 #define E_MAIL_JUNK_HOOK_GET_PRIVATE(obj) \
 	(G_TYPE_INSTANCE_GET_PRIVATE \
@@ -209,6 +209,10 @@ mail_junk_hook_construct (EPluginHook *hook,
                           xmlNodePtr node)
 {
 	EMailJunkHookPrivate *priv;
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailBackend *backend;
+	EMailSession *session;
 	gchar *property;
 
 	priv = E_MAIL_JUNK_HOOK_GET_PRIVATE (hook);
@@ -258,8 +262,15 @@ mail_junk_hook_construct (EPluginHook *hook,
 	if (priv->interface.commit_reports == NULL)
 		return -1;
 
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	mail_session_add_junk_plugin (
-		priv->interface.plugin_name, &priv->interface.camel);
+		session, priv->interface.plugin_name,
+		&priv->interface.camel);
 
 	return 0;
 }
diff --git a/modules/mail/e-mail-shell-backend.c b/modules/mail/e-mail-shell-backend.c
index 24d3eca..02b073a 100644
--- a/modules/mail/e-mail-shell-backend.c
+++ b/modules/mail/e-mail-shell-backend.c
@@ -38,6 +38,7 @@
 
 #include "e-mail-browser.h"
 #include "e-mail-reader.h"
+#include "e-mail-session.h"
 #include "e-mail-store.h"
 #include "em-account-editor.h"
 #include "em-account-prefs.h"
@@ -52,7 +53,6 @@
 #include "mail-config.h"
 #include "mail-ops.h"
 #include "mail-send-recv.h"
-#include "mail-session.h"
 #include "mail-vfolder.h"
 #include "importers/mail-importer.h"
 
@@ -237,12 +237,14 @@ mail_shell_backend_handle_email_uri_cb (gchar *folder_uri,
                                         gpointer user_data)
 {
 	EShellBackend *shell_backend = user_data;
+	EMailBackend *backend;
 	CamelURL *url = user_data;
 	EShell *shell;
 	const gchar *forward;
 	const gchar *reply;
 	const gchar *uid;
 
+	backend = E_MAIL_BACKEND (shell_backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 
 	if (folder == NULL) {
@@ -290,7 +292,7 @@ mail_shell_backend_handle_email_uri_cb (gchar *folder_uri,
 		GtkWidget *browser;
 
 		/* FIXME Should pass in the shell module. */
-		browser = e_mail_browser_new (shell_backend);
+		browser = e_mail_browser_new (backend);
 		e_mail_reader_set_folder (
 			E_MAIL_READER (browser), folder, folder_uri);
 		e_mail_reader_set_message (E_MAIL_READER (browser), uid);
@@ -306,8 +308,13 @@ mail_shell_backend_handle_uri_cb (EShell *shell,
                                   const gchar *uri,
                                   EMailShellBackend *mail_shell_backend)
 {
+	EMailBackend *backend;
+	EMailSession *session;
 	gboolean handled = TRUE;
 
+	backend = E_MAIL_BACKEND (mail_shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	if (g_str_has_prefix (uri, "mailto:";)) {
 		if (em_utils_check_user_can_send_mail ())
 			em_utils_compose_new_message_with_mailto (
@@ -321,7 +328,7 @@ mail_shell_backend_handle_uri_cb (EShell *shell,
 			gchar *curi = em_uri_to_camel (uri);
 
 			mail_get_folder (
-				curi, 0,
+				session, curi, 0,
 				mail_shell_backend_handle_email_uri_cb,
 				mail_shell_backend, mail_msg_unordered_push);
 			g_free (curi);
@@ -357,8 +364,14 @@ mail_shell_backend_send_receive_cb (EShell *shell,
                                     GtkWindow *parent,
                                     EShellBackend *shell_backend)
 {
+	EMailBackend *backend;
+	EMailSession *session;
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	em_utils_clear_get_password_canceled_accounts_flag ();
-	mail_send_receive (parent);
+	mail_send_receive (parent, session);
 }
 
 static void
@@ -469,7 +482,7 @@ mail_shell_backend_constructed (GObject *object)
 		G_CALLBACK (mail_shell_backend_window_created_cb),
 		shell_backend);
 
-	e_mail_shell_settings_init (shell);
+	e_mail_shell_settings_init (shell_backend);
 
 	/* Setup preference widget factories */
 	preferences_window = e_shell_get_preferences_window (shell);
@@ -513,6 +526,8 @@ mail_shell_backend_start (EShellBackend *shell_backend)
 	EMailShellBackendPrivate *priv;
 	EShell *shell;
 	EShellSettings *shell_settings;
+	EMailBackend *backend;
+	EMailSession *session;
 	gboolean enable_search_folders;
 
 	priv = E_MAIL_SHELL_BACKEND_GET_PRIVATE (shell_backend);
@@ -520,12 +535,15 @@ mail_shell_backend_start (EShellBackend *shell_backend)
 	shell = e_shell_backend_get_shell (shell_backend);
 	shell_settings = e_shell_get_shell_settings (shell);
 
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	enable_search_folders = e_shell_settings_get_boolean (
 		shell_settings, "mail-enable-search-folders");
 	if (enable_search_folders)
-		vfolder_load_storage ();
+		vfolder_load_storage (session);
 
-	mail_autoreceive_init (shell_backend, session);
+	mail_autoreceive_init (backend);
 
 	if (g_getenv ("CAMEL_FLUSH_CHANGES") != NULL)
 		priv->mail_sync_source_id = g_timeout_add_seconds (
diff --git a/modules/mail/e-mail-shell-content.c b/modules/mail/e-mail-shell-content.c
index 6ba5273..fd1db02 100644
--- a/modules/mail/e-mail-shell-content.c
+++ b/modules/mail/e-mail-shell-content.c
@@ -244,6 +244,20 @@ mail_shell_content_get_action_group (EMailReader *reader)
 	return E_SHELL_WINDOW_ACTION_GROUP_MAIL (shell_window);
 }
 
+static EMailBackend *
+mail_shell_content_get_backend (EMailReader *reader)
+{
+	EMailShellContentPrivate *priv;
+
+	priv = E_MAIL_SHELL_CONTENT_GET_PRIVATE (reader);
+
+	/* Forward this to our internal EMailView, which
+	 * also implements the EMailReader interface. */
+	reader = E_MAIL_READER (priv->mail_view);
+
+	return e_mail_reader_get_backend (reader);
+}
+
 static EMFormatHTML *
 mail_shell_content_get_formatter (EMailReader *reader)
 {
@@ -300,20 +314,6 @@ mail_shell_content_get_popup_menu (EMailReader *reader)
 	return e_mail_reader_get_popup_menu (reader);
 }
 
-static EShellBackend *
-mail_shell_content_get_shell_backend (EMailReader *reader)
-{
-	EMailShellContentPrivate *priv;
-
-	priv = E_MAIL_SHELL_CONTENT_GET_PRIVATE (reader);
-
-	/* Forward this to our internal EMailView, which
-	 * also implements the EMailReader interface. */
-	reader = E_MAIL_READER (priv->mail_view);
-
-	return e_mail_reader_get_shell_backend (reader);
-}
-
 static GtkWindow *
 mail_shell_content_get_window (EMailReader *reader)
 {
@@ -412,11 +412,11 @@ static void
 mail_shell_content_reader_init (EMailReaderInterface *interface)
 {
 	interface->get_action_group = mail_shell_content_get_action_group;
+	interface->get_backend = mail_shell_content_get_backend;
 	interface->get_formatter = mail_shell_content_get_formatter;
 	interface->get_hide_deleted = mail_shell_content_get_hide_deleted;
 	interface->get_message_list = mail_shell_content_get_message_list;
 	interface->get_popup_menu = mail_shell_content_get_popup_menu;
-	interface->get_shell_backend = mail_shell_content_get_shell_backend;
 	interface->get_window = mail_shell_content_get_window;
 	interface->set_folder = mail_shell_content_set_folder;
 	interface->show_search_bar = mail_shell_content_show_search_bar;
diff --git a/modules/mail/e-mail-shell-settings.c b/modules/mail/e-mail-shell-settings.c
index 690a0ef..5452a19 100644
--- a/modules/mail/e-mail-shell-settings.c
+++ b/modules/mail/e-mail-shell-settings.c
@@ -24,18 +24,28 @@
 #include <gconf/gconf-client.h>
 #include <libedataserver/e-account-list.h>
 
-#include "e-util/e-signature-list.h"
-#include "mail/e-mail-label-list-store.h"
-#include "mail/mail-session.h"
+#include <e-util/e-signature-list.h>
+
+#include <mail/e-mail-backend.h>
+#include <mail/e-mail-label-list-store.h>
+
+#include <shell/e-shell.h>
 
 void
-e_mail_shell_settings_init (EShell *shell)
+e_mail_shell_settings_init (EShellBackend *shell_backend)
 {
+	EShell *shell;
 	EShellSettings *shell_settings;
+	EMailBackend *backend;
+	EMailSession *session;
 	gpointer object;
 
+	shell = e_shell_backend_get_shell (shell_backend);
 	shell_settings = e_shell_get_shell_settings (shell);
 
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	/*** Global Objects ***/
 
 	e_shell_settings_install_property (
@@ -58,9 +68,9 @@ e_mail_shell_settings_init (EShell *shell)
 			NULL,
 			G_PARAM_READWRITE));
 
-	g_object_ref (session);
 	e_shell_settings_set_pointer (
-		shell_settings, "mail-session", session);
+		shell_settings, "mail-session",
+		g_object_ref (session));
 
 	/*** Mail Preferences ***/
 
diff --git a/modules/mail/e-mail-shell-settings.h b/modules/mail/e-mail-shell-settings.h
index 4267fd8..1faf6c6 100644
--- a/modules/mail/e-mail-shell-settings.h
+++ b/modules/mail/e-mail-shell-settings.h
@@ -22,11 +22,11 @@
 #ifndef E_MAIL_SHELL_SETTINGS_H
 #define E_MAIL_SHELL_SETTINGS_H
 
-#include <shell/e-shell.h>
+#include <shell/e-shell-backend.h>
 
 G_BEGIN_DECLS
 
-void		e_mail_shell_settings_init	(EShell *shell);
+void		e_mail_shell_settings_init	(EShellBackend *shell_backend);
 
 G_END_DECLS
 
diff --git a/modules/mail/e-mail-shell-sidebar.c b/modules/mail/e-mail-shell-sidebar.c
index ac736a0..bc1cbcd 100644
--- a/modules/mail/e-mail-shell-sidebar.c
+++ b/modules/mail/e-mail-shell-sidebar.c
@@ -23,6 +23,7 @@
 
 #include "e-util/e-binding.h"
 
+#include "mail/e-mail-backend.h"
 #include "mail/e-mail-sidebar.h"
 #include "mail/em-folder-utils.h"
 
@@ -116,10 +117,13 @@ mail_shell_sidebar_constructed (GObject *object)
 {
 	EMailShellSidebar *mail_shell_sidebar;
 	EShellSettings *shell_settings;
+	EShellBackend *shell_backend;
 	EShellSidebar *shell_sidebar;
 	EShellWindow *shell_window;
 	EShellView *shell_view;
 	EShell *shell;
+	EMailBackend *backend;
+	EMailSession *session;
 	GtkTreeSelection *selection;
 	GtkTreeView *tree_view;
 	GtkWidget *container;
@@ -130,11 +134,15 @@ mail_shell_sidebar_constructed (GObject *object)
 
 	shell_sidebar = E_SHELL_SIDEBAR (object);
 	shell_view = e_shell_sidebar_get_shell_view (shell_sidebar);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
 	shell_window = e_shell_view_get_shell_window (shell_view);
 
 	shell = e_shell_window_get_shell (shell_window);
 	shell_settings = e_shell_get_shell_settings (shell);
 
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	mail_shell_sidebar = E_MAIL_SHELL_SIDEBAR (object);
 
 	/* Build sidebar widgets. */
@@ -152,7 +160,7 @@ mail_shell_sidebar_constructed (GObject *object)
 
 	container = widget;
 
-	widget = e_mail_sidebar_new ();
+	widget = e_mail_sidebar_new (session);
 	gtk_container_add (GTK_CONTAINER (container), widget);
 	mail_shell_sidebar->priv->folder_tree = g_object_ref (widget);
 	gtk_widget_show (widget);
diff --git a/modules/mail/e-mail-shell-view-actions.c b/modules/mail/e-mail-shell-view-actions.c
index 4b4a1d8..852edbd 100644
--- a/modules/mail/e-mail-shell-view-actions.c
+++ b/modules/mail/e-mail-shell-view-actions.c
@@ -50,6 +50,10 @@ action_mail_account_disable_cb (GtkAction *action,
                                 EMailShellView *mail_shell_view)
 {
 	EMailShellSidebar *mail_shell_sidebar;
+	EShellBackend *shell_backend;
+	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
 	EMFolderTree *folder_tree;
 	EAccountList *account_list;
 	EAccount *account;
@@ -57,6 +61,12 @@ action_mail_account_disable_cb (GtkAction *action,
 
 	mail_shell_sidebar = mail_shell_view->priv->mail_shell_sidebar;
 
+	shell_view = E_SHELL_VIEW (mail_shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	folder_tree = e_mail_shell_sidebar_get_folder_tree (mail_shell_sidebar);
 	folder_uri = em_folder_tree_get_selected_uri (folder_tree);
 	g_return_if_fail (folder_uri != NULL);
@@ -70,7 +80,7 @@ action_mail_account_disable_cb (GtkAction *action,
 
 	account->enabled = !account->enabled;
 	e_account_list_change (account_list, account);
-	e_mail_store_remove_by_uri (folder_uri);
+	e_mail_store_remove_by_uri (session, folder_uri);
 
 	if (account->parent_uid != NULL)
 		e_account_list_remove (account_list, account);
@@ -87,24 +97,32 @@ action_mail_create_search_folder_cb (GtkAction *action,
 	EMailShellContent *mail_shell_content;
 	EMailReader *reader;
 	EShellView *shell_view;
+	EShellBackend *shell_backend;
 	EShellSearchbar *searchbar;
 	EFilterRule *search_rule;
 	EMVFolderRule *vfolder_rule;
+	EMailBackend *backend;
+	EMailSession *session;
 	EMailView *mail_view;
 	const gchar *folder_uri;
 	const gchar *search_text;
 	gchar *rule_name;
 
-	vfolder_load_storage ();
-
 	shell_view = E_SHELL_VIEW (mail_shell_view);
-	search_rule = e_shell_view_get_search_rule (shell_view);
-	g_return_if_fail (search_rule != NULL);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
 
 	mail_shell_content = mail_shell_view->priv->mail_shell_content;
 	mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);
 	searchbar = e_mail_shell_content_get_searchbar (mail_shell_content);
 
+	vfolder_load_storage (session);
+
+	search_rule = e_shell_view_get_search_rule (shell_view);
+	g_return_if_fail (search_rule != NULL);
+
 	search_text = e_shell_searchbar_get_search_text (searchbar);
 	if (search_text == NULL || *search_text == '\0')
 		search_text = "''";
@@ -141,21 +159,39 @@ static void
 action_mail_flush_outbox_cb (GtkAction *action,
                              EMailShellView *mail_shell_view)
 {
-	mail_send ();
+	EShellBackend *shell_backend;
+	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
+
+	shell_view = E_SHELL_VIEW (mail_shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
+	mail_send (session);
 }
 
 static void
 action_mail_folder_copy_cb (GtkAction *action,
                             EMailShellView *mail_shell_view)
 {
-	EShellView *shell_view;
-	EShellWindow *shell_window;
 	EMailShellSidebar *mail_shell_sidebar;
+	EShellBackend *shell_backend;
+	EShellWindow *shell_window;
+	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
 	CamelFolderInfo *folder_info;
 	EMFolderTree *folder_tree;
 
 	shell_view = E_SHELL_VIEW (mail_shell_view);
 	shell_window = e_shell_view_get_shell_window (shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
 
 	mail_shell_sidebar = mail_shell_view->priv->mail_shell_sidebar;
 	folder_tree = e_mail_shell_sidebar_get_folder_tree (mail_shell_sidebar);
@@ -164,7 +200,7 @@ action_mail_folder_copy_cb (GtkAction *action,
 
 	/* XXX Leaking folder_info? */
 	em_folder_utils_copy_folder (
-		GTK_WINDOW (shell_window), folder_info, FALSE);
+		GTK_WINDOW (shell_window), session, folder_info, FALSE);
 }
 
 static void
@@ -272,14 +308,21 @@ static void
 action_mail_folder_move_cb (GtkAction *action,
                             EMailShellView *mail_shell_view)
 {
-	EShellView *shell_view;
-	EShellWindow *shell_window;
 	EMailShellSidebar *mail_shell_sidebar;
+	EShellBackend *shell_backend;
+	EShellWindow *shell_window;
+	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
 	CamelFolderInfo *folder_info;
 	EMFolderTree *folder_tree;
 
 	shell_view = E_SHELL_VIEW (mail_shell_view);
 	shell_window = e_shell_view_get_shell_window (shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
 
 	mail_shell_sidebar = mail_shell_view->priv->mail_shell_sidebar;
 	folder_tree = e_mail_shell_sidebar_get_folder_tree (mail_shell_sidebar);
@@ -288,7 +331,7 @@ action_mail_folder_move_cb (GtkAction *action,
 
 	/* XXX Leaking folder_info? */
 	em_folder_utils_copy_folder (
-		GTK_WINDOW (shell_window), folder_info, TRUE);
+		GTK_WINDOW (shell_window), session, folder_info, TRUE);
 }
 
 static void
@@ -411,14 +454,24 @@ action_mail_folder_unsubscribe_cb (GtkAction *action,
                                    EMailShellView *mail_shell_view)
 {
 	EMailShellSidebar *mail_shell_sidebar;
+	EShellBackend *shell_backend;
+	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
 	EMFolderTree *folder_tree;
 	gchar *folder_uri;
 
 	mail_shell_sidebar = mail_shell_view->priv->mail_shell_sidebar;
 	folder_tree = e_mail_shell_sidebar_get_folder_tree (mail_shell_sidebar);
 
+	shell_view = E_SHELL_VIEW (mail_shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	folder_uri = em_folder_tree_get_selected_uri (folder_tree);
-	em_folder_utils_unsubscribe_folder (folder_uri);
+	em_folder_utils_unsubscribe_folder (session, folder_uri);
 	g_free (folder_uri);
 }
 
@@ -426,13 +479,20 @@ static void
 action_mail_global_expunge_cb (GtkAction *action,
                                EMailShellView *mail_shell_view)
 {
+	EShellBackend *shell_backend;
 	EShellWindow *shell_window;
 	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
 
 	shell_view = E_SHELL_VIEW (mail_shell_view);
 	shell_window = e_shell_view_get_shell_window (shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
 
-	em_utils_empty_trash (GTK_WIDGET (shell_window));
+	em_utils_empty_trash (GTK_WIDGET (shell_window), session);
 }
 
 static void
@@ -813,13 +873,20 @@ static void
 action_mail_tools_filters_cb (GtkAction *action,
                               EMailShellView *mail_shell_view)
 {
+	EShellBackend *shell_backend;
 	EShellWindow *shell_window;
 	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
 
 	shell_view = E_SHELL_VIEW (mail_shell_view);
 	shell_window = e_shell_view_get_shell_window (shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
 
-	em_utils_edit_filters (GTK_WIDGET (shell_window));
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
+	em_utils_edit_filters (GTK_WIDGET (shell_window), session);
 }
 
 static void
@@ -834,8 +901,11 @@ action_mail_tools_subscriptions_cb (GtkAction *action,
                                     EMailShellView *mail_shell_view)
 {
 	EMailShellSidebar *mail_shell_sidebar;
+	EShellBackend *shell_backend;
 	EShellWindow *shell_window;
 	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
 	EMFolderTree *folder_tree;
 	EAccount *account = NULL;
 	GtkWidget *dialog;
@@ -843,10 +913,14 @@ action_mail_tools_subscriptions_cb (GtkAction *action,
 
 	shell_view = E_SHELL_VIEW (mail_shell_view);
 	shell_window = e_shell_view_get_shell_window (shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
 
 	mail_shell_sidebar = mail_shell_view->priv->mail_shell_sidebar;
 	folder_tree = e_mail_shell_sidebar_get_folder_tree (mail_shell_sidebar);
 
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	uri = em_folder_tree_get_selected_uri (folder_tree);
 	if (uri != NULL) {
 		account = mail_config_get_account_by_source_url (uri);
@@ -854,7 +928,8 @@ action_mail_tools_subscriptions_cb (GtkAction *action,
 	}
 
 	dialog = em_subscription_editor_new (
-		GTK_WINDOW (shell_window), session, account);
+		GTK_WINDOW (shell_window),
+		CAMEL_SESSION (session), account);
 	gtk_dialog_run (GTK_DIALOG (dialog));
 	gtk_widget_destroy (dialog);
 }
diff --git a/modules/mail/e-mail-shell-view-private.c b/modules/mail/e-mail-shell-view-private.c
index 8d4fa99..e8febf5 100644
--- a/modules/mail/e-mail-shell-view-private.c
+++ b/modules/mail/e-mail-shell-view-private.c
@@ -801,14 +801,17 @@ mail_shell_view_create_filter_cb (CamelFolder *folder,
                                   gpointer user_data)
 {
 	struct {
+		EMailSession *session;
 		const gchar *source;
 		gint type;
 	} *filter_data = user_data;
 
 	if (message != NULL)
 		filter_gui_add_from_message (
-			message, filter_data->source, filter_data->type);
+			filter_data->session, message,
+			filter_data->source, filter_data->type);
 
+	g_object_unref (filter_data->session);
 	g_free (filter_data);
 }
 
@@ -817,6 +820,10 @@ e_mail_shell_view_create_filter_from_selected (EMailShellView *mail_shell_view,
                                                gint filter_type)
 {
 	EMailShellContent *mail_shell_content;
+	EShellBackend *shell_backend;
+	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
 	EMailReader *reader;
 	EMailView *mail_view;
 	CamelFolder *folder;
@@ -825,6 +832,7 @@ e_mail_shell_view_create_filter_from_selected (EMailShellView *mail_shell_view,
 	GPtrArray *uids;
 
 	struct {
+		EMailSession *session;
 		const gchar *source;
 		gint type;
 	} *filter_data;
@@ -834,6 +842,12 @@ e_mail_shell_view_create_filter_from_selected (EMailShellView *mail_shell_view,
 	mail_shell_content = mail_shell_view->priv->mail_shell_content;
 	mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);
 
+	shell_view = E_SHELL_VIEW (mail_shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	reader = E_MAIL_READER (mail_view);
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
@@ -848,6 +862,7 @@ e_mail_shell_view_create_filter_from_selected (EMailShellView *mail_shell_view,
 
 	if (uids->len == 1) {
 		filter_data = g_malloc (sizeof (*filter_data));
+		filter_data->session = g_object_ref (session);
 		filter_data->source = filter_source;
 		filter_data->type = filter_type;
 
@@ -868,14 +883,17 @@ mail_shell_view_create_vfolder_cb (CamelFolder *folder,
                                    gpointer user_data)
 {
 	struct {
+		EMailSession *session;
 		gchar *uri;
 		gint type;
 	} *vfolder_data = user_data;
 
 	if (message != NULL)
 		vfolder_gui_add_from_message (
-			message, vfolder_data->type, vfolder_data->uri);
+			vfolder_data->session, message,
+			vfolder_data->type, vfolder_data->uri);
 
+	g_object_unref (vfolder_data->session);
 	g_free (vfolder_data->uri);
 	g_free (vfolder_data);
 }
@@ -885,6 +903,10 @@ e_mail_shell_view_create_vfolder_from_selected (EMailShellView *mail_shell_view,
                                                 gint vfolder_type)
 {
 	EMailShellContent *mail_shell_content;
+	EShellBackend *shell_backend;
+	EShellView *shell_view;
+	EMailBackend *backend;
+	EMailSession *session;
 	EMailReader *reader;
 	EMailView *mail_view;
 	CamelFolder *folder;
@@ -892,6 +914,7 @@ e_mail_shell_view_create_vfolder_from_selected (EMailShellView *mail_shell_view,
 	GPtrArray *uids;
 
 	struct {
+		EMailSession *session;
 		gchar *uri;
 		gint type;
 	} *vfolder_data;
@@ -901,6 +924,12 @@ e_mail_shell_view_create_vfolder_from_selected (EMailShellView *mail_shell_view,
 	mail_shell_content = mail_shell_view->priv->mail_shell_content;
 	mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);
 
+	shell_view = E_SHELL_VIEW (mail_shell_view);
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	reader = E_MAIL_READER (mail_view);
 	folder = e_mail_reader_get_folder (reader);
 	folder_uri = e_mail_reader_get_folder_uri (reader);
@@ -908,6 +937,7 @@ e_mail_shell_view_create_vfolder_from_selected (EMailShellView *mail_shell_view,
 
 	if (uids->len == 1) {
 		vfolder_data = g_malloc (sizeof (*vfolder_data));
+		vfolder_data->session = g_object_ref (session);
 		vfolder_data->uri = g_strdup (folder_uri);
 		vfolder_data->type = vfolder_type;
 
diff --git a/modules/mail/e-mail-shell-view-private.h b/modules/mail/e-mail-shell-view-private.h
index 816fcef..dbd2d21 100644
--- a/modules/mail/e-mail-shell-view-private.h
+++ b/modules/mail/e-mail-shell-view-private.h
@@ -43,6 +43,7 @@
 #include "e-mail-label-list-store.h"
 #include "e-mail-local.h"
 #include "e-mail-reader.h"
+#include "e-mail-session.h"
 #include "e-mail-sidebar.h"
 #include "e-mail-store.h"
 #include "em-composer-utils.h"
@@ -56,7 +57,6 @@
 #include "mail-config.h"
 #include "mail-ops.h"
 #include "mail-send-recv.h"
-#include "mail-session.h"
 #include "mail-tools.h"
 #include "mail-vfolder.h"
 #include "message-list.h"
diff --git a/modules/mail/e-mail-shell-view.c b/modules/mail/e-mail-shell-view.c
index d7f77de..4696a39 100644
--- a/modules/mail/e-mail-shell-view.c
+++ b/modules/mail/e-mail-shell-view.c
@@ -212,6 +212,8 @@ mail_shell_view_execute_search (EShellView *shell_view)
 	EShellSettings *shell_settings;
 	EShellSearchbar *searchbar;
 	EActionComboBox *combo_box;
+	EMailBackend *backend;
+	EMailSession *session;
 	EMFolderTree *folder_tree;
 	GtkTreeSelection *selection;
 	GtkWidget *message_list;
@@ -249,6 +251,9 @@ mail_shell_view_execute_search (EShellView *shell_view)
 	shell = e_shell_window_get_shell (shell_window);
 	shell_settings = e_shell_get_shell_settings (shell);
 
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	mail_shell_content = E_MAIL_SHELL_CONTENT (shell_content);
 	mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);
 	searchbar = e_mail_shell_content_get_searchbar (mail_shell_content);
@@ -550,7 +555,7 @@ all_accounts:
 
 	/* FIXME Using data_dir like this is not portable. */
 	uri = g_strdup_printf ("vfolder:%s/vfolder", data_dir);
-	store = camel_session_get_store (session, uri, NULL);
+	store = camel_session_get_store (CAMEL_SESSION (session), uri, NULL);
 	g_free (uri);
 
 	search_folder = (CamelVeeFolder *) camel_vee_folder_new (
@@ -562,7 +567,8 @@ all_accounts:
 	while (iter != NULL) {
 		folder_uri = iter->data;
 		/* FIXME Not passing a GCancellable or GError here. */
-		folder = mail_tool_uri_to_folder (folder_uri, 0, NULL, NULL);
+		folder = e_mail_session_uri_to_folder_sync (
+			E_MAIL_SESSION (session), folder_uri, 0, NULL, NULL);
 
 		if (folder != NULL)
 			list = g_list_append (list, folder);
@@ -577,7 +583,8 @@ all_accounts:
 	while (iter != NULL) {
 		folder_uri = iter->data;
 		/* FIXME Not passing a GCancellable or GError here. */
-		folder = mail_tool_uri_to_folder (folder_uri, 0, NULL, NULL);
+		folder = e_mail_session_uri_to_folder_sync (
+			E_MAIL_SESSION (session), folder_uri, 0, NULL, NULL);
 
 		if (folder != NULL)
 			list = g_list_append (list, folder);
@@ -733,7 +740,7 @@ current_account:
 
 	/* FIXME Using data_dir like this is not portable. */
 	uri = g_strdup_printf ("vfolder:%s/vfolder", data_dir);
-	store = camel_session_get_store (session, uri, NULL);
+	store = camel_session_get_store (CAMEL_SESSION (session), uri, NULL);
 	g_free (uri);
 
 	search_folder = (CamelVeeFolder *) camel_vee_folder_new (
diff --git a/modules/mail/em-account-prefs.c b/modules/mail/em-account-prefs.c
index 7b3b61f..5b734a4 100644
--- a/modules/mail/em-account-prefs.c
+++ b/modules/mail/em-account-prefs.c
@@ -35,6 +35,7 @@
 #include "e-util/e-alert-dialog.h"
 #include "e-util/e-account-utils.h"
 
+#include "e-mail-backend.h"
 #include "e-mail-store.h"
 #include "em-config.h"
 #include "em-account-editor.h"
@@ -46,28 +47,37 @@
 	((obj), EM_TYPE_ACCOUNT_PREFS, EMAccountPrefsPrivate))
 
 struct _EMAccountPrefsPrivate {
+	EMailSession *session;
 	gpointer assistant; /* weak pointer */
 	gpointer editor;    /* weak pointer */
 };
 
+enum {
+	PROP_0,
+	PROP_SESSION
+};
+
 G_DEFINE_TYPE (
 	EMAccountPrefs,
 	em_account_prefs,
 	E_TYPE_ACCOUNT_MANAGER)
 
 static void
-account_prefs_enable_account_cb (EAccountTreeView *tree_view)
+account_prefs_enable_account_cb (EAccountTreeView *tree_view,
+                                 EMAccountPrefs *prefs)
 {
 	EAccount *account;
 
 	account = e_account_tree_view_get_selected (tree_view);
 	g_return_if_fail (account != NULL);
 
-	e_mail_store_add_by_uri (account->source->url, account->name);
+	e_mail_store_add_by_uri (
+		prefs->priv->session, account->source->url, account->name);
 }
 
 static void
-account_prefs_disable_account_cb (EAccountTreeView *tree_view)
+account_prefs_disable_account_cb (EAccountTreeView *tree_view,
+                                  EMAccountPrefs *prefs)
 {
 	EAccountList *account_list;
 	EAccount *account;
@@ -96,7 +106,81 @@ account_prefs_disable_account_cb (EAccountTreeView *tree_view)
 
 	e_account_list_remove_account_proxies (account_list, account);
 
-	e_mail_store_remove_by_uri (account->source->url);
+	e_mail_store_remove_by_uri (
+		prefs->priv->session, account->source->url);
+}
+
+static void
+account_prefs_set_session (EMAccountPrefs *prefs,
+                           EMailSession *session)
+{
+	g_return_if_fail (E_IS_MAIL_SESSION (session));
+	g_return_if_fail (prefs->priv->session == NULL);
+
+	prefs->priv->session = g_object_ref (session);
+}
+
+static void
+account_prefs_set_property (GObject *object,
+                            guint property_id,
+                            const GValue *value,
+                            GParamSpec *pspec)
+{
+	switch (property_id) {
+		case PROP_SESSION:
+			account_prefs_set_session (
+				EM_ACCOUNT_PREFS (object),
+				g_value_get_object (value));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
+account_prefs_get_property (GObject *object,
+                            guint property_id,
+                            GValue *value,
+                            GParamSpec *pspec)
+{
+	switch (property_id) {
+		case PROP_SESSION:
+			g_value_set_object (
+				value,
+				em_account_prefs_get_session (
+				EM_ACCOUNT_PREFS (object)));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
+account_prefs_dispose (GObject *object)
+{
+	EMAccountPrefsPrivate *priv;
+
+	priv = EM_ACCOUNT_PREFS_GET_PRIVATE (object);
+
+	if (priv->session != NULL) {
+		g_object_unref (priv->session);
+		priv->session = NULL;
+	}
+
+	if (priv->assistant != NULL) {
+		g_object_remove_weak_pointer (
+			G_OBJECT (priv->assistant), &priv->assistant);
+		priv->assistant = NULL;
+	}
+
+	if (priv->editor != NULL) {
+		g_object_remove_weak_pointer (
+			G_OBJECT (priv->editor), &priv->editor);
+		priv->editor = NULL;
+	}
+
+	/* Chain up to parent's dispose() method. */
+	G_OBJECT_CLASS (em_account_prefs_parent_class)->dispose (object);
 }
 
 static void
@@ -126,7 +210,7 @@ account_prefs_add_account (EAccountManager *manager)
 		 * The new mail account assistant.
 		 */
 		emae = em_account_editor_new (
-			NULL, EMAE_ASSISTANT,
+			NULL, EMAE_ASSISTANT, priv->session,
 			"org.gnome.evolution.mail.config.accountAssistant");
 		e_config_create_window (
 			E_CONFIG (emae->config), NULL,
@@ -176,7 +260,7 @@ account_prefs_edit_account (EAccountManager *manager)
 	 * The account editor window.
 	 */
 	emae = em_account_editor_new (
-		account, EMAE_NOTEBOOK,
+		account, EMAE_NOTEBOOK, priv->session,
 		"org.gnome.evolution.mail.config.accountEditor");
 	e_config_create_window (
 		E_CONFIG (emae->config), parent, _("Account Editor"));
@@ -229,7 +313,8 @@ account_prefs_delete_account (EAccountManager *manager)
 
 	/* Remove the account from the folder tree. */
 	if (account->enabled && account->source && account->source->url)
-		e_mail_store_remove_by_uri (account->source->url);
+		e_mail_store_remove_by_uri (
+			priv->session, account->source->url);
 
 	/* Remove all the proxies the account has created. */
 	if (has_proxies)
@@ -242,29 +327,6 @@ account_prefs_delete_account (EAccountManager *manager)
 }
 
 static void
-account_prefs_dispose (GObject *object)
-{
-	EMAccountPrefsPrivate *priv;
-
-	priv = EM_ACCOUNT_PREFS_GET_PRIVATE (object);
-
-	if (priv->assistant != NULL) {
-		g_object_remove_weak_pointer (
-			G_OBJECT (priv->assistant), &priv->assistant);
-		priv->assistant = NULL;
-	}
-
-	if (priv->editor != NULL) {
-		g_object_remove_weak_pointer (
-			G_OBJECT (priv->editor), &priv->editor);
-		priv->editor = NULL;
-	}
-
-	/* Chain up to parent's dispose() method. */
-	G_OBJECT_CLASS (em_account_prefs_parent_class)->dispose (object);
-}
-
-static void
 em_account_prefs_class_init (EMAccountPrefsClass *class)
 {
 	GObjectClass *object_class;
@@ -273,12 +335,25 @@ em_account_prefs_class_init (EMAccountPrefsClass *class)
 	g_type_class_add_private (class, sizeof (EMAccountPrefsPrivate));
 
 	object_class = G_OBJECT_CLASS (class);
+	object_class->set_property = account_prefs_set_property;
+	object_class->get_property = account_prefs_get_property;
 	object_class->dispose = account_prefs_dispose;
 
 	account_manager_class = E_ACCOUNT_MANAGER_CLASS (class);
 	account_manager_class->add_account = account_prefs_add_account;
 	account_manager_class->edit_account = account_prefs_edit_account;
 	account_manager_class->delete_account = account_prefs_delete_account;
+
+	g_object_class_install_property (
+		object_class,
+		PROP_SESSION,
+		g_param_spec_object (
+			"session",
+			NULL,
+			NULL,
+			E_TYPE_MAIL_SESSION,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
 }
 
 static void
@@ -294,22 +369,42 @@ em_account_prefs_init (EMAccountPrefs *prefs)
 
 	g_signal_connect (
 		tree_view, "enable-account",
-		G_CALLBACK (account_prefs_enable_account_cb), NULL);
+		G_CALLBACK (account_prefs_enable_account_cb), prefs);
 
 	g_signal_connect (
 		tree_view, "disable-account",
-		G_CALLBACK (account_prefs_disable_account_cb), NULL);
+		G_CALLBACK (account_prefs_disable_account_cb), prefs);
 }
 
 GtkWidget *
 em_account_prefs_new (EPreferencesWindow *window)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailBackend *backend;
+	EMailSession *session;
 	EAccountList *account_list;
 
 	account_list = e_get_account_list ();
-
 	g_return_val_if_fail (E_IS_ACCOUNT_LIST (account_list), NULL);
 
+	/* XXX Figure out a better way to get the EMailSession. */
+	shell = e_preferences_window_get_shell (window);
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	return g_object_new (
-		EM_TYPE_ACCOUNT_PREFS, "account-list", account_list, NULL);
+		EM_TYPE_ACCOUNT_PREFS,
+		"account-list", account_list,
+		"session", session, NULL);
+}
+
+EMailSession *
+em_account_prefs_get_session (EMAccountPrefs *prefs)
+{
+	g_return_val_if_fail (EM_IS_ACCOUNT_PREFS (prefs), NULL);
+
+	return prefs->priv->session;
 }
diff --git a/modules/mail/em-account-prefs.h b/modules/mail/em-account-prefs.h
index 5fe49b6..c00b860 100644
--- a/modules/mail/em-account-prefs.h
+++ b/modules/mail/em-account-prefs.h
@@ -25,6 +25,7 @@
 #include <gtk/gtk.h>
 #include <table/e-table.h>
 #include <libedataserver/e-account-list.h>
+#include <mail/e-mail-session.h>
 #include <misc/e-account-manager.h>
 #include <widgets/misc/e-preferences-window.h>
 
@@ -62,8 +63,9 @@ struct _EMAccountPrefsClass {
 	EAccountManagerClass parent_class;
 };
 
-GType      em_account_prefs_get_type (void);
-GtkWidget *em_account_prefs_new (EPreferencesWindow *window);
+GType		em_account_prefs_get_type	(void);
+GtkWidget *	em_account_prefs_new		(EPreferencesWindow *window);
+EMailSession *	em_account_prefs_get_session	(EMAccountPrefs *prefs);
 
 G_END_DECLS
 
diff --git a/modules/mail/em-mailer-prefs.c b/modules/mail/em-mailer-prefs.c
index 570da0e..60a8795 100644
--- a/modules/mail/em-mailer-prefs.c
+++ b/modules/mail/em-mailer-prefs.c
@@ -44,13 +44,13 @@
 #include "widgets/misc/e-charset-combo-box.h"
 #include "shell/e-shell-utils.h"
 
+#include "e-mail-backend.h"
 #include "e-mail-label-manager.h"
 #include "e-mail-reader-utils.h"
 #include "mail-config.h"
 #include "em-folder-selection-button.h"
 #include "em-junk.h"
 #include "em-config.h"
-#include "mail-session.h"
 
 enum {
 	HEADER_LIST_NAME_COLUMN, /* displayable name of the header (may be a translation) */
@@ -104,6 +104,7 @@ em_mailer_prefs_finalize (GObject *object)
 {
 	EMMailerPrefs *prefs = (EMMailerPrefs *) object;
 
+	g_object_unref (prefs->session);
 	g_object_unref (prefs->builder);
 
 	if (prefs->labels_change_notify_id) {
@@ -660,7 +661,7 @@ static void
 junk_plugin_changed (GtkWidget *combo, EMMailerPrefs *prefs)
 {
 	gchar *def_plugin = gtk_combo_box_get_active_text (GTK_COMBO_BOX (combo));
-	const GList *plugins = mail_session_get_junk_plugins ();
+	const GList *plugins = mail_session_get_junk_plugins (prefs->session);
 
 	gconf_client_set_string (prefs->gconf, "/apps/evolution/mail/junk/default_plugin", def_plugin, NULL);
 	while (plugins) {
@@ -669,7 +670,8 @@ junk_plugin_changed (GtkWidget *combo, EMMailerPrefs *prefs)
 		if (iface->plugin_name && def_plugin && !strcmp (iface->plugin_name, def_plugin)) {
 			gboolean status;
 
-			session->junk_plugin = CAMEL_JUNK_PLUGIN (&iface->camel);
+			CAMEL_SESSION (prefs->session)->junk_plugin =
+				CAMEL_JUNK_PLUGIN (&iface->camel);
 			status = e_plugin_invoke (iface->hook->plugin, iface->validate_binary, NULL) != NULL;
 			if ((gboolean)status == TRUE) {
 				gchar *text, *html;
@@ -701,7 +703,7 @@ junk_plugin_setup (GtkComboBox *combo_box, EMMailerPrefs *prefs)
 	GtkCellRenderer *cell;
 	gint index = 0;
 	gboolean def_set = FALSE;
-	const GList *plugins = mail_session_get_junk_plugins ();
+	const GList *plugins = mail_session_get_junk_plugins (prefs->session);
 	gchar *pdefault = gconf_client_get_string (prefs->gconf, "/apps/evolution/mail/junk/default_plugin", NULL);
 
 	store = gtk_list_store_new (1, G_TYPE_STRING);
@@ -1127,12 +1129,23 @@ GtkWidget *
 em_mailer_prefs_new (EPreferencesWindow *window)
 {
 	EMMailerPrefs *new;
-	EShell *shell = e_preferences_window_get_shell (window);
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailBackend *backend;
+	EMailSession *session;
 
-	g_return_val_if_fail (E_IS_SHELL (shell), NULL);
+	/* XXX Figure out a better way to get the EMailSession. */
+	shell = e_preferences_window_get_shell (window);
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
 
 	new = g_object_new (EM_TYPE_MAILER_PREFS, NULL);
 
+	/* FIXME This should be a constructor property. */
+	new->session = g_object_ref (session);
+
 	/* FIXME Kill this function. */
 	em_mailer_prefs_construct (new, shell);
 
diff --git a/modules/mail/em-mailer-prefs.h b/modules/mail/em-mailer-prefs.h
index a6b3f18..d28fd5f 100644
--- a/modules/mail/em-mailer-prefs.h
+++ b/modules/mail/em-mailer-prefs.h
@@ -26,6 +26,7 @@
 #include <gtk/gtk.h>
 #include <gconf/gconf-client.h>
 #include <shell/e-shell.h>
+#include <mail/e-mail-session.h>
 #include <widgets/misc/e-preferences-window.h>
 
 /* Standard GObject macros */
@@ -55,6 +56,8 @@ typedef struct _EMMailerPrefsClass EMMailerPrefsClass;
 struct _EMMailerPrefs {
 	GtkVBox parent_object;
 
+	EMailSession *session;
+
 	GtkBuilder *builder;
 	GConfClient *gconf;
 
diff --git a/modules/startup-wizard/evolution-startup-wizard.c b/modules/startup-wizard/evolution-startup-wizard.c
index e1b8551..6f07d68 100644
--- a/modules/startup-wizard/evolution-startup-wizard.c
+++ b/modules/startup-wizard/evolution-startup-wizard.c
@@ -25,6 +25,7 @@
 #include <e-util/e-extension.h>
 #include <e-util/e-import.h>
 
+#include <mail/e-mail-backend.h>
 #include <mail/em-account-editor.h>
 #include <capplet/settings/mail-capplet-shell.h>
 #include <calendar/gui/calendar-config.h>
@@ -424,14 +425,24 @@ startup_wizard_progress_page (EConfig *config,
 static GtkWidget *
 startup_wizard_new_assistant (EStartupWizard *extension)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailBackend *backend;
+	EMailSession *session;
 	EMAccountEditor *emae;
 	EConfig *config;
 	EConfigItem *config_item;
 	GtkWidget *widget;
 	GSList *items = NULL;
 
+	shell = startup_wizard_get_shell (extension);
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+
+	backend = E_MAIL_BACKEND (shell_backend);
+	session = e_mail_backend_get_session (backend);
+
 	emae = em_account_editor_new (
-		NULL, EMAE_ASSISTANT,
+		NULL, EMAE_ASSISTANT, session,
 		"org.gnome.evolution.mail.config.accountWizard");
 
 	config = E_CONFIG (emae->config);
@@ -515,7 +526,7 @@ startup_wizard_run (EStartupWizard *extension)
 	const gchar *startup_view;
 	gboolean express_mode;
 
-	shell = e_shell_get_default ();
+	shell = startup_wizard_get_shell (extension);
 	express_mode = e_shell_get_express_mode (shell);
 	startup_view = e_shell_get_startup_view (shell);
 
diff --git a/plugins/dbx-import/dbx-importer.c b/plugins/dbx-import/dbx-importer.c
index c4f1d46..3079442 100644
--- a/plugins/dbx-import/dbx-importer.c
+++ b/plugins/dbx-import/dbx-importer.c
@@ -65,6 +65,7 @@
 #include <libedataserver/e-data-server-util.h>
 #include <libedataserverui/e-source-selector-dialog.h>
 
+#include <mail/e-mail-backend.h>
 #include <mail/e-mail-local.h>
 #include <mail/mail-mt.h>
 #include <mail/mail-tools.h>
@@ -542,6 +543,9 @@ dbx_read_email (DbxImporter *m, guint32 offset, gint bodyfd, gint *flags)
 static void
 dbx_import_file (DbxImporter *m)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	gchar *filename;
 	CamelFolder *folder;
 	gint tmpfile;
@@ -551,9 +555,16 @@ dbx_import_file (DbxImporter *m)
 	filename = g_filename_from_uri (((EImportTargetURI *)m->target)->uri_src, NULL, NULL);
 	m->parent_uri = g_strdup (((EImportTargetURI *)m->target)->uri_dest); /* Destination folder, was set in our widget */
 
+	/* XXX Dig up the EMailSession from the default EShell.
+	 *     Since the EImport framework doesn't allow for user
+	 *     data, I don't see how else to get to it. */
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	camel_operation_push_message (NULL, _("Importing '%s'"), filename);
-	folder = mail_tool_uri_to_folder (
-		m->parent_uri, CAMEL_STORE_FOLDER_CREATE,
+	folder = e_mail_session_uri_to_folder_sync (
+		session, m->parent_uri, CAMEL_STORE_FOLDER_CREATE,
 		m->base.cancellable, &m->base.error);
 	if (!folder)
 		return;
diff --git a/plugins/groupwise-features/install-shared.c b/plugins/groupwise-features/install-shared.c
index 92d5ae6..b216d49 100644
--- a/plugins/groupwise-features/install-shared.c
+++ b/plugins/groupwise-features/install-shared.c
@@ -31,6 +31,7 @@
 #include <gconf/gconf-client.h>
 #include <e-util/e-config.h>
 #include <shell/e-shell.h>
+#include <mail/e-mail-backend.h>
 #include <mail/em-config.h>
 #include <mail/em-event.h>
 #include <mail/em-folder-tree.h>
@@ -155,17 +156,24 @@ accept_free (gpointer data)
 static void
 apply_clicked (GtkAssistant *assistant, CamelMimeMessage *msg)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	EMFolderTree *folder_tree;
 	GtkWidget *dialog;
 	struct AcceptData *accept_data;
 	gchar *uri;
 	gpointer parent;
 
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	parent = gtk_widget_get_toplevel (GTK_WIDGET (assistant));
 	parent = gtk_widget_is_toplevel (parent) ? parent : NULL;
 
 	accept_data = g_new0 (struct AcceptData, 1);
-	folder_tree = (EMFolderTree *) em_folder_tree_new ();
+	folder_tree = (EMFolderTree *) em_folder_tree_new (session);
 
 	dialog = em_folder_selector_create_new (parent, folder_tree, 0, _("Create folder"), _("Specify where to create the folder:"));
 	uri = em_folder_tree_get_selected_uri (folder_tree);
diff --git a/plugins/groupwise-features/proxy-login.c b/plugins/groupwise-features/proxy-login.c
index 12d095b..74b67f0 100644
--- a/plugins/groupwise-features/proxy-login.c
+++ b/plugins/groupwise-features/proxy-login.c
@@ -30,6 +30,7 @@
 
 #include <libedataserverui/e-passwords.h>
 #include <mail/em-folder-tree.h>
+#include <mail/e-mail-backend.h>
 #include <mail/e-mail-store.h>
 #include <mail/mail-config.h>
 #include <mail/em-folder-selector.h>
@@ -307,6 +308,9 @@ proxy_soap_login (gchar *email, GtkWindow *error_parent)
 	EAccountList *accounts = e_get_account_list ();
 	EAccount *srcAccount;
 	EAccount *dstAccount;
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	EGwConnection *proxy_cnc, *cnc;
 	CamelURL *uri = NULL, *parent = NULL;
 	gchar *password = NULL, *user_name = NULL;
@@ -315,6 +319,10 @@ proxy_soap_login (gchar *email, GtkWindow *error_parent)
 	gint i;
 	gint permissions = 0;
 
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	for (i=0; email[i]!='\0' && email[i]!='@' ; i++);
 	if (email[i]=='@')
 		name = g_strndup (email, i);
@@ -360,7 +368,7 @@ proxy_soap_login (gchar *email, GtkWindow *error_parent)
 		e_account_list_change (accounts, srcAccount);
 		e_account_list_save (accounts);
 		g_object_set_data ((GObject *)dstAccount, "permissions", GINT_TO_POINTER(permissions));
-		mail_get_store (e_account_get_string (dstAccount, E_ACCOUNT_SOURCE_URL), NULL, proxy_login_add_new_store, dstAccount);
+		mail_get_store (session, e_account_get_string (dstAccount, E_ACCOUNT_SOURCE_URL), NULL, proxy_login_add_new_store, dstAccount);
 
 		g_free (proxy_source_url);
 		g_free (parent_source_url);
@@ -381,9 +389,16 @@ proxy_soap_login (gchar *email, GtkWindow *error_parent)
 static void
 proxy_login_add_new_store (gchar *uri, CamelStore *store, gpointer user_data)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	EAccount *account = user_data;
 	gint permissions = GPOINTER_TO_INT(g_object_get_data ((GObject *)account, "permissions"));
 
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	if (store == NULL)
 		return;
 
@@ -391,7 +406,7 @@ proxy_login_add_new_store (gchar *uri, CamelStore *store, gpointer user_data)
 	    store->mode &= !CAMEL_STORE_WRITE;
 
 	store->flags |= CAMEL_STORE_PROXY;
-	e_mail_store_add (store, account->name);
+	e_mail_store_add (session, store, account->name);
 }
 
 static void
diff --git a/plugins/groupwise-features/proxy.c b/plugins/groupwise-features/proxy.c
index beb1c94..3d35023 100644
--- a/plugins/groupwise-features/proxy.c
+++ b/plugins/groupwise-features/proxy.c
@@ -40,7 +40,8 @@
 #include <mail/em-account-editor.h>
 #include <mail/em-config.h>
 #include <mail/mail-ops.h>
-#include <mail/mail-session.h>
+#include <mail/e-mail-backend.h>
+#include <shell/e-shell.h>
 #include <libedataserver/e-account.h>
 #include <libedataserver/e-account-list.h>
 #include <e-util/e-icon-factory.h>
@@ -637,6 +638,9 @@ GtkWidget*
 org_gnome_proxy (EPlugin *epl, EConfigHookItemFactoryData *data)
 {
 	EMConfigTargetAccount *target_account;
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	EAccount *account;
 	GtkButton *addProxy, *removeProxy, *editProxy;
 	proxyDialog *prd;
@@ -644,13 +648,18 @@ org_gnome_proxy (EPlugin *epl, EConfigHookItemFactoryData *data)
 	CamelOfflineStore *store;
 	gint pag_num;
 
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	target_account = (EMConfigTargetAccount *)data->config->target;
 	account = target_account->account;
 	/* We are using some g_object_set on this. We shuold also avoid double-free later. So reffing */
 	g_object_ref (account);
 
 	store = (CamelOfflineStore *) camel_session_get_service (
-		session, e_account_get_string (account, E_ACCOUNT_SOURCE_URL),
+		CAMEL_SESSION (session),
+		e_account_get_string (account, E_ACCOUNT_SOURCE_URL),
 		CAMEL_PROVIDER_STORE, NULL);
 	if (store == NULL)
 		return NULL;
diff --git a/plugins/groupwise-features/share-folder-common.c b/plugins/groupwise-features/share-folder-common.c
index 387c960..adb373d 100644
--- a/plugins/groupwise-features/share-folder-common.c
+++ b/plugins/groupwise-features/share-folder-common.c
@@ -29,6 +29,7 @@
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 #include <e-util/e-config.h>
+#include <mail/e-mail-backend.h>
 #include <mail/em-config.h>
 #include <mail/em-folder-properties.h>
 #include <mail/em-folder-tree.h>
@@ -243,6 +244,7 @@ users_dialog_response (GtkWidget *dialog, gint response, struct ShareInfo *ssi)
 	struct _EMFolderTreeModelStoreInfo *si;
 	EMFolderSelector *emfs = ssi->emfs;
 	const gchar *uri, *path;
+	EMailSession *session;
 	CamelStore *store;
 
 	if (response != GTK_RESPONSE_OK) {
@@ -253,11 +255,12 @@ users_dialog_response (GtkWidget *dialog, gint response, struct ShareInfo *ssi)
 
 	uri = em_folder_selector_get_selected_uri (emfs);
 	path = em_folder_selector_get_selected_path (emfs);
+	session = em_folder_tree_get_session (emfs->emft);
 
 	d(printf ("Creating new folder: %s (%s)\n", path, uri));
 
 	store = (CamelStore *) camel_session_get_service (
-		session, uri, CAMEL_PROVIDER_STORE, NULL);
+		CAMEL_SESSION (session), uri, CAMEL_PROVIDER_STORE, NULL);
 	if (store == NULL)
 		return;
 
@@ -268,14 +271,14 @@ users_dialog_response (GtkWidget *dialog, gint response, struct ShareInfo *ssi)
 	}
 
 	if (CAMEL_IS_VEE_STORE (store)) {
-		EMVFolderRule *rule;
+		EFilterRule *rule;
 
 		/* ensures vfolder is running */
-		vfolder_load_storage ();
+		vfolder_load_storage (session);
 
-		rule = em_vfolder_rule_new ();
-		e_filter_rule_set_name ((EFilterRule *)rule, path);
-		vfolder_gui_add_rule (rule);
+		rule = em_vfolder_rule_new (session);
+		e_filter_rule_set_name (rule, path);
+		vfolder_gui_add_rule (EM_VFOLDER_RULE (rule));
 		gtk_widget_destroy ((GtkWidget *)emfs);
 	} else {
 		g_object_ref (emfs);
@@ -336,12 +339,17 @@ void
 gw_new_shared_folder_cb (GtkAction *action, EShellView *shell_view)
 {
 	EMFolderTree *folder_tree;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	GtkWidget *dialog;
 	gchar *uri;
 	gpointer parent;
 
 	parent = e_shell_view_get_shell_window (shell_view);
-	folder_tree = (EMFolderTree *) em_folder_tree_new ();
+	shell_backend = e_shell_view_get_shell_backend (shell_view);
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
+	folder_tree = (EMFolderTree *) em_folder_tree_new (session);
 	emu_restore_folder_tree_state (folder_tree);
 
 	dialog = em_folder_selector_create_new (parent, folder_tree, 0, _("Create folder"), _("Specify where to create the folder:"));
diff --git a/plugins/mailing-list-actions/mailing-list-actions.c b/plugins/mailing-list-actions/mailing-list-actions.c
index 254451c..8b123e7 100644
--- a/plugins/mailing-list-actions/mailing-list-actions.c
+++ b/plugins/mailing-list-actions/mailing-list-actions.c
@@ -116,15 +116,18 @@ emla_list_action_do (CamelFolder *folder,
 	gint t;
 	EMsgComposer *composer;
 	gint send_message_response;
-	EShellBackend *shell_backend;
 	EShell *shell;
+	EMailBackend *backend;
+	EShellBackend *shell_backend;
 	EAccount *account;
 	GtkWindow *window;
 
 	if (msg == NULL)
 		return;
 
-	shell_backend = e_mail_reader_get_shell_backend (action_data->reader);
+	backend = e_mail_reader_get_backend (action_data->reader);
+
+	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 
 	window = e_mail_reader_get_window (action_data->reader);
diff --git a/plugins/mark-all-read/mark-all-read.c b/plugins/mark-all-read/mark-all-read.c
index ffbbbcc..62b8102 100644
--- a/plugins/mark-all-read/mark-all-read.c
+++ b/plugins/mark-all-read/mark-all-read.c
@@ -421,15 +421,19 @@ action_mail_mark_read_recursive_cb (GtkAction *action,
 {
 	EShellSidebar *shell_sidebar;
 	EMFolderTree *folder_tree;
+	EMailSession *session;
 	gchar *folder_uri;
 
 	shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);
 	g_object_get (shell_sidebar, "folder-tree", &folder_tree, NULL);
+
+	session = em_folder_tree_get_session (folder_tree);
+
 	folder_uri = em_folder_tree_get_selected_uri (folder_tree);
 	g_return_if_fail (folder_uri != NULL);
 
 	mail_get_folder (
-		folder_uri, 0, mar_got_folder,
+		session, folder_uri, 0, mar_got_folder,
 		NULL, mail_msg_unordered_push);
 
 	g_object_unref (folder_tree);
diff --git a/plugins/pst-import/pst-importer.c b/plugins/pst-import/pst-importer.c
index 8a42d0d..e9203d1 100644
--- a/plugins/pst-import/pst-importer.c
+++ b/plugins/pst-import/pst-importer.c
@@ -52,10 +52,12 @@
 #include <libedataserver/e-data-server-util.h>
 #include <libedataserverui/e-source-selector-dialog.h>
 
+#include <mail/e-mail-backend.h>
 #include <mail/e-mail-local.h>
 #include <mail/mail-mt.h>
 #include <mail/mail-tools.h>
 #include <mail/em-utils.h>
+#include <shell/e-shell.h>
 
 #include <libpst/libpst.h>
 #include <libpst/timeconv.h>
@@ -214,12 +216,22 @@ folder_selected (EMFolderSelectionButton *button, EImportTargetURI *target)
 static gchar *
 get_suggested_foldername (EImportTargetURI *target)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	const gchar *inbox;
 	gchar *delim, *filename;
 	gchar *rootname = NULL;
 	GString *foldername;
 	pst_file pst;
 
+	/* XXX Dig up the EMailSession from the default EShell.
+	 *     Since the EImport framework doesn't allow for user
+	 *     data, I don't see how else to get to it. */
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	/* Suggest a folder that is in the same mail storage as the users' inbox,
 	   with a name derived from the .PST file */
 	inbox = e_mail_local_get_folder_uri (E_MAIL_FOLDER_INBOX);
@@ -251,8 +263,10 @@ get_suggested_foldername (EImportTargetURI *target)
 		g_string_append (foldername, "outlook_data");
 	}
 
+	/* FIXME Leaking a CamelFolder reference here. */
 	/* FIXME Not passing a GCancellable or GError here. */
-	if (mail_tool_uri_to_folder (foldername->str, 0, NULL, NULL) != NULL) {
+	if (e_mail_session_uri_to_folder_sync (
+		session, foldername->str, 0, NULL, NULL) != NULL) {
 		CamelFolder *folder;
 
 		/* Folder exists - add a number */
@@ -263,7 +277,8 @@ get_suggested_foldername (EImportTargetURI *target)
 			g_string_truncate (foldername, len);
 			g_string_append_printf (foldername, "_%d", i);
 			/* FIXME Not passing a GCancellable or GError here. */
-			if ((folder=mail_tool_uri_to_folder (foldername->str, 0, NULL, NULL)) == NULL) {
+			if ((folder = e_mail_session_uri_to_folder_sync (
+				session, foldername->str, 0, NULL, NULL)) == NULL) {
 				/* Folder does not exist */
 				break;
 			}
@@ -438,19 +453,29 @@ pst_import_import (PstImporter *m)
 static void
 pst_import_file (PstImporter *m)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	gint ret;
 	gchar *filename;
 	pst_item *item = NULL;
 	pst_desc_tree *d_ptr;
 
+	/* XXX Dig up the EMailSession from the default EShell.
+	 *     Since the EImport framework doesn't allow for user
+	 *     data, I don't see how else to get to it. */
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	filename = g_filename_from_uri (((EImportTargetURI *)m->target)->uri_src, NULL, NULL);
 	m->parent_uri = g_strdup (((EImportTargetURI *)m->target)->uri_dest); /* Destination folder, was set in our widget */
 
 	camel_operation_push_message (NULL, _("Importing '%s'"), filename);
 
 	if (GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-mail"))) {
-		mail_tool_uri_to_folder (
-			m->parent_uri, CAMEL_STORE_FOLDER_CREATE,
+		e_mail_session_uri_to_folder_sync (
+			session, m->parent_uri, CAMEL_STORE_FOLDER_CREATE,
 			m->base.cancellable, &m->base.error);
 	}
 
@@ -682,10 +707,20 @@ pst_process_folder (PstImporter *m, pst_item *item)
 static void
 pst_create_folder (PstImporter *m)
 {
+	EShell *shell;
+	EShellBackend *shell_backend;
+	EMailSession *session;
 	const gchar *parent;
 	gchar *dest, *dest_end, *pos;
 	gint dest_len;
 
+	/* XXX Dig up the EMailSession from the default EShell.
+	 *     Since the EImport framework doesn't allow for user
+	 *     data, I don't see how else to get to it. */
+	shell = e_shell_get_default ();
+	shell_backend = e_shell_get_backend_by_name (shell, "mail");
+	session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+
 	parent = ((EImportTargetURI *)m->target)->uri_dest;
 	dest = g_strdup (m->folder_uri);
 
@@ -703,8 +738,8 @@ pst_create_folder (PstImporter *m)
 
 			*pos = '\0';
 
-			folder = mail_tool_uri_to_folder (
-				dest, CAMEL_STORE_FOLDER_CREATE,
+			folder = e_mail_session_uri_to_folder_sync (
+				session, dest, CAMEL_STORE_FOLDER_CREATE,
 				m->base.cancellable, &m->base.error);
 			g_object_unref (folder);
 			*pos = '/';
@@ -717,8 +752,8 @@ pst_create_folder (PstImporter *m)
 		g_object_unref (m->folder);
 	}
 
-	m->folder = mail_tool_uri_to_folder (
-		m->folder_uri, CAMEL_STORE_FOLDER_CREATE,
+	m->folder = e_mail_session_uri_to_folder_sync (
+		session, m->folder_uri, CAMEL_STORE_FOLDER_CREATE,
 		m->base.cancellable, &m->base.error);
 }
 
diff --git a/plugins/templates/templates.c b/plugins/templates/templates.c
index 7f62240..901f902 100644
--- a/plugins/templates/templates.c
+++ b/plugins/templates/templates.c
@@ -33,9 +33,9 @@
 
 #include <mail/e-mail-local.h>
 #include <mail/e-mail-reader.h>
+#include <mail/e-mail-session.h>
 #include <mail/em-composer-utils.h>
 #include <mail/em-utils.h>
-#include <mail/mail-session.h>
 #include <mail/mail-ops.h>
 #include <mail/message-list.h>
 #include <e-util/e-alert-dialog.h>



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