diff --git a/mobile/lib/kdeclarativeapplication.cpp b/mobile/lib/kdeclarativeapplication.cpp
index eb8bbca..5e24edd 100644
--- a/mobile/lib/kdeclarativeapplication.cpp
+++ b/mobile/lib/kdeclarativeapplication.cpp
@@ -32,7 +32,7 @@ int staticInitKConfigGroupGui();
#endif

static inline bool runPreApplicationSetup( const KCmdLineOptions & opts ) {
-  Q_UNUSED( opts );
+
#ifdef _WIN32_WCE
  QThread::currentThread()->setPriority(QThread::HighPriority);
#endif
@@ -40,7 +40,7 @@ static inline bool runPreApplicationSetup( const KCmdLineOptions & opts ) {
  //This is needed to get KConfig working with QColor
  staticInitKConfigGroupGui();
#endif
-    KDeclarativeApplicationBase::preApplicationSetup();
+    KDeclarativeApplicationBase::preApplicationSetup(opts);
    return true; // <-- default value of KApplication(bool) ctor
}

diff --git a/mobile/lib/kdeclarativeapplication.h b/mobile/lib/kdeclarativeapplication.h
index 217dd4e..edeaffa 100644
--- a/mobile/lib/kdeclarativeapplication.h
+++ b/mobile/lib/kdeclarativeapplication.h
@@ -86,7 +86,7 @@ class KDeclarativeApplication : public KDeclarativeApplicationBase
      return 0;
    }

-  private:
+  protected:
    T* m_mainView;
};

diff --git a/mobile/mail/kmailmobileoptions.h b/mobile/mail/kmailmobileoptions.h
new file mode 100644
index 0000000..2e1db92
--- /dev/null
+++ b/mobile/mail/kmailmobileoptions.h
@@ -0,0 +1,38 @@
+/*
+* Copyright 2011 Lamarque Souza <[email protected]>
+*
+* This library 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.1 of the License, or (at your option) any later version.
+*
+* This library 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 this library; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+* 02110-1301  USA
+*/
+
+#ifndef KMAIL_MOBILE_OPTIONS_H
+#define KMAIL_MOBILE_OPTIONS_H
+
+#include <kcmdlineargs.h>
+#include <klocale.h>
+
+static KCmdLineOptions kmailMobileOptions()
+{
+  KCmdLineOptions options;
+  options.add("t <address>",   ki18n("Send message to 'address'"));
+  options.add("s <subject>",   ki18n("Set subject of message"));
+  options.add("c <address>",   ki18n("Send CC: to 'address'"));
+  options.add("b <address>",   ki18n("Send BCC: to 'address'"));
+  options.add("B <text>",      ki18n("Set body of message"));
+  options.add("A <url>",       ki18n("Add an attachment to the mail. This can be repeated"));
+  return options;
+}
+
+#endif
diff --git a/mobile/mail/main.cpp b/mobile/mail/main.cpp
index df80253..9cb9707 100644
--- a/mobile/mail/main.cpp
+++ b/mobile/mail/main.cpp
@@ -20,9 +20,9 @@
*/

#include "mainview.h"
+#include "kmailmobileoptions.h"

#include <kaboutdata.h>
-#include <kcmdlineargs.h>
#include <kdeclarativeapplication.h>

#ifdef Q_OS_WINCE
@@ -45,6 +45,31 @@ Q_IMPORT_PLUGIN(akonadi_serializer_kcalcore)
extern bool ___MailTransport____INIT();
#endif

