PDA

View Full Version : ChilKat email OCX -VDF wrapper class



Torben Lund
13-Mar-2009, 03:41 AM
This is a VDF wrapper class that makes it very easy to use the ChilKat email component for sending and managing emails from VDF. It will work with both windows and webapp applications.

// Example 1 - most simple way to send an email:



// use cCKMail.pkg
// integer iRet
// get comQuickSend of oCKmail "" "mickey@disney.com" "Subject text" "Mail body text" to iRet
// send info_box (if(iRet=0,"Mail could not be sent!","Mail sent OK"))
//


// Example 2 - Send plain text email with file attachment:



// integer iRet
// string sRet
// handle hMail
// get CreateNewEmail of oCKmail to hMail
// if (hMail<>0) begin
// get comAddTo of hMail "Mickey Mouse" "mickey@disney.com" to iRet
// set comSubject of hMail to "Subject text"
// set comBody of hMail to "Mail body text"
// get ComAddFileAttachment of hMail "c:\files\myfile.zip" to sRet
// get sendEmail of hMail to iRet
// send destroy of hMail
// end
// send info_box (if(hMail=0 OR iRet=0,"Mail could not be sent!","Mail sent OK"))
//


// Example 3 - Send HTML email with embedded image and signature from a file:



// integer iRet
// string sRet sTxt
// handle hMail
// get CreateNewEmail of oCKmail to hMail
// if (hMail<>0) begin
// get comAddTo of hMail "" "mickey@disney.com" to iRet
// set ComSubject of hMail to "Subject text"
// send addHtmlText of hMail '<center><font face="Comic Sans MS" size=16 color=$FF0000>'
// send addHtmlText of hMail "My home<p>"
// send addImage of hMail "c:\images\disneyland.jpg") "MIDDLE"
// send addHtmlText of hMail "<p>Disneyland</font><br>Have You ever been there?</CENTER><p>"
// get ComLoadTextFileToUnicode of hMail "c:\files\signature.htm" "iso-8859-1" to sTxt
// send addHtmlText of hMail sTxt
// send constructHTMLbody of hMail
// get sendEmail of hMail to iRet
// send destroy of hMail
// end
// send info_box (if(hMail=0 OR iRet=0,"Mail could not be sent!","Mail sent OK"))
//


// Example 4 - Get a list of emails on your POP3 server:



// integer iCount iItem iRet
// handle hMailMan hMail hBundle
// variant vBundle vMail
// string sFrom sSubject
// datetime dtSent
// move oCKMail to hMailMan
// set ComMailHost of hMailMan to "mail.yourdomain.com"
// set comPopUserName of hMailMan to "yourPOP3username"
// set comPopPassword of hMailMan to "yourPOP3password"
// get create U_cCKmailBundle to hBundle
// get createNewEmail of hMailMan to hMail
// if (hMail<>0 and hBundle<>0) begin
// get ComGetAllHeaders of hMailMan 0 to vBundle
// set pvComObject of hBundle to vBundle
// get comMessageCount of hBundle to iCount
// if (iCount>0) begin
// decrement iCount
// send delete_data of oGrid1
// for iItem from 0 to iCount
// get comGetEmail of hBundle iItem to vMail
// set pvComObject of hMail to vMail
// get comFromName of hMail to sFrom
// if (sFrom="") get comFrom of hMail to sFrom
// get comSubject of hMail to sSubject
// get comLocalDate of hMail to dtSent
// send releaseComObject of hMail
// send add_item of oGrid1 msg_none sFrom
// send add_item of oGrid1 msg_none sSubject
// send add_item of oGrid1 msg_none dtSent
// loop
// end
// send releaseComObject of hBundle
// send destroy of hMail
// send destroy of hBundle
// end

Torben Lund
13-Mar-2009, 06:23 AM
I just realized that ChilKatSoft put out a new release (7.9.3) of the email control. You need to Import the ActiveX class again in the Studio (generating a new cComChilkatMailMan2.pkg) to make this work. At least I had to on my Vista box. The cckmail.pkg do not need to be changed, all I did was to change the comment at the top of the file, stating that Chilkat version 7.9.3 is required.

