Sending e-mails from a NetRexx application.

classic Classic list List threaded Threaded
26 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Sending e-mails from a NetRexx application.

alansam
Not to be outdone, here are two more examples of sending mail via GMail, one using Transport Layer Security [TLS], the other using Secure Socket Layer [SSL]:


On 7 June 2011 10:43, David Requena <[hidden email]> wrote:
OK, here you go. David's crude html formated email sending through gmail sample. Enjoy modifying it :-)

/* NetRexx */
options replace format comments java crossref savelog symbols


import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage

/**
 * Send mail using JavaMail through GMail using Transport Layer Security [TLS]
 *
 * @author Alan Sampson
 * @version 1.0
 * @see http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
 * @see http://java-sql.blogspot.com/2011/01/send-email-in-java.html
 */
class SendMailTLS_NetRexx public
 
  properties public constant
    k_true  = String("true")
    k_false = String("false")
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)

    smtp_gateway_GMAIL ="smtp.gmail.com"
    address_from = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = '"Noni Mouse" <[hidden email]>'

  /**
   *
   */
  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailTLS_NetRexx <mail_account> <password>'
      say

      signal IllegalArgumentException("Provide runtime arguments!")
      end

    sess = Session
    tran = Transport
    mmsg = Message

    host     = String(smtp_gateway_GMAIL)
    port     = int(587)
    username = args[0]
    password = args[1]
    m_from   = String(address_from)
    m_to     = String(address_to)
    m_cc     = String(address_cc)
    m_bcc    = String(address_bcc)
    m_subj   = String('Testing NetRexx JavaMail [TLS]')
    m_text   = String("" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      )

    props = Properties()

    props.put(String("mail.smtp.starttls.enable"), k_true)
    props.put(String("mail.smtp.host"), host)
    props.put(String("mail.smtp.user"), username)
    props.put(String("mail.smtp.password"), password)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.debug"), k_true)

    sess = Session.getInstance(props)
    sess.setDebug(TRUE)
 
    do
      mmsg = MimeMessage(sess)
      mmsg.setFrom(InternetAddress(m_from))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(m_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(m_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(m_bcc))
      mmsg.setSubject(m_subj)
      mmsg.setText(m_text)
 
      tran = sess.getTransport("smtp")
      tran.connect(host, port, username, password)
      tran.sendMessage(mmsg, mmsg.getAllRecipients())
      tran.close

      say "Done"

    catch ex = Exception
      ex.printStackTrace
    end

    return


/* NetRexx */
options replace format comments java crossref savelog symbols

import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.PasswordAuthentication
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import javax.mail.Authenticator

/**
 * Send mail using JavaMail through GMail using Secure Socket Layer [SSL]
 *
 * @author Alan Sampson
 * @version 1.0
 */
class SendMailSSL_NetRexx

  properties constant
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)
    k_true  = String("true")
    k_false = String("false")

    smtp_gateway_GMAIL = "smtp.gmail.com"

    address_sender = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = ''

  properties inheritable static
    username = String
    password = String
    port     = int

  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailSSL_NetRexx <mail_account> <password>'
      say
      signal IllegalArgumentException("Provide runtime arguments!")
      end

    mmsg = Message
    sess = Session
    auth = Authenticator

    username = args[0]
    password = args[1]
    port = int(465)
    m_text = "" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      || ""

    props = Properties()
    props.put(String("mail.smtp.host"), smtp_gateway_GMAIL.toString)
    props.put(String("mail.smtp.socketFactory.port"), Integer.toString(port))
    props.put(String("mail.smtp.socketFactory.class"), String("javax.net.ssl.SSLSocketFactory"))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.debug"), k_true)
   
    auth = SMTPAuthenticator()
    sess = Session.getDefaultInstance(props, auth)
    sess.setDebug(TRUE)

    do
      mmsg = MimeMessage(sess)

      mmsg.setFrom(InternetAddress(address_sender))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(address_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(address_bcc))
      mmsg.setSubject('Testing NetRexx JavaMail [SSL]')
      mmsg.setText(m_text)
 
      Transport.send(mmsg)

    catch ex = Exception
      ex.printStackTrace
    end

    return

  /**
   * Inner class required because of  NetRexx's inability to generate
   * anonymous classes
   */
  class SendMailSSL_NetRexx.SMTPAuthenticator private extends Authenticator uses SendMailSSL_NetRexx
    method getPasswordAuthentication inheritable returns PasswordAuthentication
      return PasswordAuthentication(username, password)






