HOWTO: Call a Visual Basic ActiveX DLL to Send Mail

ID: Q200150


The information in this article applies to:


SUMMARY

This article shows how to write a Visual Basic ActiveX DLL to send an e-mail message using Collaboration Data Objects (CDO). It also shows how to call this DLL from C code.


MORE INFORMATION

Use the following steps to create a Microsoft Visual Basic ActiveX DLL Project:

  1. Open a new Visual Basic ActiveX DLL Project.


  2. Save the Project as Project2 and the Class as Class1.


  3. Make sure that the Name property in the Properties section of the project is also Project2.


  4. Make sure your project has a reference to either Microsoft CDO 1.2 Library or Active Messaging Library.


  5. Place the following code in a class module in Project2.



  6. 
    Public Function sendmail(Recip As String, Subject As String)
       Dim osession As MAPI.Session
       Dim omessage As Message
       Dim oRecip As Recipient
       
       Set osession = CreateObject("MAPI.Session")
       osession.Logon
       Set omessage = osession.Outbox.Messages.Add
       omessage.Subject = Subject
       Set oRecip = omessage.Recipients.Add(Recip)
       oRecip.Resolve
       omessage.Text = "Testing"
       
       omessage.Send , True
       osession.Logoff
    End Function
     

    The following C code (Win 32 Application) is used to create and call the ActiveX DLL:
    
    // activexdll1.cpp : Defines the entry point for the application.
    // 
    
    #include "stdafx.h"
    #include <objbase.h>
    
    // Change to include your DLL
    
    #import "Project2.dll" no_namespace
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    
    // Initialize COM
    
       CoInitialize(NULL);
    
       //  Change to the name of the person 
       //  you wish to send the message
    
       _variant_t name ("alias");
       _variant_t subject ("Testing ActiveX DLL");
       
       // Create an instance of the ActiveX object
       // Where Class1 is the class module of your Project2
     
       _Class1Ptr oDLL("Project2.Class1");
    
       oDLL->sendmail(&name.bstrVal,&subject.bstrVal);
       oDLL = NULL;
       CoUninitialize();
      
       return 0;
    } 

Additional query words: kbDSupport kbCDO kbCDO110 kbCDO120 kbMsg kbActMsg


Keywords          : kbActMsg kbCDO110 kbCDO120 kbMsg kbVBp kbVC kbGrpMsg 
Version           : WINDOWS:1.1,1.2
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 7, 1999