+class KMailMobileApplication : public KDeclarativeApplication<MainView>
+{
+public:
+  KMailMobileApplication();
+  explicit KMailMobileApplication( const KCmdLineOptions &applicationOptions );
+  virtual int newInstance();
+};
+
+KMailMobileApplication::KMailMobileApplication(): KDeclarativeApplication<MainView>()
+{
+};
+
+KMailMobileApplication::KMailMobileApplication( const KCmdLineOptions &applicationOptions ): KDeclarativeApplication<MainView>( applicationOptions )
+{
+};
+
+int KMailMobileApplication::KMailMobileApplication::newInstance()
+{
+  KDeclarativeApplication<MainView>::newInstance();
+  if ( m_mainView ) {
+    m_mainView->handleCommandLine();
+  }
+  return 0;
+}
+
int main( int argc, char **argv )
{
  kWarning() << "Starting main function" << QDateTime::currentDateTime();
@@ -62,7 +87,11 @@ int main( int argc, char **argv )
  aboutData.setProductName( "KMail Mobile" ); //has to match the bugzilla product name

  KCmdLineArgs::init( argc, argv, &aboutData );
-  KDeclarativeApplication<MainView> app;
+  KMailMobileApplication app( kmailMobileOptions() );
+
+  if ( !KMailMobileApplication::start() ) {
+     return 0;
+  }

  KGlobal::locale()->insertCatalog( "libakonadi-kmime" );
  KGlobal::locale()->insertCatalog( "libmessagecore" );
diff --git a/mobile/mail/mainview.cpp b/mobile/mail/mainview.cpp
index b3ddfdf..427937e 100644
--- a/mobile/mail/mainview.cpp
+++ b/mobile/mail/mainview.cpp
@@ -86,11 +86,13 @@
#include <kactioncollection.h>
#include <kcmdlineargs.h>
#include <kcmultidialog.h>
+#include <kcodecs.h>
#include <kdebug.h>
#include <klinkitemselectionmodel.h>
#include <klocalizedstring.h>
#include <kmessagebox.h>
#include <kmime/kmime_message.h>
+#include <kmimetype.h>
#include <kpimidentities/identity.h>
#include <kpimidentities/identitymanager.h>
#include <kselectionproxymodel.h>
@@ -180,6 +182,28 @@ MainView::~MainView()
  }
}