--
Can't tweet, won't tweet!

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Alan

--
Needs more cowbell.
Reply | Threaded
Open this post in threaded view
|

Re: Sending e-mails from a NetRexx application.

Robert L Hamilton
Alan Sampson's version of MailSender.nrx comiled with jEdit/NetRExxDE.
 
Thanks for your help and Enjoy the Day
 
Bob Hamilton
Richardson Texas USA

On Tue, Jun 7, 2011 at 1:44 PM, Alan Sampson <[hidden email]> wrote:
Not to be outdone, here are two more examples of sending mail via GMail, one using Transport Layer Security [TLS], the other using Secure Socket Layer [SSL]:



On 7 June 2011 10:43, David Requena <[hidden email]> wrote:
OK, here you go. David's crude html formated email sending through gmail sample. Enjoy modifying it :-)

/* NetRexx */
options replace format comments java crossref savelog symbols


import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage

/**
 * Send mail using JavaMail through GMail using Transport Layer Security [TLS]
 *
 * @author Alan Sampson
 * @version 1.0
 * @see http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
 * @see http://java-sql.blogspot.com/2011/01/send-email-in-java.html
 */
class SendMailTLS_NetRexx public
 
  properties public constant
    k_true  = String("true")
    k_false = String("false")
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)

    smtp_gateway_GMAIL ="smtp.gmail.com"
    address_from = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = '"Noni Mouse" <[hidden email]>'

  /**
   *
   */

  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailTLS_NetRexx <mail_account> <password>'
      say

      signal IllegalArgumentException("Provide runtime arguments!")
      end

    sess = Session
    tran = Transport
    mmsg = Message

    host     = String(smtp_gateway_GMAIL)
    port     = int(587)
    username = args[0]
    password = args[1]
    m_from   = String(address_from)
    m_to     = String(address_to)
    m_cc     = String(address_cc)
    m_bcc    = String(address_bcc)
    m_subj   = String('Testing NetRexx JavaMail [TLS]')
    m_text   = String("" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      )

    props = Properties()

    props.put(String("mail.smtp.starttls.enable"), k_true)
    props.put(String("mail.smtp.host"), host)
    props.put(String("mail.smtp.user"), username)
    props.put(String("mail.smtp.password"), password)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.debug"), k_true)

    sess = Session.getInstance(props)
    sess.setDebug(TRUE)
 
    do
      mmsg = MimeMessage(sess)
      mmsg.setFrom(InternetAddress(m_from))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(m_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(m_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(m_bcc))
      mmsg.setSubject(m_subj)
      mmsg.setText(m_text)
 
      tran = sess.getTransport("smtp")
      tran.connect(host, port, username, password)
      tran.sendMessage(mmsg, mmsg.getAllRecipients())
      tran.close

      say "Done"

    catch ex = Exception
      ex.printStackTrace
    end

    return


/* NetRexx */
options replace format comments java crossref savelog symbols

import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.PasswordAuthentication
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import javax.mail.Authenticator

/**
 * Send mail using JavaMail through GMail using Secure Socket Layer [SSL]
 *
 * @author Alan Sampson
 * @version 1.0
 */
class SendMailSSL_NetRexx

  properties constant
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)
    k_true  = String("true")
    k_false = String("false")

    smtp_gateway_GMAIL = "smtp.gmail.com"

    address_sender = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = ''

  properties inheritable static
    username = String
    password = String
    port     = int


  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailSSL_NetRexx <mail_account> <password>'
      say
      signal IllegalArgumentException("Provide runtime arguments!")
      end

    mmsg = Message
    sess = Session
    auth = Authenticator

    username = args[0]
    password = args[1]
    port = int(465)
    m_text = "" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      || ""

    props = Properties()
    props.put(String("mail.smtp.host"), smtp_gateway_GMAIL.toString)
    props.put(String("mail.smtp.socketFactory.port"), Integer.toString(port))
    props.put(String("mail.smtp.socketFactory.class"), String("javax.net.ssl.SSLSocketFactory"))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.debug"), k_true)
   
    auth = SMTPAuthenticator()
    sess = Session.getDefaultInstance(props, auth)
    sess.setDebug(TRUE)

    do
      mmsg = MimeMessage(sess)

      mmsg.setFrom(InternetAddress(address_sender))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(address_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(address_bcc))
      mmsg.setSubject('Testing NetRexx JavaMail [SSL]')
      mmsg.setText(m_text)
 
      Transport.send(mmsg)

    catch ex = Exception
      ex.printStackTrace
    end

    return

  /**
   * Inner class required because of  NetRexx's inability to generate
   * anonymous classes
   */
  class SendMailSSL_NetRexx.SMTPAuthenticator private extends Authenticator uses SendMailSSL_NetRexx
    method getPasswordAuthentication inheritable returns PasswordAuthentication
      return PasswordAuthentication(username, password)






