Solved

Create CommuniGate Pro User Accounts from VB 2008 application

Posted on 2009-03-30
2
436 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now