Solved

Create CommuniGate Pro User Accounts from VB 2008 application

Posted on 2009-03-30
2
447 Views
Last Modified: 2014-10-31
I'm trying to create a HelpDesk application in Visual Basic 2008 with user management functions including user creation for Active Directory, MySQL and (hopefully) CommuniGate Pro e-mail accounts.  I haven't started on any of these yet because I want to do my research first to make sure that all of this will be possible.  I've seen some code posts for the Active Directory part, and the MySQL piece should be easy enough.  The part I'm having trouble pinning down is the e-mail account creation.

From the research I've done so far, the only way I've found to connect to the CommuniGate server from my VB application is through a command-line interface using the TELNET command.  I'm still pretty new to programming in general, so I'm still not 100% certain how I'm going to do that exactly, but my bigger question is whether or not this is the most effective method of accomplishing the goal.

So, I suppose the reason I'm posting here is to find out if anyone out there has done anything similar, and if there are any suggestions on the best way to make this happen.  Thanks in advance for your assistance.
0
Comment
Question by:khufford19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
khufford19 earned 0 total points
ID: 24064772
After doing a lot of searching, I found a useful link on another EE thread that pointed me in the right direction.  Now I've come up with the following code.  This uses some user settings variables I setup in the project to help keep from hard-coding any login credentials into the program.  Additionally, this isn't "cleaned up" for final use, but this is what I threw together and tested just to see if I could get a connection to work.

This is only for the user creation, but the commands can be tweaked to create, delete, or modify users in a CommuniGate mail server, or any other mail server administration function.

Hope this is helpful to someone else out there looking for a way to automate user creation functions for a CommuniGate server.  Please feel free to ask if you have any questions about the details as it took me a lot of digging around to find this, and then a bit of effort to put it all into practice.
Imports System.Net.Sockets
Imports System.Text
 
Public Class CGServer
    Private TELNETCONN As New TcpClient("mail.mailserver.com", <portID>)
    Private TELNET As NetworkStream
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Message As String
        Dim Response As String
 
        If TELNETCONN.Connected Then
            TELNET = TELNETCONN.GetStream
            If TELNET.CanRead Then
                Message = GetResponse()
                Response = Microsoft.VisualBasic.Left(Message, 3)
                If Response <> "200" Then
                    MsgBox(Message)
                    CloseCon()
                    Exit Sub
                End If
                SendText("USER " & My.Settings.MailAdminUser.ToString & vbCrLf)
 
                Message = GetResponse()
                Response = Microsoft.VisualBasic.Left(Message, 3)
 
                If Response <> "300" Then
                    MsgBox(Message)
                    CloseCon()
                    Exit Sub
                End If
 
                SendText("PASS " & My.Settings.MailAdminPass.ToString & vbCrLf)
 
                Message = GetResponse()
                Response = Microsoft.VisualBasic.Left(Message, 3)
 
                If Response <> "200" Then
                    MsgBox(Message)
                    CloseCon()
                    Exit Sub
                End If
 
                SendText("CREATEACCOUNT " & NewUserName & vbCrLf)
 
                Message = GetResponse()
                Response = Microsoft.VisualBasic.Left(Message, 3)
 
                If Response <> "200" Then
                    MsgBox(Message)
                    CloseCon()
                    Exit Sub
                End If
 
                SendText("SETACCOUNTPASSWORD " & NewUserName & " PASSWORD " & TemporaryPassword & vbCrLf)
 
                Message = GetResponse()
                Response = Microsoft.VisualBasic.Left(Message, 3)
 
                If Response <> "200" Then
                    MsgBox(Message)
                    CloseCon()
                    Exit Sub
                End If
 
                TELNET.Close()
            Else
                MsgBox("Unable to connect")
            End If
        Else
            Debug.Print("Connection Failed")
        End If
 
        CloseCon()
 
    End Sub
 
    Private Sub SendText(ByVal strMsg As String)
        Dim sendBytes As [Byte]()
 
        If Not ExtendedASCIIEncode(strMsg, sendBytes) Then
            Err.Raise(vbObjectError + 1, "SendText", "Error converting the byte array.")
            Exit Sub
        End If
 
        TELNET.Write(sendBytes, 0, sendBytes.Length)
    End Sub
 
    Private Function ExtendedASCIIEncode(ByVal strMsg As String, ByRef arrByte() As Byte) As Boolean
        Dim i As Integer
 
        Try
            ReDim arrByte(strMsg.Length - 1)
            For i = 0 To strMsg.Length - 1
                arrByte(i) = CByte(Asc(strMsg.Substring(i, 1)))
            Next i
 
            ExtendedASCIIEncode = True
        Catch ex As Exception
            If i > 0 Then
                ReDim Preserve arrByte(i - 1)
            End If
            ExtendedASCIIEncode = False
        End Try
    End Function
 
    Private Function GetResponse() As String
        Dim Start As Double
        Dim Tmr As Double
        Dim bytes() As Byte
 
        Start = Now.TimeOfDay.TotalSeconds
 
        ReDim bytes(TELNETCONN.ReceiveBufferSize)
 
        While Not TELNET.DataAvailable
 
            Tmr = Now.TimeOfDay.TotalSeconds - Start
 
            Application.DoEvents()
 
            If Tmr > 60 Then
                GetResponse = "TIMEOUT!"
                Exit Function
            End If
        End While
 
        If TELNET.DataAvailable Then
            ' Returns the data received from the host to the console.
            TELNET.Read(bytes, 0, CInt(TELNETCONN.ReceiveBufferSize))
            GetResponse = Encoding.ASCII.GetString(bytes)
        Else
            GetResponse = "TIMEOUT!"
        End If
    End Function
 
    Private Sub CloseCon()
        If Not TELNETCONN Is Nothing Then
            TELNETCONN.Close()
        End If
        TELNETCONN = Nothing
    End Sub
End Class

Open in new window

0
 
LVL 2

Expert Comment

by:G_Hosa_Phat
ID: 40415812
Thank you for this post.  I'm working on tweaking a few things (including adding a test for whether or not the account exists), but this definitely gives me a jumping-off point, and seems to work great for a new project I'm working on so my IT Department can have a single application to register/deregister users.  Much appreciated.

I'm also working on creating a CommuniGate Pro class library for .NET that I can use for future implementations.  Of course, I've got a LOT of work to do as I've just started the project, but I hope that I can put together something really useful for any other CG users going forward.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question