I have put a new set of files in the attached zip. If you use these files you don't need to import the class yourself, although it might be a good idea to do so everytime you install a new release of the ActiveX control, just to make sure to keep the wrapper class up to date.

sandhya
14-Mar-2009, 04:10 AM
Hi Torben,

yes, we are using Chilkat version 7.9.3. I will go through your code.

Thanks.

Ola Eldoy
12-May-2011, 06:25 AM
Torben, I encountered a problem when using your SendEmail function and the Chilkat email component. For the potential benefit of others, I am sharing my experience here.

I am using an SMTP server without user authentication, and subsequently, ComVerifySmtpLogin fails. However, ComSendMail works nonetheless. So I had to remove the ComVerifySmtpLogin(hObj)=1 check.

In other words, using ComSendMail without checking for ComVerifySmtpLogin made it possible for me to send emails.

Håkan Carlsson
13-Jan-2015, 07:57 AM
Hi,
I have installed ChilKat 9.5.0
I got the message "ActiveX component may not be properly installed or registered. Error code: 0x80040154.
It have been installed I can se it in the register.
What is wrong?

Hans van de Laar
13-Jan-2015, 08:00 AM
Have you been using the package as provided by Torben?
If so, the Chilkat 9.5.0 is a different version and you probably need to regenerate a new DF wrapper class for it.

Also make sure you have installed the 32-bits version of the chilkat dll.

Håkan Carlsson
13-Jan-2015, 01:21 PM
Yes I use the package from Torben. I have installed the 32-bits version
How do I regenerate a new DF wrapper class?

Bob Worsley
13-Jan-2015, 01:58 PM
1. File menu, New & Class
2. Select Com Automation
3. Look through the list for the Chilkat DLL you installed and if you can't find it browse to it
4. Generate the class file

Håkan Carlsson
13-Jan-2015, 03:23 PM
Thank you Bob,
It did it, thanks

Now I get an error "cComChilkatMailMan2 must be defined

Garret Mott
13-Jan-2015, 03:30 PM
I think you'll find that it's been replaced. I am using an older DLL & it's fine. If I remember correctly, Bob ran into this using a newer DLL & maybe he'll post back with what he did to fix it.

Bob Worsley
13-Jan-2015, 07:40 PM
I think you'll find that it's been replaced. I am using an older DLL & it's fine. If I remember correctly, Bob ran into this using a newer DLL & maybe he'll post back with what he did to fix it.What did I do? I didn't end up using Torben's code, couldn't make it work. Some guy on here gave me some code that I managed to figure out and get working...

Garret Mott
13-Jan-2015, 08:25 PM
Speaking as "some guy" - I thought you'd run into a problem with "cComChilkatMailMan2" in my code because of using a later DLL. Maybe I'm hallucinating?

Bob Worsley
13-Jan-2015, 09:00 PM
Maybe I'm hallucinating?Probably.

Seriously though, I had two problems that I had to figure out. Your code worked fine except that in my case I kept getting GPFs when I closed any program that had the Chilkat code in it. I had to add the following to the oChilKatMailMan2 object to fix it.


Procedure ReleaseComObject
// commented out forward send because it causes GPF when shutting down
//Forward Send ReleaseComObject
End_Procedure


The second problem was that when sending batch email the various Chilkat internal address arrays retained their previous addresses and each subsequent mailing sent to every accumulated address. Clearing with the following fixed that problem


send ComClearTo to hMailObj
send ComClearCC to hMailObj
etc...

And that could be because I'm using a later version than you. Other than that it worked great.

Michael Mullan
13-Jan-2015, 10:08 PM
Ding ! Point worsley! That points me in the fught right direction thanks.

Richard Hogg
13-Jan-2015, 10:10 PM
fught? pronounce that wrong and you could be in trouble :D

Garret Mott
13-Jan-2015, 10:27 PM
Adjoining keys & no proofreading can get one in trouble!

