|By Bruce Armstrong||
|August 13, 2006 03:30 PM EDT||
I used to implement SMTP for PowerBuilder using a PBNI class that used P.J. Naughter's CPJNSMTPConnection MFC class to do the heavy lifting.
Well, .NET 2.0 changes all that. One of the new features of .NET 2.0 is the introduction of a SMTPClient class. One of the features of .NET since it was introduced is the ability of non-.NET applications to call .NET components through the use of a COM Callable Wrapper (CCW).
I'm going to demonstrate how to use a CCW to use the new SMTPClient class in .NET 2.0. The first thing we'll need to do is create our own .NET component using the new class. We'll start by creating a class library using Visual Studio.NET 2005 (see Figure 1). Note that although I haven't tried it personally, I believe everything that I'm doing is possible with Visual C# Express.
Once we've created the new solution, we should rename the C# file that was automatically created from "Class1" to something a bit more meaningful. We can do that in the Solution Explorer, and we'll be prompted to let Visual Studio apply the change throughout the code (see Figure 2).
We also have to make a couple of adjustments to the project properties. Right-click on the project and select Properties. On the Application tab, we need to select the "Assembly Information..." command button to access the Assembly Information editor.
Once there, we have to check the "Make assembly COM-visible" option. You can also provide values for some of the other assembly properties at this time (see Figure 3).
We're going to be adding our assembly to the Global Assembly Cache (GAC), and we need to have a strong name for it to do that. So we go to the Signing tab of the Project properties and check the "Sign the assembly" option. Then click the dropdown list box under "Choose a strong name key file" and select the "
Using this dialog automatically creates the strong name file and adds it as a reference to the project.
At some point after creating our assembly we're going to have to:
- Add the assembly to the GAC using the GACUTIL utility and
- Create a REG file with the COM entries for the registry using the REGASM utility
Here I'm dynamically creating environmental variables to point to the location of the .NET Framework 2.0 directory (where the REGASM utility is found) and the .NET 2.0 SDK bin directory (where the GACUTIL utility is found). It's not necessary, but it's helpful if you're calling more than one utility in those directories. It also helps makes the later command lines more legible. Note that the call to GACUTIL has quotes around the command line and the TargetPath argument. That's because the path to the .NET 2.0 SDK directory and the TargetPath have embedded spaces and the command will fail if the quotes aren't used.
The output of the REGASM utility is a .REG file, which we'll need to run (double-click) to add the entries into the registry.
So far we've set up a class that can be used as a CCW, but it doesn't have any code it in yet. Go back to the class file and add "using System.NET.Mail" to the using section so we can reference the new SMTPClient classes without having to qualify them fully. Then add the code in Listing 3 to implement the class. Compile the new class, and then (assuming there were no errors) run the REG file to add the COM information to the registry.
We probably want to test out the class to make sure it does what we're expecting. We can do that in Visual Studio.NET 2005 (or Visual C# Express) by creating another project to test the assembly. Create another solution, this time a standard Windows Application. We'll need to add a reference to the assembly, so select Project->Add Reference from the menu, choose the Browse tab in the Add Reference dialog, and then navigate to the assembly in the build directory from the previous project (see Figure 6).
Now drop a CommandButton on the default form, and then double-click on it to bring up the editor for its Click event. Add the code from Listing 1, inserting your own name, e-mail address, and e-mail server. Run the project, and when you click on the button in the form, you should send yourself e-mail using the assembly.
Because of the CCW, it's as easy (perhaps even easier) to call the assembly from PowerBuilder. Just add a CommandButton to a window in PowerBuilder, and add the code from Listing 2 to its clicked event. That's it! You're done. You can use the same technique to call other .NET components. And you'll probably want to expand on this example to add support for authentication to the e-mail server, attachments, multiple recipients, etc. However, the supplied sample should get you well started.
- Where Are RIA Technologies Headed in 2008?
- PowerBuilder History - How Did It Evolve?
- Creation and Consumption of Web Services with PowerBuilder
- Cloud People: A Who's Who of Cloud Computing
- DDDW Tips and Tricks
- Cloud Expo 2011 East To Attract 10,000 Delegates and 200 Exhibitors
- Working with SOA & Web Services in PowerBuilder
- Dynamically Creating DataWindow Objects
- OLE - Extending the Capabilities of PowerBuilder
- DataWindow.NET How To: Data Entry Form