SMTP Email: Difference between revisions

From DataFlex Wiki
Jump to navigationJump to search
No edit summary
minor tidy up, categorisation
Line 1: Line 1:
=='''Creating a COM Interface for SMTP Email'''==
'''SMTP''' or '''Simple Mail Transfer Protocol''' is a method of sending emails, and is commonly used as an alternative to sending emails via MAPI.  SMTP is not natively supported in [[Dataflex]] but can easily be achieved through an [[ActiveX]] control.


'''''Why SMTP?'''''
==Creating a COM Interface for SMTP Email==


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.
===Why SMTP?===


'''''How 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.


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.
===How SMTP?===


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.
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 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.


'''''Dll Package Creation'''''
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.
This is actually the easy part, believe it or not.  We will install the dll and create the Com interface.
Line 22: Line 24:
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.


'''''The View'''''
===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:
Create a simple one-view test application.  On the view you will need the following "form" controls and name the form objects as indicated:
Line 35: Line 37:
In addition, take the following steps
In addition, take the following steps


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


Line 52: Line 54:
   Set peAutoCreate to acNoAutoCreate
   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.   
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'''''
===Sending the email===


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


   String sAttach sName
   String sAttach sName
Line 85: Line 87:
   If (not(iRC)) send stop_Box "Email failed."  
   If (not(iRC)) send stop_Box "Email failed."  


'''''SMTP Host'''''
===SMTP Host===


To determine who your SMTP host is, do the following
To determine who your SMTP host is, do the following
Line 95: Line 97:
# On the “Server” tab use the entry from “Outgoing mail (SMTP)”
# On the “Server” tab use the entry from “Outgoing mail (SMTP)”


'''''Operation'''''
===Operation===


Fill in the fields and click “Send. You will need valid email addresses for the “from” and “to” and the attachment is optional.
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 addressee's “name” could be added to the form.
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 addressee's ''name'' could be added to the form.


'''''In Conclusion'''''
===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.
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.


[[Category: Active X]]
[[Category: ActiveX]]
[[Category: How To]]
[[Category: How To]]

Revision as of 23:15, 22 November 2007

SMTP or Simple Mail Transfer Protocol is a method of sending emails, and is commonly used as an alternative to sending emails via MAPI. SMTP is not natively supported in Dataflex but can easily be achieved through an ActiveX control.

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 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

And then 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. Add the create statement just below the object declaration.

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
 Boolean bIsComObjectCreated
 
 Get IsComObjectCreated of oComMailSender to bIsComObjectCreated
 
 If (not(bIsComObjectCreated)) Begin
     Send CreateComObject to oComMailSender
 End
 
 Send ComReset to oComMailSender
 
 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
 If (not(iRC)) send stop_Box "Email failed." 

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 addressee's 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.