--
Can't tweet, won't tweet!

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/




_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Sending e-mails from a NetRexx application.

ThSITC
I'm glad to see that I've started with my innocent Query of this topic such a useful conversation about useful possible NetRexx usage on this group ;-)

Thomas.

PS: Thank you all for the time invested for those examples. . :-)
========================================================

Am 07.06.2011 21:27, schrieb Robert Hamilton:
Alan Sampson's version of MailSender.nrx comiled with jEdit/NetRExxDE.
 
Thanks for your help and Enjoy the Day
 
Bob Hamilton
Richardson Texas USA

On Tue, Jun 7, 2011 at 1:44 PM, Alan Sampson <[hidden email]> wrote:
Not to be outdone, here are two more examples of sending mail via GMail, one using Transport Layer Security [TLS], the other using Secure Socket Layer [SSL]:



On 7 June 2011 10:43, David Requena <[hidden email]> wrote:
OK, here you go. David's crude html formated email sending through gmail sample. Enjoy modifying it :-)

/* NetRexx */
options replace format comments java crossref savelog symbols


import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage

/**
 * Send mail using JavaMail through GMail using Transport Layer Security [TLS]
 *
 * @author Alan Sampson
 * @version 1.0
 * @see http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
 * @see http://java-sql.blogspot.com/2011/01/send-email-in-java.html
 */
class SendMailTLS_NetRexx public
 
  properties public constant
    k_true  = String("true")
    k_false = String("false")
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)

    smtp_gateway_GMAIL ="smtp.gmail.com"
    address_from = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = '"Noni Mouse" <[hidden email]>'

  /**
   *
   */

  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailTLS_NetRexx <mail_account> <password>'
      say

      signal IllegalArgumentException("Provide runtime arguments!")
      end

    sess = Session
    tran = Transport
    mmsg = Message

    host     = String(smtp_gateway_GMAIL)
    port     = int(587)
    username = args[0]
    password = args[1]
    m_from   = String(address_from)
    m_to     = String(address_to)
    m_cc     = String(address_cc)
    m_bcc    = String(address_bcc)
    m_subj   = String('Testing NetRexx JavaMail [TLS]')
    m_text   = String("" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      )

    props = Properties()

    props.put(String("mail.smtp.starttls.enable"), k_true)
    props.put(String("mail.smtp.host"), host)
    props.put(String("mail.smtp.user"), username)
    props.put(String("mail.smtp.password"), password)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.debug"), k_true)

    sess = Session.getInstance(props)
    sess.setDebug(TRUE)
 
    do
      mmsg = MimeMessage(sess)
      mmsg.setFrom(InternetAddress(m_from))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(m_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(m_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(m_bcc))
      mmsg.setSubject(m_subj)
      mmsg.setText(m_text)
 
      tran = sess.getTransport("smtp")
      tran.connect(host, port, username, password)
      tran.sendMessage(mmsg, mmsg.getAllRecipients())
      tran.close

      say "Done"

    catch ex = Exception
      ex.printStackTrace
    end

    return


/* NetRexx */
options replace format comments java crossref savelog symbols

import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.PasswordAuthentication
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import javax.mail.Authenticator

/**
 * Send mail using JavaMail through GMail using Secure Socket Layer [SSL]
 *
 * @author Alan Sampson
 * @version 1.0
 */
class SendMailSSL_NetRexx

  properties constant
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)
    k_true  = String("true")
    k_false = String("false")

    smtp_gateway_GMAIL = "smtp.gmail.com"

    address_sender = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = ''

  properties inheritable static
    username = String
    password = String
    port     = int


  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailSSL_NetRexx <mail_account> <password>'
      say
      signal IllegalArgumentException("Provide runtime arguments!")
      end

    mmsg = Message
    sess = Session
    auth = Authenticator

    username = args[0]
    password = args[1]
    port = int(465)
    m_text = "" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      || ""

    props = Properties()
    props.put(String("mail.smtp.host"), smtp_gateway_GMAIL.toString)
    props.put(String("mail.smtp.socketFactory.port"), Integer.toString(port))
    props.put(String("mail.smtp.socketFactory.class"), String("javax.net.ssl.SSLSocketFactory"))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.debug"), k_true)
   
    auth = SMTPAuthenticator()
    sess = Session.getDefaultInstance(props, auth)
    sess.setDebug(TRUE)

    do
      mmsg = MimeMessage(sess)

      mmsg.setFrom(InternetAddress(address_sender))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(address_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(address_bcc))
      mmsg.setSubject('Testing NetRexx JavaMail [SSL]')
      mmsg.setText(m_text)
 
      Transport.send(mmsg)

    catch ex = Exception
      ex.printStackTrace
    end

    return

  /**
   * Inner class required because of  NetRexx's inability to generate
   * anonymous classes
   */
  class SendMailSSL_NetRexx.SMTPAuthenticator private extends Authenticator uses SendMailSSL_NetRexx
    method getPasswordAuthentication inheritable returns PasswordAuthentication
      return PasswordAuthentication(username, password)






