Search Persits Software Knowledge Base

 

Article PS121023186

Using .NET components in classic ASP

Problem Description

This article explains how to use Persits Software native .NET components AspPDF.NET and AspEmail.NET in a classic ASP environment (or any other environment supporting COM.)

Solution

To use AspEmail.NET and AspPDF.NET from a classic ASP application, or any other environment supporting COM, please follow these steps:

1. Upgrade if necessary

AspEmail.NET is COM-visible as of version 5.3.0.3127.

AspPDF.NET is COM-visible as of version 2.1.0.15816.

If you are using an older version, please upgrade to the latest version, upgrading is free.

2. Use regasm to register the component assembly under COM

Open the .NET Command prompt.

To register AspEmail.NET, run the following command:

regasm c:\path\Persits.Email.dll /codebase

To register AspPDF.NET, run the following command:

regasm c:\path\Persits.PDF.dll /codebase

path is the full path to the component assembly.

regasm should respond with the message

Types registered successfully.

If the response is

RegAsm: warning RA000 : No types were registered.

it means the version of the component assembly is old (see step 1 above.)

Note that on a 64-bit version of Windows, there are two copies of the .NET Command prompt -- the 32-bit and 64-bit versions. We recommend that you perform the registration process described above from both prompts - this will make the registration visible to ASP applications running under both the 32-bit and 64-bit modes of IIS.

3. Try to create the object

If step 2 was successful, you should now be able to create an instance of AspEmail.NET's MailSender object, and AspPDF.NET's PdfManager object, as if they were COM objects. The syntax is:

AspEmail.NET:
Set Mail = Server.CreateObject("Persits.Email.MailSender")

AspPDF.NET:
Set PDF = Server.CreateObject("Persits.PDF.PdfManager")

An attempt to call Server.CreateObject may result in the following error:

Server object error 'ASP 0177 : 8000ffff'
Server.CreateObject Failed

To fix this error, follow the procedure described in the Microsoft KB article 945701, as follows:

Run regedit.

Locate and then click one of the following registry subkeys, depending on the version of Windows that is running on your computer. If your computer is running a 32-bit version of Windows, click the following registry subkey:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\ FEATURE_IGNORE_ZONES_INITIALIZATION_FAILURE_KB945701

Programs that are 32-bit and run on a 64-bit version of Windows operate in WOW64 mode. These programs access registry keys and values that are stored in a different registry subkey. If you are running 32-bit programs on a 64-bit version of Windows, click the following registry subkey:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\ FEATURE_IGNORE_ZONES_INITIALIZATION_FAILURE_KB945701

Note If the FEATURE_IGNORE_ZONES_INITIALIZATION_FAILURE_KB945701 subkey does not exist, you must manually create it. Right-click FEATURE_IGNORE_ZONES_INITIALIZATION_FAILURE_KB945701, point to New, and then click DWORD Value. Type w3wp.exe to name the new registry entry, and then press ENTER. Right-click w3wp.exe, and then click Modify. In the Value data box, type 1, and then click OK.

4. Use suffixes to call overloaded methods

.NET allows method overloading but COM does not. To resolve this conflict and provide a unique COM-visible name for each overloaded .NET method, the 2nd, 3rd, etc. overloaded methods are assigned suffixes _2, _3, etc.

For example, AspPDF.NET's PdfManager object overloads the method OpenDocument as follows:

public PdfDocument OpenDocument(string Path)
public PdfDocument OpenDocument(string Path, string Password)
public PdfDocument OpenDocument(byte[] Blob)
public PdfDocument OpenDocument(byte[] Blob, string Password)

To call these 4 methods from an ASP environment, you must use the syntax:

Set Doc = PDF.OpenDocument(path)

Set Doc = PDF.OpenDocument_2(path, password)

Set Doc = PDF.OpenDocument_3(blob)

Set Doc = PDF.OpenDocument_4(blob, password)

5. Use Item() for all this[] properties

In the AspPDF.NET collections such as PdfFonts and PdfPages, the parameterized property this[...] should be replaced with the name Item (or Item_2, etc.) when called from an ASP environment. For example, the C# code

PdfPage page = objDoc.Pages[1];

becomes ASP code

Set page = objDoc.Pages.Item(1)

Or, the C# code

PdfFont font = objDoc.Fonts["Courier"];

becomes ASP code

Set font = objDoc.Fonts.Item_2("Courier")

6. Using registration keys

Under ASP.NET, the registration keys for AspEmail.NET and AspPDF.NET are placed in the application's web.config file. In a classic ASP environment, there are no web.config files, so it is recommended that the registration key be specified right in the script via the RegKey property, for example:

Set PDF = Server.CreateObject("Persits.PDF.PdfManager")
PDF.RegKey = "GYlbv4BJLz......Vl7xrVj2uF+BiKq"

Alternatively, you need to create a .config file with the same name, and in the same folder, as the calling process, and put the key in it. We have added the property ConfigPath to both AspPDF.NET and AspEmail.NET which returns the full path of the .config file that the component is looking for, for its registration key.

On Windows 7, the ConfigPath property usually returns "c:\windows\system32\inetsrv\w3wp.exe.config", and on Windows XP, "c:\windows\system32\dllhost.exe.config".

The registration key is to be put in the .config file as follows:

<configuration>
   <appSettings>
      <add key="AspPDF_RegKey" value="GYlbc3gt5Th3...3zNJ5kD9" />

      <add key="AspEmail_RegKey" value="fH4pAk3bZF...svTWklpI" />
   </appSettings>
   ...
</configuration>

Secure Mail Code Samples for AspEmail.NET

As of Version 5.3.0.3218, AspEmail.NET can be used to send secure email in classic ASP. The advantage of using AspEmail.NET over AspEmail is that the latter also requires AspEncrypt to send secure email, while AspEmail.NET is completely self-sufficient.

The following code samples demonstrate how AspEmail.NET can be used to send secure mail in a classic ASP environment:

Set Mail = Server.CreateObject("Persits.Email.MailSender")
Mail.RegKey = "fH4p.....EoN3x6BggQ" ' registration key
Mail.Host = "smtp.myisp.com"
Mail.From = "me@mydomain.com"
Mail.AddAddress_2 "them@theirdomain.com"
Mail.Subject = "Secure Mail"
Mail.Body = "This is a test."


'Encrypted email
Mail.SendEncrypted_3 "c:\path\cert.cer"


'Signed email
Mail.LogonUser "domain", "username", "password"
Mail.SendSigned_2 "c:\cert2.pfx", "pwd"


'Signed and encrypted email
Mail.LogonUser "domain", "username", "password"
Mail.SendSignedAndEncrypted_3 "c:\path\cert2.pfx", "pwd", "c:\path\cert.cer"
'DKIM-signed email
Mail.LogonUser "domain", "username", "password"
Mail.SendCertified_2 "c:\path\cert2.pfx", "pwd", "mydomain.com", "myselector", 3

Created: 10/23/2012 2:20:42 PM
Last Modified: 10/17/2013 12:08:36 PM

Copyright © Persits Software, Inc. 1998 - 2017
For technical support, write to support@persits.com.