Todd Forsberg
14-Jan-2015, 12:13 AM
I think we have all "been there, done that" at some point in our lives... (but strictly only speaking for myself, LOL)

Håkan Carlsson
14-Jan-2015, 02:07 AM
I looked at ChilKat but the current version is 9.5 and I can't find any older DLL's.
Do you know where to find the older working DLL

Hans van de Laar
14-Jan-2015, 05:27 AM
Hi Hakan,

Yes, the cComChilkatMailMan2 has been replaced by the cComChilkatMailMan class. I've worked with several versions of the Chilkat mail component and they are all working well with DataFlex (like the other products of Chilkat).
When using the mail component I'm also using their ChilkatMht component to send emails based on HTML files/templates. If you are interested I can see if I can provide some sample code for the latest Chilkat dll.

Håkan Carlsson
14-Jan-2015, 05:34 AM
Yes Hans, that would be very kind.

Peter van Mil
14-Jan-2015, 06:34 AM
Hi Hans,

That's very interesting. I send HMTL mails based on templates (based on reading template files), but I didn't know that Chilkat has a component for it.

Could you send me a sample?

Bob Worsley
14-Jan-2015, 07:56 AM
Not to mention the sometimes infuriating iPhone thinking it knows better what you want to say than you do...

Michael, what did I say that got you on the right track? The crashes or the array clearing? Or something unrelated?

Hans van de Laar
14-Jan-2015, 09:48 AM
Here I've attached a workspace which demonstrates how to send emails with the latest version of the 32-bit chilkat activex (as now version 9.5.46).
In order to get this example working you have to complete the following steps:


- insert your Chilkat Unlock codes in the mail.pkg
- define your smtp settings in the config.ws in the key section [Mail]
* smtpserver
* smtpport
* smtpssl (values 1 or 0)
* smtplogin (values Y or N)
* smtpusername (if smtplogin = Y)
* smtppassword (if smtplogin = Y)


- define your from emailadres in the config.ws in the key section [Mail]
Compile the SendMail.src and run

Because of the size of the workspace attachment I've omitted the chilkat dll. Put your ChilkatAx-9.5.0-win32.dll in the programs folder also. It is referenced by its manifest file (thanks Nils) so you don't have to register it.

This examples demonstrates the sending of an html based e-mail. It uses a html template loaded from appHtml\mailtemplates folder. This example also demonstrates how you can do some kind of mail merge .
In this sample it dynamically fills the name of the application sending this email (nothing fancy), but you will get the idea.

I can imagine you can use a completely different approach of sending emails from within your application but I hope I got you started with this example.

Hans van de Laar
14-Jan-2015, 09:48 AM
Hakan,

I've just posted a sample.

Hans van de Laar
14-Jan-2015, 09:49 AM
Peter,

You can find the example in this thread.

Peter van Mil
14-Jan-2015, 10:14 AM
Hi Hans,

Thanks. I will look at it soon!

Michael Mullan
14-Jan-2015, 12:19 PM
The Array Clearing. I've a customer getting nuts about sending email to the wrong people. I was fairly sure he's just doing it wrong, but this might be an actual issue.

MM.

Bob Worsley
14-Jan-2015, 01:49 PM
but this might be an actual issueit sure was for me!

Michael Mullan
14-Jan-2015, 02:37 PM
we've all got that customer where you can't ind the issue past the hysteria? Yep. Him.

Håkan Carlsson
15-Jan-2015, 12:06 PM
Thank you for all the help.
I have struggling with it for a while, but so far no luck
I have this in config.ws
[Mail]
smtpserver=SMTP.asfaltsystem.se
smtpport=2525
smtpusername=info@asfaltsystem.se
smtpssl=1
smtplogin=Y
smtppassword=The password
mailfrom=info@asfaltsystem.se
mailfromname=DataFlex mail sample program
mailtemplate=sampletemplate.html

Garret Mott
15-Jan-2015, 01:29 PM
Won't those all need quotes - except smtpport?


mailfromname= "DataFlex mail sample program"

etc.

