HOWTO: Send Mail to Remote Exchange Server in ASP with VB DLL

ID: Q176914

The information in this article applies to:

SUMMARY

This article describes how to create a Web application that uses an Hypertext Markup Language (HTML) page, an Active Server Page (ASP) and a Visual Basic ActiveX .dll to send mail when Microsoft Exchange Server and Microsoft Internet Information Server are installed on separate machines.

MORE INFORMATION

To use an ActiveX .dll in an ASP page to send mail when the Internet Information Server (IIS) and Exchange servers are not on the same computer, you need to set up Windows NT and IIS security so the user can log onto the Exchange server.

This article provides a step-by-step method that includes all source code required.

PART 1

Create an NT user group that you grant access to the web.

1. On the IIS machine, using "User Manager for Domains" from the

   Administrative Tools program group, select User and "New Local Group."

2. Type a Group Name (for example, WebBrowsers) and a Description.

3. Click the Add button, and select where you want to list the names from,

   "Add Individual Users" or Groups. Click OK to return to the main "User
   Manager" window.

4. Select the WebBrowsers group in the bottom part of the User Manager
   window.

5. From the User Manager menu, click "Policies", "User Rights."

6. Next to Rights, select "Log on Locally."

7. Click Add and use the "List Names From" drop-down list box to list names

   from MACHINENAME.

8. Select the WebBrowsers group and click Add.

9. Click OK to clear the "User Rights Policy" dialog box.

You have now created a user group that you will eventually give rights to the web you will create later in this article.

PART 2

Create the Visual Basic .dll and install to the internet server.

1. Create a new Visual Basic ActiveX .dll. Set the Project Properties as

   follows:

      General tab:

          Project Type:          ActiveX .dll
          Startup Object:        (None)
          Project Name:          CDOMail
          Project Description:   CDO .dll to send mail
          Unattended Execution:  Checked

      Make tab:

          Title:                 CDOMail
          All other settings as desired.

      Compile tab:

          All settings as desired.

      Component tab:

        Remote Server:          Unchecked
        Version Compatibility:  Project Compatibility (Change to
                                Binary Compatibility after compiling
                                to maintain client-server compatibility.)

2. Set the Class properties as follows:

      Name:                       clsSendMail
      Instancing                  5 - MultiUse

3. Reference Olemsg32.dll version 5.0.1458.49 or later or Cdo.dll version
   5.5.1664.0 or later.

4. Paste the following code into the class module. This code defines a
   class with common messaging properties and contains a method that uses
   those properties to log onto the user's Exchange Server mailbox, then
   creates and sends mail.

      Option Explicit
      'Requires a reference to OLEMSG32.DLL version 5.0.1458.49 or later
      'or CDO.DLL version 5.5.1664.0 or later.
      'Local variable(s) to hold property value(s).

      Public Recipient As Variant
      Public Subject As Variant
      Public Text As Variant
      Public Mailbox As Variant
      Public Server As Variant
      Public Sub Send()
        Dim objSession As MAPI.Session
        Dim objOutbox As Folder
        Dim objNewMessage As Message
        Dim objRecipients As Recipients
        Dim objOneRecip As Recipient

        Set objSession = New MAPI.Session
        objSession.Logon profileinfo:=Server & _
                                    vbLf & _
                                    Mailbox, _
                                    nomail:=True

        Set objOutbox = objSession.Outbox
        Set objNewMessage = objOutbox.Messages.Add
        Set objRecipients = objNewMessage.Recipients
        Set objOneRecip = objRecipients.Add
         With objOneRecip
          .Name = Recipient
          .Type = ActMsgTo
          .Resolve ' Get MAPI to determine complete E-mail address.
         End With
        With objNewMessage
          .Subject = Subject
          .Text = Text
          .Send
        End With
      End Sub

5. Compile to a .dll.

6. Create setup files using the Setup Wizard.

7. Install the .dll to your IIS computer.

PART 3

Start Visual InterDev and create a new web on your IIS machine.

 1. On the Microsoft Visual InterDev or Microsoft Developer Studio menu
    select File, and then select New.

 2. Select "Web Project Wizard" from the Projects tab.

 3. Next, select the location for your Web Server. The location will be the
    Web server you intend users to log on to.

 4. Give the Project a name (for example, KBCDOMail).

 5. Select the "Create New Workspace" option and click OK.

 6. Select the server name from the drop-down list. Click Next. The Wizard
    will contact the Web server. Depending on how you have authentication
    set up, you may be asked to supply a password.

 7. Select "Create a New Web" and use KBCDOMail for the name. You do not
    have to select the "Enable full text searching for pages in this web"
    check box but it would be convenient if you plan to add this capability
    to the Web. Click Finish. The Wizard will create the web and some web
    files for you.

 8. Select Project, Web Permissions from the Visual InterDev menu.

 9. On the Settings tab, select "Use unique permissions for this Web", and
    then click Apply.

10. On the Groups tab, use the "Obtain list from" drop-down list box to
   select the IIS machine.

11. Add the WebBrowsers group and select the "Browse this Web" option.
   Click OK until all the dialog boxes are cleared.

PART 4

Add an HTML page to your project.

1. Select File, then New from the menu.

