How to call a Visual Basic ActiveX DLL to send mail (200150)



The information in this article applies to:

  • Collaboration Data Objects (CDO) 1.1
  • Collaboration Data Objects (CDO) 1.2
  • Collaboration Data Objects (CDO) 1.21

This article was previously published under Q200150

SUMMARY

This article shows how to write a Visual Basic ActiveX DLL to send an e-mail message using Collaboration Data Objects (1.1, 1.2, 1.21). 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 Microsoft CDO (1.1, 1.2, 1.21) Library.
  5. Place the following code in a class module in Project2.
    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;
    }
    					

Modification Type:MajorLast Reviewed:7/19/2005
Keywords:kbFAQ kbhowto kbMsg KB200150