--
Can't tweet, won't tweet!

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/



_______________________________________________ Ibm-netrexx mailing list [hidden email] Online Archive : http://ibm-netrexx.215625.n3.nabble.com/


--
Thomas Schneider (www.thsitc.com)

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Thomas Schneider, Vienna, Austria (Europe) :-)

www.thsitc.com
www.db-123.com
Reply | Threaded
Open this post in threaded view
|

Fwd: Sending e-mails from a NetRexx application.

rvjansen
In reply to this post by alansam
Earl,

this is probably the best I can find, from last year's efforts by David Requena and Alan Sampson.

best regards,

René.

Begin forwarded message:

From: Alan Sampson <[hidden email]>
Subject: Re: [Ibm-netrexx] Sending e-mails from a NetRexx application.
Date: 7 juni 2011 20:44:26 CEST
To: IBM Netrexx <[hidden email]>
Reply-To: IBM Netrexx <[hidden email]>

Not to be outdone, here are two more examples of sending mail via GMail, one using Transport Layer Security [TLS], the other using Secure Socket Layer [SSL]:


On 7 June 2011 10:43, David Requena <[hidden email]> wrote:
OK, here you go. David's crude html formated email sending through gmail sample. Enjoy modifying it :-)

/* NetRexx */
options replace format comments java crossref savelog symbols


import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage

/**
 * Send mail using JavaMail through GMail using Transport Layer Security [TLS]
 *
 * @author Alan Sampson
 * @version 1.0
 * @see http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
 * @see http://java-sql.blogspot.com/2011/01/send-email-in-java.html
 */
