'Save the message on the Server
This backend always assigns a new uid, so the uid arg is ignored.
This function will update the self.messagelist dict to contain
the new message after sucessfully saving it.
See folder/Base for details. Note that savemessage() does not
check against dryrun settings, so you need to ensure that
savemessage is never called in a dryrun mode.
:param rtime: A timestamp to be used as the mail date
:returns: the UID of the new message as assigned by the server. If the
message is saved, but it\'s UID can not be found, it will
return 0. If the message can\'t be written (folder is
read-only for example) it will return -1.'

def savemessage(self, uid, content, flags, rtime):

    self.ui.savemessage('imap', uid, flags, self)
    if ((uid > 0) and self.uidexists(uid)):
        self.savemessageflags(uid, flags)
        return uid
    content = self.deletemessageheaders(content, self.filterheaders)
    content = re.sub('(?<!\r)\n', CRLF, content)
    date = self.__getmessageinternaldate(content, rtime)
    msg_id = self.getmessageheader(content, 'message-id')
    if (not msg_id):
        msg_id = '[unknown    message-id]'
    retry_left = 2
    imapobj = self.imapserver.acquireconnection()
    try:
        while retry_left:
            use_uidplus = ('UIDPLUS' in imapobj.capabilities)
            if (not use_uidplus):
                (headername, headervalue) = self.__generate_randomheader(content)
                self.ui.debug('imap', ('savemessage:    header    is:    %s:    %s' % (headername, headervalue)))
                content = self.addmessageheader(content, CRLF, headername, headervalue)
            if (len(content) > 200):
                dbg_output = ('%s...%s' % (content[:150], content[(-50):]))
            else:
                dbg_output = content
            self.ui.debug('imap', ("savemessage:    date:    %s,    content:    '%s'" % (date, dbg_output)))
            try:
                imapobj.select(self.getfullname())
            except imapobj.readonly:
                self.ui.msgtoreadonly(self, uid, content, flags)
                return uid
            try:
                (typ, dat) = imapobj.append(self.getfullname(), imaputil.flagsmaildir2imap(flags), date, content)
                if (typ != 'OK'):
                    msg = ("Saving    msg    (%s)    in    folder    '%s',    repository    '%s'    failed    (abort).    Server    responded:    %s    %s\n" % (msg_id, self, self.getrepository(), typ, dat))
                    raise OfflineImapError(msg, OfflineImapError.ERROR.REPO)
                retry_left = 0
            except imapobj.abort as e:
                retry_left -= 1
                self.imapserver.releaseconnection(imapobj, True)
                imapobj = self.imapserver.acquireconnection()
                if (not retry_left):
                    six.reraise(OfflineImapError, OfflineImapError(("Saving    msg    (%s)    in    folder    '%s',    repository    '%s'    failed    (abort).    Server    responded:    %s\nMessage    content    was:    %s" % (msg_id, self, self.getrepository(), str(e), dbg_output)), OfflineImapError.ERROR.MESSAGE), exc_info()[2])
                self.ui.error(e, exc_info()[2])
            except imapobj.error as e:
                self.imapserver.releaseconnection(imapobj, True)
                imapobj = None
                six.reraise(OfflineImapError, OfflineImapError(("Saving    msg    (%s)    folder    '%s',    repo    '%s'failed    (error).    Server    responded:    %s\nMessage    content    was:    %s" % (msg_id, self, self.getrepository(), str(e), dbg_output)), OfflineImapError.ERROR.MESSAGE), exc_info()[2])
        (typ, dat) = imapobj.check()
        assert (typ == 'OK')
        if use_uidplus:
            resp = imapobj._get_untagged_response('APPENDUID')
            if ((resp == [None]) or (resp is None)):
                self.ui.warn(('Server    supports    UIDPLUS    but    got    no    APPENDUID    appending    a    message.    Got:    %s.' % str(resp)))
                return 0
            try:
                uid = int(resp[(-1)].split('    ')[1])
            except ValueError as e:
                uid = 0
            except Exception as e:
                raise OfflineImapError(('Unexpected    response:    %s' % str(resp)), OfflineImapError.ERROR.MESSAGE)
            if (uid == 0):
                self.ui.warn(("savemessage:    Server    supports    UIDPLUS,    but    we    got    no    usable    uid    back.    APPENDUID    reponse    was    '%s'" % str(resp)))
        else:
            uid = self.__savemessage_searchforheader(imapobj, headername, headervalue)
            if (uid == 0):
                self.ui.debug('imap', 'savemessage:    attempt    to    get    new    UID    UID    failed.    Search    headers    manually.')
                uid = self.__savemessage_fetchheaders(imapobj, headername, headervalue)
                self.ui.warn(('savemessage:    Searching    mails    for    new    Message-ID    failed.    Could    not    determine    new    UID    on    %s.' % self.getname()))
    finally:
        if imapobj:
            self.imapserver.releaseconnection(imapobj)
    if uid:
        self.messagelist[uid] = self.msglist_item_initializer(uid)
        self.messagelist[uid]['flags'] = flags
    self.ui.debug('imap', ('savemessage:    returning    new    UID    %d' % uid))
    return uid