Todd Forsberg
15-Jan-2015, 01:35 PM
No. "config.ws" is essentially an .ini file. All text to the right of the = is treated as strings.

Håkan Carlsson
15-Jan-2015, 02:00 PM
I tried but no luck,
Attachment chilkat log

8572

Bob Worsley
15-Jan-2015, 08:23 PM
If you're getting this far you're pretty close to getting it working.

Here are the only settings I have:


Move oChilkatMailMan2 to hSmtpObj
Move oChilkatEmail2 to hMailObj

Get InitMail of hSmtpObj to iIsOk

If (iIsOk) Begin
Set ComSmtpHost of hSmtpObj to (trim(SysFrt.SMTP_HOST))
Set ComSmtpPort of hSmtpObj to "25"
Set ComSmtpSsl of hSmtpObj to 0
Set ComSmtpUsername of hSmtpObj to "" // authentication isn't required
Set ComSmtpPassword of hSmtpObj to ""
Set ComFromAddress of hMailObj to (trim(SysFrt.SMTP_FROM_ADDR))
Set ComFromName of hMailObj to (trim(Sysfrt.SMTP_From_Name))

// all the recipient address, subject and message stuff...

Get pvComObject of hMailObj to vNewMail
Get ComSendEmail of hSmtpObj vNewMail to iRC

Håkan Carlsson
16-Jan-2015, 02:54 AM
Hmm... I can't get it to work and I don't know how to move on.

Garret Mott
16-Jan-2015, 06:30 AM
Can you post the code you are using for the actual send of the email? The error you posted showed that it wasn't connecting to SMTP/your ISP correctly because of no user and/or password.

Håkan Carlsson
16-Jan-2015, 07:30 AM
Ok here it is,


Use Windows.pkg
Use DFClient.pkg
Use Mail.pkg
Deferred_View Activate_oMail for ;
Object oMail is a dbView
Set Border_Style to Border_Thick
Set Size to 91 231
Set Location to 2 2
Set Label to "Mail"
Object oMailManager is a cMailManager

Procedure onAddReceiverToMail Handle hoMailObj
String sEmailTo snameTo
Integer iVal
//
Get Value of oNamereceiverForm to sNameTo
Get Value of oEmailreceiverForm to sEmailTo
Get ComAddTo of hoMailObj sNameTo sEmailTo to iVal
End_Procedure
End_Object
Object oNamereceiverForm is a Form
Set Size to 13 150
Set Location to 10 75
Set Label to "Name receiver:"
Set Label_Col_Offset to 70
End_Object

Object oEmailreceiverForm is a Form
Set Size to 13 150
Set Location to 25 75
Set Label to "Email adress receiver:"
Set Label_Col_Offset to 70
End_Object
Object oTextBox1 is a TextBox
Set Size to 10 33
Set Location to 54 86
Set Label to ''
End_Object
Object oAttachmentCheckBox is a CheckBox
Set Size to 10 111
Set Location to 41 75
Set Label to 'Include attachment'
End_Object
Procedure SampleMail
Handle hoDialog
String sAttachment sFileName sError
String[] aSelectedFiles
Boolean bOpen
Integer iRetVal
//
If (Checked_State(oAttachmentCheckBox) = True) Begin
Get Create (RefClass(OpenDialog)) to hoDialog
If (hoDialog) Begin
Set MultiSelect_State of hoDialog to False
Get Show_Dialog of hoDialog to bOpen
If bOpen Begin
Get File_Title of hoDialog to sFileName
Set Label of oTextBox1 to sFileName
Get Selected_Files of hoDialog to aSelectedFiles
Move aSelectedFiles[0] to sAttachment
End
Send Destroy of hoDialog
End
End
Get SendMailMessage of oMailManager sAttachment to iRetval
If (iRetVal = 1) Send Info_Box "E-mail successfully send" "Chilkat mail example"
Else Begin
Get psError of oMailManager to sError
Send UserError sError "Chilkat mail example"
End
End_Procedure

Object oButton1 is a Button
Set Location to 70 175
Set Label to 'Send Email'
// fires when the button is clicked
Procedure OnClick
Send SampleMail
End_Procedure
End_Object