class SendMailTLS_NetRexx public
 
  properties public constant
    k_true  = String("true")
    k_false = String("false")
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)

    smtp_gateway_GMAIL ="smtp.gmail.com"
    address_from = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = '"Noni Mouse" <[hidden email]>'

  /**
   *
   */
  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailTLS_NetRexx <mail_account> <password>'
      say

      signal IllegalArgumentException("Provide runtime arguments!")
      end

    sess = Session
    tran = Transport
    mmsg = Message

    host     = String(smtp_gateway_GMAIL)
    port     = int(587)
    username = args[0]
    password = args[1]
    m_from   = String(address_from)
    m_to     = String(address_to)
    m_cc     = String(address_cc)
    m_bcc    = String(address_bcc)
    m_subj   = String('Testing NetRexx JavaMail [TLS]')
    m_text   = String("" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      )

    props = Properties()

    props.put(String("mail.smtp.starttls.enable"), k_true)
    props.put(String("mail.smtp.host"), host)
    props.put(String("mail.smtp.user"), username)
    props.put(String("mail.smtp.password"), password)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.debug"), k_true)

    sess = Session.getInstance(props)
    sess.setDebug(TRUE)
 
    do
      mmsg = MimeMessage(sess)
      mmsg.setFrom(InternetAddress(m_from))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(m_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(m_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(m_bcc))
      mmsg.setSubject(m_subj)
      mmsg.setText(m_text)
 
      tran = sess.getTransport("smtp")
      tran.connect(host, port, username, password)
      tran.sendMessage(mmsg, mmsg.getAllRecipients())
      tran.close

      say "Done"

    catch ex = Exception
      ex.printStackTrace
    end

    return


/* NetRexx */
options replace format comments java crossref savelog symbols

import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.PasswordAuthentication
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import javax.mail.Authenticator

/**
 * Send mail using JavaMail through GMail using Secure Socket Layer [SSL]
 *
 * @author Alan Sampson
 * @version 1.0
 */
class SendMailSSL_NetRexx

  properties constant
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)
    k_true  = String("true")
    k_false = String("false")

    smtp_gateway_GMAIL = "smtp.gmail.com"

    address_sender = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = ''

  properties inheritable static
    username = String
    password = String
    port     = int

  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailSSL_NetRexx <mail_account> <password>'
      say
      signal IllegalArgumentException("Provide runtime arguments!")
      end

    mmsg = Message
    sess = Session
    auth = Authenticator

    username = args[0]
    password = args[1]
    port = int(465)
    m_text = "" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      || ""

    props = Properties()
    props.put(String("mail.smtp.host"), smtp_gateway_GMAIL.toString)
    props.put(String("mail.smtp.socketFactory.port"), Integer.toString(port))
    props.put(String("mail.smtp.socketFactory.class"), String("javax.net.ssl.SSLSocketFactory"))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.debug"), k_true)
   
    auth = SMTPAuthenticator()
    sess = Session.getDefaultInstance(props, auth)
    sess.setDebug(TRUE)

    do
      mmsg = MimeMessage(sess)

      mmsg.setFrom(InternetAddress(address_sender))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(address_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(address_bcc))
      mmsg.setSubject('Testing NetRexx JavaMail [SSL]')
      mmsg.setText(m_text)
 
      Transport.send(mmsg)

    catch ex = Exception
      ex.printStackTrace
    end

    return

  /**
   * Inner class required because of  NetRexx's inability to generate
   * anonymous classes
   */
  class SendMailSSL_NetRexx.SMTPAuthenticator private extends Authenticator uses SendMailSSL_NetRexx
    method getPasswordAuthentication inheritable returns PasswordAuthentication
      return PasswordAuthentication(username, password)






--
Can't tweet, won't tweet!
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/



_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Sending e-mails from a NetRexx application.

Earl Hodil
Thanks!

On Aug 16, 2012, at 6:28 AM, René Jansen <[hidden email]> wrote:

Earl,

this is probably the best I can find, from last year's efforts by David Requena and Alan Sampson.

best regards,

René.

Begin forwarded message:

From: Alan Sampson <[hidden email]>
Subject: Re: [Ibm-netrexx] Sending e-mails from a NetRexx application.
Date: 7 juni 2011 20:44:26 CEST
To: IBM Netrexx <[hidden email]>
Reply-To: IBM Netrexx <[hidden email]>

Not to be outdone, here are two more examples of sending mail via GMail, one using Transport Layer Security [TLS], the other using Secure Socket Layer [SSL]:


On 7 June 2011 10:43, David Requena <[hidden email]> wrote:
OK, here you go. David's crude html formated email sending through gmail sample. Enjoy modifying it :-)

/* NetRexx */
options replace format comments java crossref savelog symbols


import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage

/**
 * Send mail using JavaMail through GMail using Transport Layer Security [TLS]
 *
 * @author Alan Sampson
 * @version 1.0
 * @see http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
 * @see http://java-sql.blogspot.com/2011/01/send-email-in-java.html
 */
