Page 1 of 1

ASCII Character Validation

Posted: Thu Jun 18, 2015 8:32 pm
by M45HY
Hello,

I currently have a program that emails our clients with a random word (reference). The reference is unique and can contain letters, numbers and the following symbols: * $ ! & % ? ^

The letters, numbers and symbols are held in an array, and when the reference needs to be created, it uses the random() method to pick a position from the array and then adds the character at that position to the reference.

Once this has been done, the reference is then emailed but I am getting the error message that the reference contians an non-ASCII character.

I have put in validation, such as:

Code: Select all

vars smtp_connection : CnSmtpConnection; begin smtp_connection.name := str_mailServer; smtp_connection.sendFrom := str_mailFrom; smtp_connection.sendTo := str_mailTo; smtp_connection.subject := str_mailSubject; smtp_connection.contentType := "html"; smtp_connection.message:= "<html>"; smtp_connection.message:= smtp_connection.essage & str_uniqueReference; smtp_connection.message:= smtp_connection.essage & str_additionalMessage; smtp_connection.message:= smtp_connection.essage & "</html>"; smtp_connection.send; if smtp_connection.result.bitAnd(CnSmtpConnection.Err_Non_ASCII_Character) <> null then // Create a New Reference and Recall this Method using the New Reference as str_uniqueReference endif; if global.isValidObject(smtp_connection) then app.doWindowEvents(10); endif;
But this does not seem to be working. Just to confirm that I would check once the email has been sent as thats when I would get something in smtp_connection.result? If not, could someone suggest where I would place it?

Furthermore, is it not possible to have the validation in the method where the reference is being created? For example, when the reference is created, validate it there and then and if it fails, create another one and keep doing this check until it is valid?

Thanks,

Omash

Re: ASCII Character Validation

Posted: Thu Jun 18, 2015 9:16 pm
by murray
Hi Omash,

This reply is not about your random character business, but I do agree with you that you should select and validate the characters first. Don't call send() until you have confirmed that the content is valid.

This is more about the design of your example. I think it may not be working correctly at the moment.

It is important to realise that the call to smtp_connection.send() is asynchronous.
When it has been executed the email has not been sent. It has merely initiated the sending in another process and immediately returned.
Therefore the email is not "sent" until some undetermined later time. You cannot test the result until you know it has been sent.

The app.doWindowEvents() does nothing much in your example (being last), although it could have been taken out of context.
Jade use app.doWindowEvents(time_delay) in their example scripts so that the script waits a suitable time for the email to send.

The correct way to implement the checking of the result is to use Jade notifications (refer to my previous posting on this matter to you).
A correctly configured notification will generate a callback event when the email is sent, or raise an exception on error.

I believe the Jade documentation for the CnSmtpConnection class details how to set up the notifications (I haven't read it in a while).
However, using the app.doWindowEvents(time_delay) will be sufficient for experimenting in a script, as long as the delay is placed between the send() and the test of the result. app.doWindowEvents() allows the Windows message queue to be processed so that the smtp_connection state can be updated.

Jade include many examples of using CnSmtpConnection in their documenation of the class, but I have seen first hand the misunderstanding of the app.doWindowEvents() with the result that it was copied into server code that had proper notifications set up and so did not need it at all. In fact it just introduces an unnecessary time delay (and hard to trace bugs). app.doWindowEvents() has some nasty side effects, so it should be used sparingly and with great caution.

Re: ASCII Character Validation

Posted: Fri Jun 19, 2015 2:47 am
by ghosttie
Can we see the code that generates the random string?

Re: ASCII Character Validation

Posted: Fri Jun 19, 2015 8:45 am
by BeeJay
Some of those characters may need encoding to be valid inside an HTML formatted e-mail. One example, is that & may need to be encoded as & for an HTML e-mail.

However, one thing that I always do, to avoid issues around 'non-Ascii' character errors, especially when I have limited control over the e-mail content, is to set the encodingType to Base64 as per the following:

Code: Select all

smtp_connection.encodingType := CnSmtpConnection.Base64;
You definitely need to take on board Murray's comments regarding not using app.doWindowEvents() and instead simply let your call stack go idle and await the notification delivery from the 'async' send that will tell you when the e-mail send has been attempted and either succeeded or failed.

Cheers,
BeeJay.

Re: ASCII Character Validation

Posted: Fri Jun 19, 2015 9:46 am
by JohnP
Note that the CnSmtpConnection class is documented in the JADECare Start documentation, not the JADE documentation.

Re: ASCII Character Validation

Posted: Fri Jun 19, 2015 8:47 pm
by murray
Note that the CnSmtpConnection class is documented in the JADECare Start documentation, not the JADE documentation.
Sorry, John. I only meant that in a general way. ;) In a previous thread I had suggested Omash look it up in the "CardSchema" documentation, so I presumed he would already be familiar with it. I usually post these things from home, so usually just doing it off the top of my head.

Re: ASCII Character Validation

Posted: Mon Jun 22, 2015 9:54 am
by JohnP
That's ok - just clarifying in case a third party looks at this six months from now...

Re: ASCII Character Validation

Posted: Fri Jul 24, 2015 3:06 am
by M45HY
Hi guys,

Sorry for the late reply but just wanted to thank everyone - I was able to resolve the issue with the help from you guys.

I looked at the JADECare Start documentation and found the section that affected me and was able to fix it (I think so far anyway :D )

Thanks everyone,
Omash