Files
MBS/PROYECTO/otrodir/conexion_smtp.4gl
T

121 lines
3.7 KiB
Plaintext

IMPORT JAVA javax.mail.Session
IMPORT JAVA java.util.Properties
IMPORT JAVA java.io.PrintStream
IMPORT JAVA java.lang.System
IMPORT JAVA java.io.ByteArrayOutputStream
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,logtext STRING
DEFINE out_stream ByteArrayOutputStream
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.senderemail = 'nominas@marmotech.com.do'
LET cnf.hostname = 'smtp.office365.com'
LET cnf.smtphost = 'smtp.office365.com'
LET cnf.smtpport = '587'
LET cnf.username = 'nominas@marmotech.com.do'
LET cnf.smtpauth = TRUE
LET cnf.password = 'Nuevo2018'
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