+void MainView::handleCommandLine()
+{
+  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
+
+  if ( args->isSet("A") ) {
+    QMetaObject::invokeMethod( this, "openComposerAndAttach", Qt::QueuedConnection,
+                               Q_ARG(QString, args->getOption("t")),
+                               Q_ARG(QString, args->getOption("c")),
+                               Q_ARG(QString, args->getOption("b")),
+                               Q_ARG(QString, args->getOption("s")),
+                               Q_ARG(QString, args->getOption("B")),
+                               Q_ARG(QStringList, QStringList() << args->getOptionList("A")) );
+  } else if ( args->isSet("t") ) {
+    QMetaObject::invokeMethod( this, "openComposer", Qt::QueuedConnection,
+                               Q_ARG(QString, args->getOption("t")),
+                               Q_ARG(QString, args->getOption("c")),
+                               Q_ARG(QString, args->getOption("b")),
+                               Q_ARG(QString, args->getOption("s")),
+                               Q_ARG(QString, args->getOption("B")) );
+  }
+}
+
void MainView::setConfigWidget( ConfigWidget *configWidget )
{
  Q_ASSERT( configWidget );
@@ -215,6 +239,86 @@ int MainView::openComposer( const QString &to, const QString &cc, const QString
  return 0;
}

+int MainView::openComposerAndAttach( const QString &to, const QString &cc, const QString &bcc,
+                                     const QString &subject, const QString &body,
+                                     const QStringList &attachments )
+{
+  if (attachments.isEmpty()) {
+      return openComposer( to, cc, bcc, subject, body );
+  }
+
+  // Set the multipart message.
+  KMime::Message::Ptr message = KMime::Message::Ptr( new KMime::Message );
+  KMime::Headers::ContentType *ct = message->contentType();
+  ct->setMimeType( "multipart/mixed" );
+  ct->setBoundary( KMime::multiPartBoundary() );
+  ct->setCategory( KMime::Headers::CCcontainer );
+  message->contentTransferEncoding()->clear();
+
+  // Set the headers.
+  message->to()->fromUnicodeString( to, "utf-8" );
+  message->cc()->fromUnicodeString( cc, "utf-8" );
+  message->bcc()->fromUnicodeString( bcc, "utf-8" );
+  message->date()->setDateTime( KDateTime::currentLocalDateTime() );
+  message->subject()->fromUnicodeString( subject, "utf-8" );
+
+  // Set the first multipart, the body message.
+  KMime::Content *bodyMessage = new KMime::Content;
+  bodyMessage->contentType()->setMimeType( "text/plain" );
+  bodyMessage->setBody( body.toUtf8() + "\n\n" );
+  message->addContent( bodyMessage );
+
+  KUrl::List attachURLs = KUrl::List( attachments );
+  for ( KUrl::List::ConstIterator it = attachURLs.constBegin(); it != attachURLs.constEnd(); ++it ) {
+    message->addContent( createAttachment( (*it) ) );
+  }
+
+  message->assemble();
+
+  ComposerView *composer = new ComposerView;
+  composer->setMessage( message );
+  composer->show();
+  composer->setIdentity( currentFolderIdentity() );
+
+  return 0;
+}
+
+KMime::Content *MainView::createAttachment( const KUrl &url ) const
+{
+  KMimeType::Ptr mimeType = KMimeType::findByUrl(url, 0, true);
+  QString fileName = url.toLocalFile();
+  QFile file(fileName);
+
+  if ( !file.open(QIODevice::ReadOnly) ) {
+      kDebug() << "Error opening attachment file" << fileName;
+      return 0;
+  }
+
+  // TODO: abort in case of huge file.
+  qint64 size = file.size();
+  QByteArray contents = file.readAll();
+  file.close();
+
+  if ( contents.size() < size ) {
+      kDebug() << "Short read while attaching file" << fileName;
+  }
+
+  QByteArray coded = KCodecs::base64Encode( contents, true );
+  KMime::Headers::ContentDisposition *d = new KMime::Headers::ContentDisposition;
+  d->setDisposition( KMime::Headers::CDattachment );
+  d->setFilename( fileName.section('/', -1) );
+  d->setDisposition( KMime::Headers::CDattachment );
+
+  KMime::Content *a = new KMime::Content();
+  a->contentType()->fromUnicodeString( mimeType->name(), "utf-8" );
+  a->setHeader( d );
+  a->contentTransferEncoding()->setEncoding( KMime::Headers::CEbase64 );
+  a->contentTransferEncoding()->setDecoded( false );
+  a->setBody( coded + "\n\n" );
+
+  return a;
+}
+
#define VIEW(model) {                        \
  QTreeView *view = new QTreeView( this );   \
  view->setWindowFlags( Qt::Window );        \
diff --git a/mobile/mail/mainview.h b/mobile/mail/mainview.h
index f137944..8c22527 100644
--- a/mobile/mail/mainview.h
+++ b/mobile/mail/mainview.h
@@ -70,6 +70,15 @@ class MainView : public KDeclarativeMainView
               "      <arg direction=\"in\" type=\"s\" name=\"subject\"/>\n"
               "      <arg direction=\"in\" type=\"s\" name=\"body\"/>\n"
               "    </method>\n"
+               "    <method name=\"openComposerAndAttach\">\n"
+               "      <arg direction=\"out\" type=\"i\"/>\n"
+               "      <arg direction=\"in\" type=\"s\" name=\"to\"/>\n"
+               "      <arg direction=\"in\" type=\"s\" name=\"cc\"/>\n"
+               "      <arg direction=\"in\" type=\"s\" name=\"bcc\"/>\n"
+               "      <arg direction=\"in\" type=\"s\" name=\"subject\"/>\n"
+               "      <arg direction=\"in\" type=\"s\" name=\"body\"/>\n"
+               "      <arg direction=\"in\" type=\"as\" name=\"attachments\"/>\n"
+               "    </method>\n"
               "  </interface>\n"
                       "")

@@ -80,6 +89,8 @@ class MainView : public KDeclarativeMainView

    ~MainView();

+    void handleCommandLine();
+
    enum ForwardMode {
      InLine = 0,
      AsAttachment,
@@ -120,6 +131,13 @@ class MainView : public KDeclarativeMainView
                                   const QString & bcc,
                                   const QString & subject,
                                   const QString & body );
+
+    Q_SCRIPTABLE int openComposerAndAttach( const QString & to,
+                                            const QString & cc,
+                                            const QString & bcc,
+                                            const QString & subject,
+                                            const QString & body,
+                                            const QStringList & attachments );
    void mailActionStateUpdated();

  Q_SIGNALS:
@@ -202,6 +220,7 @@ class MainView : public KDeclarativeMainView
    MessageViewer::MessageViewItem *messageViewerItem();
    uint currentFolderIdentity() const;
    QString itemStorageCollectionAsPath( const Akonadi::Item& ) const;
+    KMime::Content *createAttachment( const KUrl &url ) const;

    bool mAskingToGoOnline;
    QWidget *mTransportDialog;