276 lines
7.9 KiB
Plaintext
276 lines
7.9 KiB
Plaintext
IMPORT os
|
|
IMPORT JAVA java.lang.System
|
|
IMPORT JAVA java.lang.Object
|
|
IMPORT JAVA java.io.ByteArrayOutputStream
|
|
IMPORT JAVA java.io.PrintStream
|
|
IMPORT JAVA java.util.Properties
|
|
IMPORT JAVA javax.mail.Session
|
|
IMPORT JAVA javax.mail.Transport
|
|
IMPORT JAVA javax.mail.Authenticator
|
|
IMPORT JAVA javax.mail.Message
|
|
IMPORT JAVA javax.mail.Message.RecipientType
|
|
IMPORT JAVA javax.mail.internet.InternetAddress
|
|
#IMPORT JAVA javax.mail.internet.MimeMessage
|
|
IMPORT JAVA javax.mail.internet.MimeMultipart
|
|
#IMPORT JAVA javax.mail.internet.MimeBodyPart
|
|
IMPORT JAVA com.sun.mail.smtp.SMTPTransport
|
|
IMPORT security
|
|
|
|
CONSTANT debug_level = 1
|
|
|
|
DEFINE logtext,rcpt_to,subject,body STRING
|
|
DEFINE out_stream ByteArrayOutputStream
|
|
DEFINE buf base.StringBuffer
|
|
|
|
TYPE cnf_t RECORD
|
|
hostname STRING,
|
|
smtphost STRING,
|
|
smtpport INTEGER,
|
|
senderemail STRING,
|
|
smtpauth SMALLINT, -- 1: uses SMTP AUTH, 0: doesn't use AUTH
|
|
username STRING,
|
|
password STRING,
|
|
use_ssl SMALLINT, -- 1: uses SSL, 0: doesn't use SSL
|
|
use_tls SMALLINT, -- 1: uses TLS, 0: doesn't use TLS
|
|
cnxtimeout INTEGER
|
|
END RECORD
|
|
DEFINE cnf cnf_t
|
|
|
|
|
|
DEFINE xpassword STRING
|
|
|
|
DEFINE sesion Session,
|
|
propuesta Properties,
|
|
password,s_protocol,m_charset STRING
|
|
# DEFINE msg MimeMessage
|
|
# DEFINE mbodyp MimeBodyPart
|
|
DEFINE multip MimeMultipart
|
|
DEFINE trans SMTPTransport
|
|
|
|
|
|
FUNCTION enviaemail(rcpt_to,subject,body,attachment,xusername,xsmtphost,xsmtpport,xpassword)
|
|
|
|
DEFINE mensaje,rcpt_to,subject,body,attachment,xusername,xsmtphost,xsmtpport,xpassword STRING
|
|
|
|
|
|
|
|
|
|
END FUNCTION
|
|
|
|
PRIVATE FUNCTION create_cxion_session()
|
|
DEFINE s_proto, s_password STRING
|
|
DEFINE x_timeout INTEGER
|
|
DEFINE sess Session
|
|
DEFINE props Properties
|
|
DEFINE custom_ps PrintStream
|
|
|
|
# Connection parameters preparation
|
|
|
|
CALL show_debug_msg(1,"Preparing connection parameters ...")
|
|
# LET cnf.password = 'Lmmjvsd2020'
|
|
CALL string_to_base64(cnf.password) RETURNING cnf.password
|
|
|
|
LET s_password=base64_to_string(cnf.password)
|
|
|
|
IF cnf.cnxtimeout>0 THEN
|
|
LET x_timeout=cnf.cnxtimeout*1000 # convert to miliseconds
|
|
ELSE
|
|
LET x_timeout=0
|
|
END IF
|
|
|
|
LET cnf.use_tls = TRUE
|
|
LET cnf.use_ssl = FALSE
|
|
|
|
IF cnf.use_ssl THEN
|
|
LET s_proto="smtps"
|
|
ELSE
|
|
LET s_proto="smtp"
|
|
END IF
|
|
|
|
|
|
CALL show_debug_msg(2,SFMT("Host: %1",cnf.smtphost))
|
|
CALL show_debug_msg(2,SFMT("Port: %1",cnf.smtpport))
|
|
CALL show_debug_msg(2,SFMT("User: %1",cnf.username))
|
|
CALL show_debug_msg(4,SFMT("Password: %1",cnf.password)) # this is the base64 password
|
|
CALL show_debug_msg(5,SFMT("Pswd txt: %1",s_password)) # this is the plain-text password
|
|
CALL show_debug_msg(2,SFMT("Prot: %1",s_proto))
|
|
CALL show_debug_msg(2,SFMT("TLS: %1",cnf.use_tls))
|
|
CALL show_debug_msg(2,SFMT("Timeout: %1",x_timeout))
|
|
|
|
# Session properties preparation
|
|
|
|
CALL show_debug_msg(1,"Preparing Session properties ...")
|
|
|
|
LET props=System.getProperties()
|
|
|
|
IF cnf.use_ssl THEN
|
|
CALL setMailProperties(props,"smtp","ssl.enable","true")
|
|
ELSE
|
|
CALL setMailProperties(props,"smtp","ssl.enable","false")
|
|
END IF
|
|
|
|
IF cnf.smtpauth THEN
|
|
CALL setMailProperties(props,s_proto,"auth","true")
|
|
ELSE
|
|
CALL setMailProperties(props,s_proto,"auth","false")
|
|
END IF
|
|
|
|
CALL setMailProperties(props,s_proto,"port",cnf.smtpport)
|
|
|
|
CALL setMailProperties(props,s_proto,"connectiontimeout",x_timeout)
|
|
|
|
IF cnf.use_tls THEN
|
|
CALL setMailProperties(props,s_proto,"starttls.enable","true")
|
|
# the following 2 parameters are needed for FourJs mail server
|
|
# but also work fine with Gmail servers when TLS option is selected
|
|
# both servers were tested on 2016-06-02
|
|
CALL setMailProperties(props,s_proto,"ssl.checkserveridentity","false")
|
|
CALL setMailProperties(props,s_proto,"ssl.trust","*")
|
|
ELSE
|
|
CALL setMailProperties(props,s_proto,"starttls.enable","false")
|
|
END IF
|
|
|
|
# Session creation
|
|
|
|
CALL show_debug_msg(1,"Creating Session ...")
|
|
|
|
LET sess=Session.getInstance(props)
|
|
LET out_stream = ByteArrayOutputStream.create()
|
|
LET custom_ps = PrintStream.create(out_stream)
|
|
CALL sess.setDebugOut(custom_ps)
|
|
CALL sess.setDebug(TRUE)
|
|
|
|
RETURN sess,s_password,s_proto
|
|
|
|
END FUNCTION
|
|
|
|
|
|
FUNCTION base64_to_string(str_b64)
|
|
|
|
DEFINE str_out,str_b64,str_aux STRING
|
|
|
|
TRY
|
|
CALL security.Base64.ToString(str_b64) RETURNING str_out
|
|
CATCH
|
|
LET str_aux="Unable to convert the Base64 string into plain-text:",STATUS
|
|
CALL mbox_ok("Error", str_aux, "stop")
|
|
LET str_out=NULL
|
|
END TRY
|
|
|
|
RETURN str_out
|
|
|
|
END FUNCTION
|
|
|
|
PRIVATE FUNCTION show_debug_msg(lev,msg)
|
|
DEFINE lev SMALLINT
|
|
DEFINE msg STRING
|
|
IF lev>=debug_level THEN
|
|
DISPLAY msg
|
|
END IF
|
|
END FUNCTION
|
|
|
|
PRIVATE FUNCTION setMailProperties(props,prot,prop,val)
|
|
DEFINE props Properties
|
|
DEFINE auxi,prot,prop,val STRING
|
|
|
|
LET auxi=SFMT("mail.%1.%2",prot,prop)
|
|
|
|
CALL props.put(auxi,val)
|
|
|
|
CALL show_debug_msg(1,SFMT("Property '%1' set to value '%2'", auxi, val))
|
|
|
|
END FUNCTION
|
|
FUNCTION mbox_ok(t, m, i)
|
|
DEFINE t,m,i STRING
|
|
MENU t ATTRIBUTES(STYLE="dialog",COMMENT=m,IMAGE=i)
|
|
COMMAND "Ok" EXIT MENU
|
|
END MENU
|
|
END FUNCTION
|
|
|
|
FUNCTION string_to_base64(str_ent)
|
|
|
|
DEFINE str_ent,str_b64,str_aux STRING
|
|
|
|
TRY
|
|
CALL security.Base64.FromString(str_ent) RETURNING str_b64
|
|
CATCH
|
|
LET str_aux="Unable to convert the input string into Base64:",STATUS
|
|
CALL mbox_ok("Error", str_aux, "stop")
|
|
LET str_b64=NULL
|
|
END TRY
|
|
|
|
RETURN str_b64
|
|
|
|
END FUNCTION
|
|
|
|
FUNCTION validate_param_smpt_auth()
|
|
|
|
DEFINE flag_ok BOOLEAN
|
|
|
|
LET flag_ok=TRUE
|
|
|
|
IF xpassword=cnf.password THEN
|
|
-- if password didn't change we don't encrypt as it is already encrypted
|
|
ELSE
|
|
IF LENGTH(cnf.password)>0 THEN
|
|
CALL string_to_base64(cnf.password) RETURNING cnf.password
|
|
END IF
|
|
LET xpassword=cnf.password -- store new encrypted value
|
|
END IF
|
|
|
|
IF cnf.smtpauth THEN
|
|
IF LENGTH(cnf.username) == 0 THEN
|
|
LET flag_ok=FALSE
|
|
CALL mbox_ok("Error", "The username is mandatory when using Smtp Auth", "stop")
|
|
ELSE
|
|
IF LENGTH(cnf.password) == 0 THEN
|
|
LET flag_ok=FALSE
|
|
CALL mbox_ok("Error", "The password is mandatory when using Smtp Auth", "stop")
|
|
END IF
|
|
END IF
|
|
END IF
|
|
|
|
RETURN flag_ok
|
|
|
|
END FUNCTION
|
|
|
|
# Retrieve the current charset
|
|
FUNCTION getCharset()
|
|
DEFINE l STRING
|
|
DEFINE c base.Channel
|
|
|
|
LET c = base.Channel.create()
|
|
CALL c.openPipe("fglrun -i mbcs 2>&1", "r")
|
|
LET l = c.readLine()
|
|
WHILE l IS NOT NULL
|
|
IF l MATCHES "Charmap : *" THEN
|
|
RETURN l.subString(16,length(l))
|
|
EXIT WHILE
|
|
END IF
|
|
LET l = c.readLine()
|
|
END WHILE
|
|
RETURN "ISO-8859-1" -- default value
|
|
END FUNCTION
|
|
|
|
PRIVATE FUNCTION show_output_dialog(s_stat)
|
|
DEFINE s_stat,s_msg,s_color STRING
|
|
DEFINE strbuf base.StringBuffer
|
|
|
|
IF s_stat="OK" THEN
|
|
LET s_color="green"
|
|
LET s_msg="The SMTP connection ended OK."
|
|
ELSE
|
|
LET s_color="red"
|
|
LET s_msg="The SMTP connection ended with Error."
|
|
END IF
|
|
|
|
# separates the Session debug message into html lines
|
|
LET strbuf = base.StringBuffer.create()
|
|
CALL strbuf.append(out_stream.toString())
|
|
CALL strbuf.replace("\n","<BR>",0)
|
|
|
|
LET logtext=strbuf.toString(),"<BR>", # first adds the debug lines without additional format
|
|
SFMT("<font color='%1'>",s_color),s_msg,"</font>" # then adds the result message in color
|
|
|
|
END FUNCTION
|