2. Select HTML Page from the Files tab. Provide a name (for example

   Kbcdomail.HTM) and click OK.

3. Select "Kbcdomail.htm" and paste in the following code, replacing
   anything that is there.

      <HTML>
      <HEAD>
      <META NAME="GENERATOR" Content="Microsoft Developer Studio">
      <META HTTP-EQUIV="Content-Type" content="text/html;
      charset=iso-8859-1">
      <TITLE>Document Title</TITLE>
      </HEAD>
      <BODY BGCOLOR=White>

      <H1><CENTER>Test</CENTER></H1>
      <P><H2><CENTER>Enter Valid Logon and Exchange
      Server</CENTER></H2>
      <P>
      <FORM ACTION="KBCDOMail.asp" METHOD="POST" NAME="frmUserData"
      OnSubmit="return ValidateData()">
      <TABLE>
      <TR>
      <TD>Mailbox:</TD>
      <TD><INPUT TYPE=TEXT VALUE="Mailbox" NAME="txtMailbox"
      ALIGN=LEFT></TD>
      <TR>
      <TD>Exchange Server:</TD>
      <TD><INPUT TYPE=TEXT VALUE="Exchange Server" NAME="txtServer"
      ALIGN=LEFT></TD>
      <TR>
      <TD>Recipient:</TD>
      <TD><INPUT TYPE=TEXT VALUE="Recipient" NAME="txtRecipient"
      ALIGN=LEFT></TD>
      <TR>
      <TD>Subject:</TD>
      <TD><INPUT TYPE=TEXT VALUE="This is the subject."
      NAME="txtSubject" ALIGN=LEFT></TD>
      <TR>
      <TD>Message:</TD>
      <TD><TEXTAREA NAME="Name" ROWS=10 COLS=40
      WRAP=Physical></TEXTAREA></TD>
      </TR>
      </TABLE>
      <INPUT TYPE=HIDDEN VALUE="None" NAME="hidSource"><BR><P>
      <INPUT TYPE=SUBMIT VALUE="OK" NAME="btnSubmit" ALIGN=RIGHT>
      <INPUT TYPE="RESET" VALUE="Reset">
      </FORM>
      </BODY>
      </HTML>

PART 5

Add an .asp file to your project.

1. Select File, then New from the menu.

2. Select "Active Server Page" from the Files tab. Provide a name (for

   example, Kbcdomail.asp) and click OK.

3. Select "Kbcdomail.asp" and paste in the following code replacing,
   anything that is there.

      <%@ LANGUAGE="VBSCRIPT" %>
      <%
      If Request.ServerVariables("LOGON_USER") = "" Then
      Response.Status = "401 access denied"
      Response.End
      End If
      %>

      <%
      Response.Write("Logon User = " &
      Request.ServerVariables("LOGON_USER") &
      "<br>")
      bstrProfileInfo = Request.Form("txtServer") & vbLf &
      Request.Form("txtMailbox")
      response.write bstrProfileInfo %><br><%
      %>

      <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 3.2//EN">
      <HTML>
      <HEAD>
      </HEAD>
      <BODY>
      <%
      dim MyMail
      Response.Write("About to CreateObject CDOMail.clsSendMail.<br>")
      Set myMail = CreateObject ("CDOMail.clsSendMail")
      Response.Write("CDOMail.clsSendMail Created<br>")
      myMail.Server = Request.Form("txtServer")
      myMail.Mailbox = Request.Form("txtMailbox")
      myMail.Recipient = Request.Form("txtRecipient")
      myMail.Subject = Request.Form("txtSubject")  & now
      myMail.Text=Request.Form("txtMessage")
      Response.Write("myMail.Server= " & myMail.Server & "<br>")
      Response.Write("myMail.Mailbox= " & myMail.Mailbox & "<br>")
      Response.Write("myMail.Recipient= " & myMail.Recipient & "<br>")
      Response.Write("myMail.Subject= " & myMail.Subject & "<br>")
      Response.Write("myMail.Text= " & myMail.Text & "<br>")
      myMail.Send
      set myMail = Nothing
      Response.Write("Message sent! Check your inbox.<br>")
      %>
      </BODY>
      </HTML>

PART 6

Make sure you have a mail CLIENT on the IIS machine.

If you do not have a mail CLIENT on the IIS machine the user's Web browser reports this error:

      CDOMail error '800a01ad'
      ActiveX component can't create object.

Installing either Outlook 8.01 or later or installing the Exchange Client with the typical setup solves this problem.

PART 7

Test the application.

When you load Kbcdomail.htm into your browser, you will be prompted to identify yourself. Provide your DOMAIN\UserName and password. Fill in the text boxes on the page and click OK.

You should see the .asp file load and write out who the logged on user is, the profile it is using, and information about the Visual Basic Class that you previously installed on the IIS machine.

REFERENCES

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)

Additional query words: ActMsg CDO Active Messaging Collaboration Data

Keywords          : kbASP100 kbCDO110 kbCDO120 kbCDO121 kbMsg kbVBp500 kbVisID100 kbGrpMsg 
Version           : WINDOWS:1.0,1.1,1.2,5.0, 1.21; WINNT:1.0b,3.0,4.0
Platform          : WINDOWS winnt
Issue type        : kbhowto

Last Reviewed: April 8, 1999