Bitspan Knowledge Base Article - 100001

How to Automate Bitspan Spamity™ Agent from Visual Basic

 

 

SUMMARY

This article provides an overview of programming Bitspan Spamity™ Agent using Automation from another program.

MORE INFORMATION

Bitspan provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Bitspan support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Bitspan Certified Partner or the Bitspan fee-based consulting line at (604) 926-3242. For more information about Bitspan Certified Partners, please visit the following Bitspan Web site:

http://www.bitspan.com/partner/referral/

For more information about the support options that are available and about how to contact Bitspan, visit the following Bitspan Web site:

http://www.bitspan.com/

Automation (formerly OLE Automation) allows one program to control another program by either issuing commands or retrieving information programmatically. You can use the code examples in this article in Microsoft Word 97, Microsoft Excel 97, Microsoft Visual Basic, or any other program that supports Automation.

 

Sample Code

Visual Basic

You can use either "early" or "late" binding to start an Automation session. Late binding uses either the GetObject or CreateObject function to initialize Bitspan Spamity™ Agent. For example, the following code sets an object to the Bitspan Spamity™ Agent program, which is the highest level object in the Bitspan Spamity™ Agent object model. All Automation code must first define an Bitspan Spamity™ Agent object in order to access any of the other Bitspan Spamity™ Agent objects below that.


The sample code below allows you to test the functionality of Spamity™ Agent from Microsoft Visual Basic.



Create a module called "ModMain" and copy and paste the code below in it.

' This is a part of the Bitspan Spamity API.
' Copyright (C) 2001-2005 Bitspan Network Inc.
' All rights reserved.
'
' This source code is only intended as a supplement to the
' Bitspan Spamity API Reference Guide and related Electronic
' documentation provided with the Bitspan Spamity API.
' See these sources for detailed information regarding the
' Bitspan Spamity API.

'To run the application, as is, in debug mode copy "Spamity.dll" to "c:\"
Declare Function UpdateDefinitions Lib "c:\Spamity.dll" (ByVal nThreadMode As Integer) As Long
Declare Function IsSpam Lib "c:\Spamity.dll" (ByVal sBody As String, ByVal sTo As String, ByVal sCC As String, ByVal sFrom As String, ByVal sReplyTo As String, ByVal sIP As String, ByRef numDefs As Long) As Long
Declare Function SetKey Lib "c:\Spamity.dll" (ByVal sKey As String) As Long
Declare Function Terminate Lib "c:\Spamity.dll" () As Integer


Create a form called "frmMain" and copy and paste the code below in it.

Option Explicit

' This is a part of the Bitspan Spamity API.
' Copyright (C) 2001-2005 Bitspan Network Inc.
' All rights reserved.
'
' This source code is only intended as a supplement to the
' Bitspan Spamity API Reference Guide and related Electronic
' documentation provided with the Bitspan Spamity API.
' See these sources for detailed information regarding the
' Bitspan Spamity API.

'Initialize Spamity component
Private Sub cmdInit_Click()
    Me.Caption = "Spamity sample (This might take a while. Please wait...)"
    DoEvents
    
    'Set Spamity license key
    
    ' IMPORTANT NOTE:
    '   YOU NEED A LICENSE KEY FROM BITSPAN TO GET THE LATEST DEFINITIONS
    '   YOU CAN GET A TRIAL KEY BY VISITING http://www.bitspan.com/spamity
    
    SetKey "SPAMITY LICENSE KEY GOES HERE"
    
    ' The first time UpdateDefinitions function is called, it may take a few
    ' minutes for it to return (it depends on the number of definitions that need to
    ' be downloaded.  However on subsequent calls, it should take shorter time since
    ' the definitions are automatically cached and stored on the locally.
    '
    ' After that you can call IsSpam function against your emails.
    ' Updates will be downloaded automatically and will not affect
    ' the IsSpam processing time.
    
    ' Note:
    ' When you call UpdateDefinitions and pass the value "0" as its parameter,
    ' the component updates definitions once, and you need to update definitions
    ' every once in a while by calling the function again.
    '
    ' You cannot pass "1" parameter in VB, since multi-threading in not supported
    
    UpdateDefinitions 0
    
    Me.Caption = "Bitspan Spamity - Spam Email Detection Sample VB Code"
End Sub

'Is Spam button clicked
Private Sub cmdIsSpam_Click()

    Dim Result As Long
    Dim numDefs As Long
    numDefs = 0
    
    ' REQUIRED
    ' txtBody.Text = the body of email
    '
    ' OPTIONAL
    ' txtTo.Text        email address to which the email was sent
    ' txtCC.Text        email address to which the email was carbon copied
    ' txtFrom.Text      email address from which the email was sent
    ' txtReplyTo.Text   Rely-To email address
    ' txtIP.Text        IP address from which the email was sent
    ' numDefs.Text      Spamity Definition ID
    
    'Now Check whether the email is spam or not by using the values provided
    Result = IsSpam(txtBody.Text, txtTo.Text, txtCC.Text, txtFrom.Text, txtReplyTo.Text, txtIP.Text, numDefs)
    
    'Check result of IsSpam function call
    If (Result <> 0) Then
        MsgBox "Spam"
    Else
        MsgBox "May be legitimate"
    End If
    
End Sub

'Form closing
Private Sub Form_Unload(Cancel As Integer)
    'For graceful termination/clean up
    Terminate
End Sub

 

REFERENCES

For more information about creating solutions with Bitspan Spamityä, please see the following articles in the Bitspan Knowledge Base:

100002 How to Automated Bitspan Spamity™ from Visual C++
100003 How to Automated Bitspan Spamity™ from SOAP/HTTP

The information in this article applies to:

Last Reviewed:

1/06/2005 (1.0)

Keywords:

kbdtacode kbhowto kbProgramming kbusage KB100002