Email class: Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 38: Line 38:
For more detailed information about defining
For more detailed information about defining
<var class="product">Janus</var> client socket ports, see <var>[[JANUS DEFINE]]</var> and <var>[[JANUS CLSOCK]]</var>.
<var class="product">Janus</var> client socket ports, see <var>[[JANUS DEFINE]]</var> and <var>[[JANUS CLSOCK]]</var>.
<p class="code"> JANUS DEFINE MYSMTP * CLSOCK 5 REMOTE 198.242.244.100 25
 
<p class="code">JANUS DEFINE MYSMTP * CLSOCK 5 REMOTE 198.242.244.100 25
</p>
</p>
'''Note:'''
'''Note:'''
To use the <var>Email</var> class objects, you must have licensed <var class="product">Janus Sockets</var>
To use the <var>Email</var> class objects, you must have licensed <var class="product">Janus Sockets</var>
Line 68: Line 70:
   
   
==SMTP examples==
==SMTP examples==
Examples follow in which <var>Email</var> objects are used to
Examples follow in which <var>Email</var> objects are used to create and send a request to an SMTP server.
create and send a request to an SMTP server.
No <var class="product">Janus</var> port definitions are shown.
No <var class="product">Janus</var> port definitions are shown.
For comments about defining <var class="product">Janus</var> ports for the <var>Email</var> class, see
For comments about defining <var class="product">Janus</var> ports for the <var>Email</var> class, see
the [[#jport|earlier]] remarks.
the [[#jport|earlier]] remarks.
===Basic e-mail example===
===Basic e-mail example===
This example uses most of the <var>Email</var> methods to send a simple message
This example uses most of the <var>Email</var> methods to send a simple message to three recipients.
to three recipients.
The local <var>CLSOCK</var> port used is named <code>SPAM</code>.
The local <var>[[JANUS CLSOCK|CLSOCK]]</var> port used is named <code>SPAM</code>.
<p class="code">b
<p class="code"> b
   
   
%letter object Email
%letter object Email
%h      longstring
%h      longstring
%t      longstring
%t      longstring
%name  longstring
%name  longstring
%nick  string len 64
%nick  string len 64
   
   
%letter = new
%letter = new
   
   
%name = 'C_Coolidge@presidents.usa'
%name = 'C_Coolidge@presidents.usa'
%nick = 'SilentCal'
%nick = 'SilentCal'
%letter:AddRecipient(%name, %nick)
%letter:AddRecipient(%name, %nick)
   
   
%name = 'D_Eisenhower@presidents.usa'
%name = 'D_Eisenhower@presidents.usa'
%nick = 'Ike'
%nick = 'Ike'
%letter:AddCc(%name, %nick)
%letter:AddCc(%name, %nick)
   
   
%name = L_Johnson@presidents.usa'
%name = L_Johnson@presidents.usa'
%nick = 'LBJ'
%nick = 'LBJ'
%letter:AddBcc(%name, %nick)
%letter:AddBcc(%name, %nick)
   
   
%letter:Sender('a_huckster@bankandtrustme.com')
%letter:Sender('a_huckster@bankandtrustme.com')
   
   
%h = 'Subject:'
%h = 'Subject:'
%t = 'Lower Mortgage Rates'
%t = 'Lower Mortgage Rates'
%letter:AddHeader(%h, %t)
%letter:AddHeader(%h, %t)
   
   
%h = 'Great deal of debt for you!'
%h = 'Great deal of debt for you!'
%letter:SetBody(%h)
%letter:SetBody(%h)
   
   
%letter:mail('SPAM', 0)
%letter:mail('SPAM', 0)
   
   
print %letter:GetReplyCode with ' - ' with -
print %letter:GetReplyCode with ' - ' with -
      %letter:GetReplyText
      %letter:GetReplyText
   
   
%letter:discard
%letter:discard
end
end
</p>
</p>
===E-mail with attachment===
===E-mail with attachment===
In the following example, an image is attached to a short e-mail message.
In the following example, an image is attached to a short e-mail message.
The attachment is produced from a member of a local <var class="product">Model 204</var> procedure
The attachment is produced from a member of a local <var class="product">Model 204</var> procedure file
file
using <var>StringList</var> class methods.
using <var>StringList</var> class methods.
<p class="code"> b
 
%rolex is object Email
<p class="code">b
%sl  is object Stringlist
%rolex is object Email
%procdata  is object Stringlist
%sl  is object Stringlist
%jpg longstring
%procdata  is object Stringlist
%t longstring
%jpg longstring
%temp string len 255
%t longstring
%procfile string len 10
%temp string len 255
%procname string len 63
%procfile string len 10
%q float
%procname string len 63
%rc float
%q float
%rc float
   
   
%rolex = new
%rolex = new
%sl  = new
%sl  = new
   
   
%t = 'jbgood@netscape.net'
%t = 'jbgood@netscape.net'
%rolex:AddRecipient(%t, 'JB')
%rolex:AddRecipient(%t, 'JB')
   
   
%t = 'cberry@ucantbee-sirius.com'
%t = 'cberry@ucantbee-sirius.com'
%rolex:Sender(%t)
%rolex:Sender(%t)
%rolex:AddHeader('Subject:', 'watch this...')
%rolex:AddHeader('Subject:', 'watch this...')
   
   
text to %sl
text to %sl
  I want an authentic imitation Rolex watch. Something like
I want an authentic imitation Rolex watch. Something like
  the one shown in this image. Know anyone who's got one?
the one shown in this image. Know anyone who's got one?
end text
end text
%rolex:SetBody(%sl, encoding='none', type='text/plain')
%rolex:SetBody(%sl, encoding='none', type='text/plain')
   
   
%procfile = 'MYPROC'
%procfile = 'MYPROC'
%procname = 'FAKE5.JPG'
%procname = 'FAKE5.JPG'
%rc = $procopn(%procname, %procfile)
%rc = $procopn(%procname, %procfile)
if %rc ne 0 then
if %rc ne 0 then
    print 'Can''t open procedure ' with %procname    -
  print 'Can''t open procedure ' with %procname    -
    with ' in file ' with %procfile with ', rc = '  -
    with ' in file ' with %procfile with ', rc = '  -
    with %rc
    with %rc
    stop
  stop
end if
end if
   
   
%procdata = new
%procdata = new
%procdata:appendOpenProcedure
%procdata:appendOpenProcedure
%jpg = %procData:binaryProcedureDecode
%jpg = %procData:binaryProcedureDecode
%rolex:AddPart(%jpg, type='image/jpg', name='Rolex1.jpg')
%rolex:AddPart(%jpg, type='image/jpg', name='Rolex1.jpg')
   
   
print %rolex:mail('MYSMTP', 0) ' is return from Mail method'
print %rolex:mail('MYSMTP', 0) ' is return from Mail method'
print %rolex:GetReplyCode ' is return from GetReplyCode'
print %rolex:GetReplyCode ' is return from GetReplyCode'
print %rolex:GetReplyText
print %rolex:GetReplyText
   
   
%rolex:discard
%rolex:discard
end
end
</p>
</p>
   
   
If the request is successful, results like the following
If the request is successful, results like the following display at the terminal:
display at the terminal:
 
<p class="code"> 0 is return from Mail method
<p class="code">0 is return from Mail method
221 is return from GetReplyCode
221 is return from GetReplyCode
ucantbee-sirius.com running IBM VM SMTP Level 320
ucantbee-sirius.com running IBM VM SMTP Level 320
    closing connection
  closing connection
</p>
</p>



Revision as of 21:00, 13 November 2012

The Email class is a high level, object oriented interface to client sockets that lets you write a User Language SMTP (Simple Mail Transfer Protocol) request without knowledge of socket level programming or the format of SMTP requests and responses.

The Email class methods let a User Language program act as an SMTP client. SMTP clients send mail requests to an SMTP server, not directly to other SMTP clients. The Email class methods only support communication with SMTP servers.

An SMTP server will “store and forward” mail it receives. This means that all messages are first received in entirety on the SMTP server, then the server attempts to deliver it to the recipients. Mail delivery can be delayed by a server that is down or too busy to accept connections. “Store and forward” gives the SMTP server the ability to retry delivery without making the SMTP client wait for completion.

A server might try to deliver a message for as long as a week or more. This, of course, makes it impractical for a client to wait for delivery confirmation, so the SMTP protocol does not provide such information. A “success” response from an SMTP server indicates only that the server has accepted the request and will attempt to deliver the mail to the recipients.

Most SMTP servers will at least confirm that any domain names in the recipient list are valid, but they cannot confirm that an actual recipient name is valid.

You must define a Janus CLSOCK port to use the Email class. If you supply the IP address of the server and the port number in the port definition, SMTP applications need only know the Janus port name to communicate with the server. The following example defines a client socket port for use by SMTP applications. For more detailed information about defining Janus client socket ports, see JANUS DEFINE and JANUS CLSOCK.

JANUS DEFINE MYSMTP * CLSOCK 5 REMOTE 198.242.244.100 25

Note: To use the Email class objects, you must have licensed Janus Sockets and Janus SOAP. The Sirius object-oriented alternative to $functions is designed for Janus SOAP applications only, and information about using Sirius objects is provided in "Janus SOAP User Language Interface" and "Getting started with object-oriented programming for User Language programmers".

Email class summary

The following capabilities are provided by the Email class:

  • Creating general purpose e-mail for SMTP
  • Sending general purpose e-mail via SMTP
  • Setting e-mail content using Longstrings
  • Sending MIME content (attachments and alternative content)
  • Sending to multiple recipients
  • Access to status (error or confirmation messages)

You use the Email object to generate SMTP messages (e-mail) to send to an SMTP server. To construct a request, you use Email methods to set and inspect header information, content, and attachments. To send e-mail, you use the Mail method.

"List of Email methods" contains a list of all the methods in the Email class.

SMTP examples

Examples follow in which Email objects are used to create and send a request to an SMTP server. No Janus port definitions are shown. For comments about defining Janus ports for the Email class, see the earlier remarks.

Basic e-mail example

This example uses most of the Email methods to send a simple message to three recipients. The local CLSOCK port used is named SPAM.

b %letter object Email %h longstring %t longstring %name longstring %nick string len 64 %letter = new %name = 'C_Coolidge@presidents.usa' %nick = 'SilentCal' %letter:AddRecipient(%name, %nick) %name = 'D_Eisenhower@presidents.usa' %nick = 'Ike' %letter:AddCc(%name, %nick) %name = L_Johnson@presidents.usa' %nick = 'LBJ' %letter:AddBcc(%name, %nick) %letter:Sender('a_huckster@bankandtrustme.com') %h = 'Subject:' %t = 'Lower Mortgage Rates' %letter:AddHeader(%h, %t) %h = 'Great deal of debt for you!' %letter:SetBody(%h) %letter:mail('SPAM', 0) print %letter:GetReplyCode with ' - ' with - %letter:GetReplyText %letter:discard end

E-mail with attachment

In the following example, an image is attached to a short e-mail message. The attachment is produced from a member of a local Model 204 procedure file using StringList class methods.

b %rolex is object Email %sl is object Stringlist %procdata is object Stringlist %jpg longstring %t longstring %temp string len 255 %procfile string len 10 %procname string len 63 %q float %rc float %rolex = new %sl = new %t = 'jbgood@netscape.net' %rolex:AddRecipient(%t, 'JB') %t = 'cberry@ucantbee-sirius.com' %rolex:Sender(%t) %rolex:AddHeader('Subject:', 'watch this...') text to %sl I want an authentic imitation Rolex watch. Something like the one shown in this image. Know anyone who's got one? end text %rolex:SetBody(%sl, encoding='none', type='text/plain') %procfile = 'MYPROC' %procname = 'FAKE5.JPG' %rc = $procopn(%procname, %procfile) if %rc ne 0 then print 'Cant open procedure ' with %procname - with ' in file ' with %procfile with ', rc = ' - with %rc stop end if %procdata = new %procdata:appendOpenProcedure %jpg = %procData:binaryProcedureDecode %rolex:AddPart(%jpg, type='image/jpg', name='Rolex1.jpg') print %rolex:mail('MYSMTP', 0) ' is return from Mail method' print %rolex:GetReplyCode ' is return from GetReplyCode' print %rolex:GetReplyText %rolex:discard end

If the request is successful, results like the following display at the terminal:

0 is return from Mail method 221 is return from GetReplyCode ucantbee-sirius.com running IBM VM SMTP Level 320 closing connection

List of Email methods

The "List of Email methods" shows all the class methods.