Page 1 of 1

Using cnSMTPConnection to send an email

Posted: Fri Mar 16, 2012 11:13 pm
by Rich Cassell
Hi all,

In our system we use the cnSMTPConnection class in a number of places to send "automatic" emails when something occurs. This has always worked sufficiently for the emails that we send, however, we now have a requirement to send an email where the message length is greater than 1000 characters, and it turns out that this is a problem...

The error i am getting is "100007 Undefined message number - CnSmtpConnection::validateASCIIMessage One of the message lines exceeds 1000 characters". I can't find any documentation on this class (though i am sure it must be somewhere) so i'm unsure as to how to force the email to be spread over more than one message line.

Has anybody else used this method to send an email of greater length? Or, is there a better way for me to achieve this. The email sending should need no manual intervention at all.

The bare-bones code i'm using for this is:

Code: Select all

vars conn : CnSmtpConnection; begin app.cnActivateKarmaControl(Cn_Fat_Client, null, null); create conn; self.beginNotification(conn, CnSmtpConnection.Completion_Event_Type, Response_Cancel, 0); conn.name := exchange server name; conn.sendFrom := emailFrom; conn.contentType := "html"; conn.sendTo := emailTo; conn.subject := "My email"; conn.message := Message needs to be > 1000 chars long conn.send; while not conn.completed do app.doWindowEvents(10); endwhile; epilog delete conn; end;
Any help would be greatly appreciated.

Rich

Re: Using cnSMTPConnection to send an email

Posted: Sat Mar 17, 2012 10:09 pm
by murray
I have seen this error (or similar) recently on that class when the body of the message was very long without any included new-line sequences (CrLf). I think this was because it was a long XML string or data dump. Normally, lines of text should be much shorter, and terminated with A CrLf sequence.

RFC 2822 states:
"There are two limits that this standard places on the number of characters in a line. Each line of characters MUST be no more than 998 characters, and SHOULD be no more than 78 characters, excluding the CRLF."

Re: Using cnSMTPConnection to send an email

Posted: Sun Mar 18, 2012 1:12 am
by ghosttie
Whenever we set CnSmtpConnection::message we do this:

Code: Select all

smtp.message := sMsg.enforceLineMaxLength(1000);
where String::enforceLineMaxLength is:

Code: Select all

enforceLineMaxLength(pI_LineMaxLength : Integer) : String; vars sRet, sLine : String; vlsa : VLSA; iLine : Integer; begin vlsa := split(CrLf); foreach iLine in 1 to vlsa.size do sLine := vlsa[iLine]; while sLine.length > pI_LineMaxLength do sRet := sRet & sLine[1 : pI_LineMaxLength] & CrLf; sLine := sLine[pI_LineMaxLength + 1 : end]; endwhile; sRet := sRet & sLine & CrLf; endforeach; return sRet; epilog delete vlsa; end;

String::split is a method that splits the receiver on the specified substring (in this case CrLf) and returns a VLSA (Variable Length String Array).

Re: Using cnSMTPConnection to send an email

Posted: Tue Mar 20, 2012 12:57 am
by Rich Cassell
Fantastic. Thanks alot guys. Works a treat :-)

Rich