Mailenable API AJAX调用

时间:2013-11-30 15:30:58

标签: api mail-server

我试图了解MailEnable API的工作原理。 到目前为止,我只能查看AJAX文档并获得LOGIN命令。就像:

AJAXRequest('LOGIN','Username='user'&Password='pass', false);

该命令返回一个xml字符串,如

<BASEELEMENT SCHEMA="VALUE" METHOD="LOGIN"><RETURNVALUE>1</RETURNVALUE></BASEELEMENT>

运行良好,除了LOGIN命令之外的任何东西(例如LOG-OFF或LIST-MESSAGES)都会给我一个超时错误,如

<BASEELEMENT SCHEMA="TIMEOUT" METHOD="LOGOUT"></BASEELEMENT>

我用于LOG-OFF和LIST-MESSAGES的命令就是这些,它们都给我以下错误。

AJAXRequest('LIST-MESSAGES','Folder=/Inbox', false);
AJAXRequest('LOG-OFF','ID=', false);

我正在使用that链接中的示例文件。我只是无法理解我是否遗漏了某些东西,或者这些例子和文件是不是最新的或有问题或是什么?

谢谢!

(我找不到“mailenable”标签来标记这个问题。遗憾的是没有人在stackoverflow之前标记mailenable,mailenable论坛就像一个坟墓:S)

1 个答案:

答案 0 :(得分:2)

根据评论,我发布了一封自编的Mail Anable邮件Api

Imports System
Imports System.Web
Imports MailEnable.Administration
Imports QueryStringModule
Imports System.Xml

Public Class MailApi : Implements IHttpHandler

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
        Return False
    End Get
End Property

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim doc As XmlDocument = GetXmlToShow(context)

    context.Response.ContentType = "test/xml"

    context.Response.ContentEncoding = System.Text.Encoding.UTF32 
    context.Response.Expires = -1
    context.Response.Cache.SetAllowResponseInBrowserHistory(True)

    doc.Save(context.Response.Output)

End Sub

Private Function GetXmlToShow(context As HttpContext) As XmlDocument
    Dim Result As String = ""
    Dim doc As New XmlDocument

    Try
        Dim query As String = QueryStringModule.Decrypt(context.Request.QueryString("val").Replace(" ", "+"))

        Dim PostOffice As String = HttpUtility.ParseQueryString(query).Get("PostOffice")
        Dim Domain As String = HttpUtility.ParseQueryString(query).Get("Domain")
        Dim Username As String = HttpUtility.ParseQueryString(query).Get("Username")
        Dim Password As String = HttpUtility.ParseQueryString(query).Get("Password")
        Dim MailBoxQuota As Integer = CInt(Val(HttpUtility.ParseQueryString(query).Get("MailBoxQuota")))
        If MailBoxQuota = 0 Then
            MailBoxQuota = 102400
        End If

        Dim act As String = HttpUtility.ParseQueryString(query).Get("Action")

        Select Case act
            Case "Create"
                Result = fnCreateMailAccount(Username, PostOffice)
            Case "Delete"

            Case "Update"

            Case "GetAll"
                Result = fnGetAll(Username, PostOffice)
            Case "GetToday"

            Case "GetFromTo"

            Case "GetUnread"
                Result=fnGetUnread(Username,PostOffice)
            Case "GetRead"

            Case "GetUnreadCount"
                Result = fnGetUnreadCount(Username, PostOffice)
            Case "GetTotalCount"
                Result = fnGetTotalCount(Username, PostOffice)
            Case Else
                Result = "<result status=""Err""><reason>Invaid action!</reason></result>"
        End Select

        doc.LoadXml(Result)
    Catch ex As Exception
        Result = "<result status=""Err""><reason>" + ex.Message + "</reason></result>"
        doc.LoadXml(Result)
    End Try
    Return doc 
End Function

Private Function fnGetUnread(Username As String, Postoffice As String) As String
    Dim XMLFile As String = ""
    Try
        If Len(Username) <= 0 Or Len(Postoffice) <= 0 Then
            Throw New ArgumentException("Invalid Parameters!")
        Else
            Dim oMailbox As New MailEnable.Administration.Mailbox
            oMailbox.Postoffice = Postoffice
            oMailbox.MailboxName = Username
            oMailbox.Status = 1

            If oMailbox.GetMailbox() = 1 Then
                Dim APIResult As Long
                Dim oStore As New MailEnable.Store
                APIResult = oStore.StoreFolder_Open(Postoffice, Username, "\Inbox", 0, 1)
                If oStore.StoreFolder_FindFirstItem() = 1 Then
                    Do
                        If oStore.StoreItem_GetProperty("PR_ME_READ").ToString = "0" Or oStore.StoreItem_GetProperty("PR_ME_READ").ToString = "" Then

                            Dim WrapperStart As String = "<message>"

                            Dim ItemId As String = "<itemid>" + oStore.StoreItem_GetProperty("ME_ITEM_ID").ToString + "</itemid>"
                            Dim MessageDate As String = "<dated>" + Left(oStore.StoreItem_GetProperty("PR_ME_MESSAGEDATE").ToString, 10) + " " + Right(oStore.StoreItem_GetProperty("PR_ME_MESSAGEDATE").ToString, 8) + "</dated>"
                            Dim Attachments As String = "<attachments>" + oStore.StoreItem_GetProperty("PR_ME_ATTACHMENTS").ToString + "</attachments>"
                            Dim From As String = "<from>" + oStore.StoreItem_GetProperty("PR_ME_FROM").ToString.Replace("<", """").Replace(">", """") + "</from>"
                            Dim Subject As String = "<subject>" + oStore.StoreItem_GetProperty("PR_SUBJECT").Replace("<", """").Replace(">", """").ToString + "</subject>"
                            Dim Size As String = "<size>" + oStore.StoreItem_GetProperty("PR_ME_SIZE").ToString + "</size>"
                            Dim Status As String = "<status>" + oStore.StoreItem_GetProperty("PR_ME_FLAGSTATUS").ToString + "</status>"

                            Dim WrapperEnd As String = "</message>"

                            XMLFile = XMLFile + WrapperStart +ItemId + MessageDate + Attachments + From + Subject + Size + Status + WrapperEnd
                        End If
                    Loop While (oStore.StoreFolder_FindNextItem() = 1)
                    XMLFile = "<messages>" + XMLFile + "</messages>"
                End If
                APIResult = oStore.StoreFolder_FindClose()
                APIResult = oStore.StoreFolder_Close()
            Else
                Throw New ArgumentException("Invalid Mailbox!")
            End If
        End If
        XMLFile = "<result status=""Success"">" + XMLFile + "</result>"
    Catch ex As Exception
        XMLFile = "<result status=""Err""><reason>" + ex.Message + "</reason></result>"
    End Try
    Return XMLFile
End Function

Private Function fnGetAll(Username As String, Postoffice As String) As String
    Dim XMLFile As String = ""
    Try
        If Len(Username) <= 0 Or Len(Postoffice) <= 0 Then
            Throw New ArgumentException("Invalid Parameters!")
        Else
            Dim oMailbox As New MailEnable.Administration.Mailbox
            oMailbox.Postoffice = Postoffice
            oMailbox.MailboxName = Username
            oMailbox.Status = 1

            If oMailbox.GetMailbox() = 1 Then
                Dim APIResult As Long
                Dim oStore As New MailEnable.Store
                APIResult = oStore.StoreFolder_Open(Postoffice, Username, "\Sent Items", 0, 1)
                If oStore.StoreFolder_FindFirstItem() = 1 Then
                    Do
                        Dim WrapperStart As String = "<message>"

                        Dim ItemId As String="<itemid>"+oStore.StoreItem_GetProperty("ME_ITEM_ID").ToString +"</itemid>"
                        Dim MessageDate As String = "<dated>" + Left(oStore.StoreItem_GetProperty("PR_ME_MESSAGEDATE").ToString, 10) + " " + Right(oStore.StoreItem_GetProperty("PR_ME_MESSAGEDATE").ToString, 8) + "</dated>"
                        Dim Attachments As String = "<attachments>" + oStore.StoreItem_GetProperty("PR_ME_ATTACHMENTS").ToString + "</attachments>"
                        Dim From As String = "<from>" + oStore.StoreItem_GetProperty("PR_ME_FROM").ToString.Replace("<", """").Replace(">", """") + "</from>"
                        Dim Subject As String = "<subject>" + oStore.StoreItem_GetProperty("PR_SUBJECT").Replace("<", """").Replace(">", """").ToString + "</subject>"
                        Dim Size As String = "<size>" + oStore.StoreItem_GetProperty("PR_ME_SIZE").ToString + "</size>"
                        Dim Status As String = "<status>" + oStore.StoreItem_GetProperty("PR_ME_FLAGSTATUS").ToString + "</status>"

                        Dim WrapperEnd As String = "</message>"

                        XMLFile = XMLFile + WrapperStart + ItemId + MessageDate + Attachments + From + Subject + Size + Status + WrapperEnd
                    Loop While (oStore.StoreFolder_FindNextItem() = 1)
                    XMLFile = "<messages>" + XMLFile + "</messages>"
                End If
                APIResult = oStore.StoreFolder_FindClose()
                APIResult = oStore.StoreFolder_Close()
            Else
                Throw New ArgumentException("Invalid Mailbox!")
            End If
        End If
        XMLFile = "<result status=""Success"">" + XMLFile + "</result>"
    Catch ex As Exception
        XMLFile = "<result status=""Err""><reason>" + ex.Message + "</reason></result>"
    End Try
    Return XMLFile
End Function

Private Function fnGetTotalCount(Username As String, Postoffice As String) As String
    Dim XMLFile As String = ""
    Dim c As Integer = 0
    Try
        If Len(Username) <= 0 Or Len(Postoffice) <= 0 Then
            Throw New ArgumentException("Invalid Parameters!")
        Else
            Dim oMailbox As New MailEnable.Administration.Mailbox
            oMailbox.Postoffice = Postoffice
            oMailbox.MailboxName = Username
            oMailbox.Status = 1

            If oMailbox.GetMailbox() = 1 Then
                Dim APIResult As Long
                Dim oStore As New MailEnable.Store
                APIResult = oStore.StoreFolder_Open(Postoffice, Username, "\Inbox", 0, 1)
                If oStore.StoreFolder_FindFirstItem() = 1 Then
                    Do
                        c = c + 1
                    Loop While (oStore.StoreFolder_FindNextItem() = 1)
                    XMLFile = "<count>" + c.ToString + "</count>"
                End If
                APIResult = oStore.StoreFolder_FindClose()
                APIResult = oStore.StoreFolder_Close()
            Else
                Throw New ArgumentException("Invalid Mailbox!")
            End If
        End If
        XMLFile = "<result status=""Success"">" + XMLFile + "</result>"
    Catch ex As Exception
        XMLFile = "<result status=""Err""><reason>" + ex.Message + "</reason></result>"
    End Try
    Return XMLFile
End Function

Private Function fnGetUnreadCount(Username As String, Postoffice As String) As String
    Dim XMLFile As String = ""
    Dim c As Integer = 0
    Try
        If Len(Username) <= 0 Or Len(Postoffice) <= 0 Then
            Throw New ArgumentException("Invalid Parameters!")
        Else
            Dim oMailbox As New MailEnable.Administration.Mailbox
            oMailbox.Postoffice = Postoffice
            oMailbox.MailboxName = Username
            oMailbox.Status = 1

            If oMailbox.GetMailbox() = 1 Then
                Dim APIResult As Long
                Dim oStore As New MailEnable.Store
                APIResult = oStore.StoreFolder_Open(Postoffice, Username, "\Inbox", 0, 1)
                If oStore.StoreFolder_FindFirstItem() = 1 Then
                    Do
                        If oStore.StoreItem_GetProperty("PR_ME_READ").ToString = "0" Or oStore.StoreItem_GetProperty("PR_ME_READ").ToString = "" Then
                            c = c + 1
                        End If
                    Loop While (oStore.StoreFolder_FindNextItem() = 1)
                    XMLFile = "<count>" + c.ToString + "</count>"
                End If
                APIResult = oStore.StoreFolder_FindClose()
                APIResult = oStore.StoreFolder_Close()
            Else
                Throw New ArgumentException("Invalid Mailbox!")
            End If
        End If
        XMLFile = "<result status=""Success"">" + XMLFile + "</result>"
    Catch ex As Exception
        XMLFile = "<result status=""Err""><reason>" + ex.Message + "</reason></result>"
    End Try
    Return XMLFile
End Function

Private Function fnCreateMailAccount(Username As String, Postoffice As String) As String
    Dim XMLFile As String = ""
    Dim MailBoxQuota As Long = 102400
    Try
        If Len(Username) > 0 And Len(Postoffice) > 0 Then

            Dim oPostoffice As New MailEnable.Administration.Postoffice
            Dim oDomain As New MailEnable.Administration.Domain

            oPostoffice.Account = Postoffice
            oPostoffice.Name = Postoffice
            oPostoffice.Host = Postoffice
            oPostoffice.Status = 1
            If oPostoffice.GetPostoffice <> 1 Then
                CreatePostoffice(Postoffice, New Guid().ToString.Substring(20))
            End If

            Dim oLogin As New MailEnable.Administration.Login
            oLogin.Account = Postoffice
            oLogin.LastAttempt = -1
            oLogin.LastSuccessfulLogin = -1
            oLogin.LoginAttempts = -1
            oLogin.Password = ""
            oLogin.Rights = ""
            oLogin.Status = -1
            oLogin.UserName = Username & "@" & Postoffice
            If oLogin.GetLogin <> 1 Then
                oLogin.LastAttempt = 0
                oLogin.LastSuccessfulLogin = 0
                oLogin.LoginAttempts = 0
                oLogin.Password = Right(Guid.NewGuid().ToString, 16)
                oLogin.Rights = "USER"
                oLogin.Status = 1

                'Create Login
                If oLogin.AddLogin = 1 Then

                End If

                oLogin = Nothing

                Dim oMailbox As New MailEnable.Administration.Mailbox
                oMailbox.Postoffice = Postoffice
                oMailbox.MailboxName = Username
                oMailbox.RedirectAddress = ""
                oMailbox.RedirectStatus = 0
                oMailbox.Size = 0
                oMailbox.Limit = MailBoxQuota
                oMailbox.Status = 1

                'Create Mailbox
                If oMailbox.AddMailbox = 1 Then
                    '
                    ' Mailbox was added - What could go wrong!
                    '
                End If

                oMailbox = Nothing
                ' Finally, we need to assign address map(s) for the mailbox

                Dim oAddressMap As New MailEnable.Administration.AddressMap
                oAddressMap.Account = Postoffice
                oAddressMap.DestinationAddress = "[SF:" & Postoffice & "/" & Username & "]"
                oAddressMap.SourceAddress = "[SMTP:" & Username & "@" & Postoffice & "]"
                oAddressMap.Scope = 0
                If oAddressMap.AddAddressMap = 1 Then
                    '
                    ' Address Map was added too - What could go wrong!
                    '
                End If
                oAddressMap = Nothing
                XMLFile = "<result status=""Success""></result>"
            Else
                Throw New ArgumentException("Account already exists!")
            End If
        Else
            Throw New ArgumentException("Invalid Parameters!")
        End If
    Catch ex As Exception
        XMLFile = "<result status=""Err""><reason>" + ex.Message + "</reason></result>"
    End Try
    Return XMLFile
End Function

Function CreatePostoffice(sPostoffice As String, sPassword As String) As String
    Dim oPostOffice As New MailEnable.Administration.Postoffice
    Dim oMailbox As New MailEnable.Administration.Mailbox
    Dim oLogin As New MailEnable.Administration.Login
    Dim lResult As Long
    CreatePostoffice = False
    If Len(sPostoffice) > 0 And Len(sPassword) > 0 Then
        oPostOffice.Account = sPostoffice
        oPostOffice.Name = sPostoffice
        oPostOffice.Status = 1
        lResult = oPostOffice.AddPostoffice
        If (lResult = 1) Then
            oMailbox.Postoffice = sPostoffice
            oMailbox.Limit = -1
            oMailbox.MailboxName = "Postmaster"
            oMailbox.RedirectAddress = ""
            oMailbox.RedirectStatus = 0
            oMailbox.Status = 1
            lResult = oMailbox.AddMailbox
            If (lResult = 1) Then
                oLogin.Account = sPostoffice
                oLogin.Description = "Postmaster Mailbox"
                oLogin.Password = sPassword
                oLogin.Rights = "ADMIN"
                oLogin.Status = 1
                oLogin.UserName = "Postmaster@" & sPostoffice
                lResult = oLogin.AddLogin
                If (lResult = 1) Then
                    CreatePostoffice = True
                End If
            End If
        End If
    End If
    oPostoffice = Nothing
    oMailbox = Nothing
    oLogin = Nothing
End Function


End Class

虽然这不是完整的答案,但您可以开始研究它。这样。
将此ashx代码放在MailEnable webmail文件夹中,并从您的应用程序请求 QueryStringModule可供下载here
QueryStringModule在您的应用程序中用于加密http请求并在MailEnable应用程序中解密 出于安全原因,仅允许在MailEnable Web应用程序中使用您的应用程序IP。