Cd_End_Object

Garret Mott
16-Jan-2015, 12:32 PM
Hi -

What's in "SendMailMessage"? Or maybe all of oMailManager if the actual code for sending is outside SendMailMessage.

That's where the email is actually sent, right?

Håkan Carlsson
17-Jan-2015, 03:50 AM
Yes I think so, all of the code is provided by Hans van de Laar earlier in this thread

Hans van de Laar
19-Jan-2015, 04:50 AM
Hakan,

Is this the complete log file?

Håkan Carlsson
19-Jan-2015, 06:46 AM
Yes it is, is there a way to check if SMT is working

Hans van de Laar
19-Jan-2015, 07:28 AM
Try replacing the SendMail function in the mail.pkg with



Function SendMail Handle hoMail Returns Integer
Integer lretVal
Boolean bOK
Variant vMail
String sErr
//
If (hoMail > 0) Begin
Get IsComObjectCreated of hoMail to bOK
If (bOK = True) Begin
//
Get pvComObject of hoMail to vMail
If (isNullComObject(vMail) = 0) Begin
Get ComVerifySmtpConnection to lretVal <==== check this lretval, it should return 1
If (lretVal = False) Set psError to "TCP/IP connection with SMTP server not successfull"
Else Begin
Get ComVerifySmtpLogin to lretVal <==== check this lretval, it should return 1
If (lretVal = False) Set psError to "SMTP Login error"
Get comSendEmail vMail to lretVal
If (lretVal = 0) Begin
Get ComLastErrorText to sErr
Set psError to sErr
End
End
End
Function_Return lretVal
End
Else Send UserError "E-mail component has not been properly installed!\nCannot send email" "Program Error"
End
Function_Return lretVal
End_Function


and replace in Mail.vw the snippet
With


Get SendMailMessage of oMailManager sAttachment to iRetval
If (iRetVal = 1) Send Info_Box "E-mail successfully send" "Chilkat mail example"
Else Begin
Get psError of oMailManager to sError
Direct_Output "c:\temp\chilkat.err"
Writeln sError
Close_Output
End


if any errors it should be in the "c:\temp\chilkat.err".

Håkan Carlsson
19-Jan-2015, 12:48 PM
Thank you Hans
Error "TCP/IP connection with SMTP server not successfull"
I must check the SMTP, but it works in the mail account

Michael Mullan
19-Jan-2015, 01:56 PM
If you are using an SSL port then you must set the pott port, and the UseSSl



Set ComSmtpPort of oMailMan to BlatSend.SenderPort
If (BlatSend.SenderPort > 110 ) Set ComSmtpSsl of oMailMan to 1



My email system file is called BlatSend

Bob Worsley
19-Jan-2015, 02:18 PM
Set the pott? Haven't used one of those lately... Oh, did you mean port? ;)

Michael Mullan
19-Jan-2015, 03:57 PM
Funny boy.

RKuijten
27-Oct-2016, 04:29 AM
I have bought a Chilkat 1-Developer Email License and i have downloaded your example but in mail.pkg 2 codes are needed and i have only received 1. It only works if i fill in ".." for both (using code for only C_CKUnlockCode_MailManager does not work)

Do i need a seperate code for C_CKUnlockCode_MHT?

Thanks in advance.

regards,

Ronald

RKuijten
27-Oct-2016, 04:59 AM
I have now downloaded the Dll from the website (the same DLL is 8,245KB instead of 6,405) and now it doesn;lt matter what C_CKUnlockCode_MailManager i fill in the email is sent? (i have disabled using templates so no C_CKUnlockCode_MHT is needed for this test)

how do i know my code is working?

Garret Mott
27-Oct-2016, 08:30 AM
I do it like this. If (iSuccess = 1) then the email sent OK.



Get ComSendEmail of hoMailman vNewMail to iSuccess
//Check for any errors & save record as appropriate
If (iSuccess<>1) Begin
Get ComLastErrorText of hoMailman to sText
//Then examine sText for what may have gone wrong
End