Title: Sending SMTP mail

Question:

We need to send a mail message to a user via an external SMTP server when a certain event is triggered in an application. Is there an isCOBOL code example for invoking the Java method which can do this? We need to provide the SMTP server name, mail-from, user/password and of course the message.

Answer:

There is an example that uses SMTP email on the Veryant community forums. Click on Send SMS Text Message or visit http://veryant.com/forums/ and click on Sample Programs.

This sample uses JavaMail to send SMTP emails to SMS text services provided by mobile phone carriers. For example, to send a SMS text message to (123) 456-7890 carried by AT&T the program sends an email to 1234567890@txt.att.net.

The source code that accomplishes this in sndemail.cbl is as follows:


       send-message.
           set smtp-subject to lks-subject
           accept smtp-host from environment "mail.smtp.host"
           accept smtp-port from environment "mail.smtp.port" 
           accept smtp-ssl-enable from environment 
             "mail.smtp.ssl.enable" 
           accept smtp-auth from environment "mail.smtp.auth" 
           accept username from environment "username"
           set smtp-from to username 
           accept password from environment "password" 
            
           try
            set props = j-properties:>new()
            props:>put("mail.smtp.host" as string, smtp-host as string)
            props:>put("mail.smtp.port" as string, smtp-port as string)
            props:>put("mail.smtp.ssl.enable" as string, 
             smtp-ssl-enable as string)
            if smtp-auth = "true" 
      *---- authentication -------------------------------------------------------
              set ws-Authenticator to 
               Authenticator:>new(username, password)
              props:>put("mail.smtp.submitter" as string, 
               ws-Authenticator:>getPasswordAuthentication:>getUserName 
               as string)
              props:>put("mail.smtp.auth" as string, 
               "true" as string)
              set session = j-session:>getDefaultInstance(props, 
               ws-Authenticator)
      *---------------------------------------------------------------------------
            else
              set session = j-session:>getDefaultInstance ( props )
            end-if
            set msg = j-mime-message:>new(session)
            msg:>setFrom(j-internet-address:>new(smtp-from))
            msg:>setRecipient(j-recipient-type:>TO, 
                          j-internet-address:>new(lks-to as string))
            msg:>setText ( lks-message as string )
            msg:>setSubject(smtp-subject)
            j-transport:>send(msg)          
           catch j-exception 
            set tmpstr to exception-object
            display message box tmpstr 
            end-display
            exception-object:>printStackTrace()
           end-try.                       

Authored by: Veryant Support on Tue, Jan 3rd, 2012 at 7:00 PM
This question has been viewed 6115 times so far.
Online URL: http://support.veryant.com/support/phpkb/question.php?ID=187

Powered by PHPKB Knowledge Base Software