SMTP Email: Difference between revisions

From DataFlex Wiki
Jump to navigationJump to search
Bworsley (talk | contribs)
No edit summary
Bworsley (talk | contribs)
No edit summary
Line 15: Line 15:
This is actually the easy part, believe it or not.  We will install the dll and create the Com interface.
This is actually the easy part, believe it or not.  We will install the dll and create the Com interface.


1) Download the aspemail dll and register it per their instructions.
# Download the aspemail dll and register it per their instructions.
 
# In the VDF Studio (12.1 was used), go to File/New/Other and select the “Class” tab.
2) In the VDF Studio (12.1 was used), go to File/New/Other and select the “Class” tab.
# Select Import COM Automation  
3) Select Import COM Automation  
# Select “Persists Software AspEmail 5.x” and click “Ok”
4) Select “Persists Software AspEmail 5.x” and click “Ok”


This will create a VDF package named aspemail.pkg that can be used without modification in your application.
This will create a VDF package named aspemail.pkg that can be used without modification in your application.

Revision as of 14:09, 20 November 2007

Creating a Com Interface for SMTP Email

Why SMTP?

MAPI email works fine from within VDF, but if using Outlook or Outlook Express, it can be quite annoying, especially if the email is sent automatically from a background program like a service. The problem is that it wants to be sure that the email being sent is not from a Trojan horse or some other hidden program and will pop up a message asking if the message is being sent by “you”. When being sent from a service, that’s a killer, as you will not know why your application just halts. This can supposedly be turned off if using Outlook Express as your mail client, but not with Outlook. There are some third party products that can help with this, but using SMTP seems to just be much easier.

How SMTP?

It turns out that SMTP is very easy to implement, generally an activex dll will do the trick. Having looked around at the available offerings, aspemail.dll, available for free at http://www.aspemail.com was selected. The aspemail dll is free for the commonly used email functions, but costs for “premium” functionality, go to their site and see if it will work for you.

The second reason that it was chosen is because it’s asp compatible and so will work when called from either a Windows server or from an ASP page.

Dll Package Creation

This is actually the easy part, believe it or not. We will install the dll and create the Com interface.

  1. Download the aspemail dll and register it per their instructions.
  2. In the VDF Studio (12.1 was used), go to File/New/Other and select the “Class” tab.
  3. Select Import COM Automation
  4. Select “Persists Software AspEmail 5.x” and click “Ok”

This will create a VDF package named aspemail.pkg that can be used without modification in your application.

The View

Create a simple one-view test application. On the view you will need the following form controls and name the form objects as indicated: · The SMTP host address - oHost · From address - oFromAddr · To address - oToAddr · Subject - oSubject · Message (body) - oMessage · Attachment - oAttachment

In addition, take the following steps

1. Add “Use aspemail.pkg” to the top of your code, outside the view itself 2. Add one button to the view and change the label to “Send” 3. Add the following object to the top of the view, anywhere inside or outside of the view itself:

   Object oComMailSender is a cComMailSender
   End_Object

4. We need to create the object

   Send CreateComObject to oComMailSender

The COM object must be created prior to it’s being used. One of the common problems is once the code is complete you get an error about the object being activated before it’s instantiated.

In aspemail.pkg at the bottom in class cComMailSender you will find a line as follows:

Set peAutoCreate to acNoAutoCreate

When an activex component is created, this is set to AutoCreate, but when a COM Automation component is created, it’s set to NoAutoCreate. This could be changed in aspemail.pkg or simply add the line as shown above.

Sending the email

Inside the OnClick event in the “Send” button added above, add the following code:

           String sAttach sName

Integer iRC

           Set ComUserName of oComMailSender to "YourEmailLoginID"
           Set ComPassword of oComMailSender to "YourEmailLoginPassword"
           Set ComHost of oComMailSender to (Value(oHost))
           Set ComFrom of oComMailSender to (Value(oFromAddr))
           Set ComSubject of oComMailSender to (Value(oSubject))
           Set ComBody of oComMailSender to (Value(oMessage))
           Send ComAddAddress to oComMailSender (Value(oToAddr))  sName
           Get Value of oAttachment to sAttach
           If (trim(sAttach) <> "") Begin 
               Send ComAddAttachment to oComMailSender sAttach
           End
           
           Get ComSend of oComMailSender "" to iRC

SMTP Host

To determine who your SMTP host is, do the following 1. Bring up your mail client. For Outlook or Outlook Express… 2. Click on Tools 3. Click Accounts 4. Select the account for your provider 5. Click Properties 6. On the “Server” tab use the entry from “Outgoing mail (SMTP)”

Operation

Fill in the fields and click “Send.” You will need valid email addresses for the “from” and “to” and the attachment is optional.

In the code above, notice the “sName” variable. A name is not required for the “To” address, but if not used, a null value must be sent to the ComAddAddress procedure to avoid an error. Another control for the “name” could be added to the form.

In Conclusion

Look in the aspemail.pkg and you will see a large list of functions and procedures that may be used with this dll. Remove the “com” from the front of the method name and the remaining name is the one that is used in the aspemail manual, so figuring out how things work is relatively easy.