class SendMailTLS_NetRexx public
 
  properties public constant
    k_true  = String("true")
    k_false = String("false")
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)

    smtp_gateway_GMAIL ="smtp.gmail.com"
    address_from = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = '"Noni Mouse" <[hidden email]>'

  /**
   *
   */
  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailTLS_NetRexx <mail_account> <password>'
      say

      signal IllegalArgumentException("Provide runtime arguments!")
      end

    sess = Session
    tran = Transport
    mmsg = Message

    host     = String(smtp_gateway_GMAIL)
    port     = int(587)
    username = args[0]
    password = args[1]
    m_from   = String(address_from)
    m_to     = String(address_to)
    m_cc     = String(address_cc)
    m_bcc    = String(address_bcc)
    m_subj   = String('Testing NetRexx JavaMail [TLS]')
    m_text   = String("" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      )

    props = Properties()

    props.put(String("mail.smtp.starttls.enable"), k_true)
    props.put(String("mail.smtp.host"), host)
    props.put(String("mail.smtp.user"), username)
    props.put(String("mail.smtp.password"), password)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.debug"), k_true)

    sess = Session.getInstance(props)
    sess.setDebug(TRUE)
 
    do
      mmsg = MimeMessage(sess)
      mmsg.setFrom(InternetAddress(m_from))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(m_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(m_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(m_bcc))
      mmsg.setSubject(m_subj)
      mmsg.setText(m_text)
 
      tran = sess.getTransport("smtp")
      tran.connect(host, port, username, password)
      tran.sendMessage(mmsg, mmsg.getAllRecipients())
      tran.close

      say "Done"

    catch ex = Exception
      ex.printStackTrace
    end

    return


/* NetRexx */
options replace format comments java crossref savelog symbols

import java.util.Properties
import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.PasswordAuthentication
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import javax.mail.Authenticator

/**
 * Send mail using JavaMail through GMail using Secure Socket Layer [SSL]
 *
 * @author Alan Sampson
 * @version 1.0
 */
class SendMailSSL_NetRexx

  properties constant
    TRUE    = boolean (1 == 1)
    FALSE   = boolean (\TRUE)
    k_true  = String("true")
    k_false = String("false")

    smtp_gateway_GMAIL = "smtp.gmail.com"

    address_sender = '"Your Name" <[hidden email]>'

    -- address_to, address_cc and address_bcc should be comma separated email addresses
    address_to     = '"Justin Alias" <[hidden email]>, "Ann Other" <[hidden email]>'
    address_cc     = '"Ivan Interest" <[hidden email]>'
    address_bcc    = ''

  properties inheritable static
    username = String
    password = String
    port     = int

  method main(args = String[]) public static

    if args.length <= 0 then do
      say 'Usage:'
      say ' > java SendMailSSL_NetRexx <mail_account> <password>'
      say
      signal IllegalArgumentException("Provide runtime arguments!")
      end

    mmsg = Message
    sess = Session
    auth = Authenticator

    username = args[0]
    password = args[1]
    port = int(465)
    m_text = "" -
      || "\n" -
      || "\n" -
      || "\tThe quick brown fox jumps over the lazy dog.\n" -
      || "\n" -
      || "\tRound the rugged rocks the rugged rascal ran.\n" -
      || "\n" -
      || "\tRegards,\n" -
      || "\tAlan." -
      || ""

    props = Properties()
    props.put(String("mail.smtp.host"), smtp_gateway_GMAIL.toString)
    props.put(String("mail.smtp.socketFactory.port"), Integer.toString(port))
    props.put(String("mail.smtp.socketFactory.class"), String("javax.net.ssl.SSLSocketFactory"))
    props.put(String("mail.smtp.auth"), k_true)
    props.put(String("mail.smtp.port"), Integer.toString(port))
    props.put(String("mail.smtp.debug"), k_true)
   
    auth = SMTPAuthenticator()
    sess = Session.getDefaultInstance(props, auth)
    sess.setDebug(TRUE)

    do
      mmsg = MimeMessage(sess)

      mmsg.setFrom(InternetAddress(address_sender))
      mmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address_to))
      mmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(address_cc))
      mmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(address_bcc))
      mmsg.setSubject('Testing NetRexx JavaMail [SSL]')
      mmsg.setText(m_text)
 
      Transport.send(mmsg)

    catch ex = Exception
      ex.printStackTrace
    end

    return

  /**
   * Inner class required because of  NetRexx's inability to generate
   * anonymous classes
   */
  class SendMailSSL_NetRexx.SMTPAuthenticator private extends Authenticator uses SendMailSSL_NetRexx
    method getPasswordAuthentication inheritable returns PasswordAuthentication
      return PasswordAuthentication(username, password)






--
Can't tweet, won't tweet!
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/


_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/



_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Sending e-mails from a NetRexx application.

Fernando Cassia-2
In reply to this post by rvjansen
On Thu, Aug 16, 2012 at 7:28 AM, René Jansen <[hidden email]> wrote:
> this is probably the best I can find, from last year's efforts by David
> Requena and Alan Sampson.

So it was David and Alan. Sorry :) my DNA-based memory betrayed me....

I thought it was Kermit and/or RVJ :)

FC
--
During times of Universal Deceit, telling the truth becomes a revolutionary act
- George Orwell

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

12