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 sesion Session, propuesta Properties, password,s_protocol,m_charset STRING DEFINE msg MimeMessage DEFINE mbodyp MimeBodyPart DEFINE multip MimeMultipart DEFINE trans SMTPTransport DEFINE Toemail,subject,cuerpo_correo,xusername,xsmtphost,xsmtpport,xpassword,emailadd STRING DEFINE logtext STRING DEFINE out_stream 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 MAIN DEFINE xemail STRING #CALL generatemplate() CALL enviaemail() END MAIN #FUNCTION enviaemail(rcpt_to,subject,body,attachment,xusername,xsmtphost,xsmtpport,xpassword) FUNCTION enviaemail() DEFINE rcpt_to,subject,body,attachment,xusername,xsmtphost,xsmtpport,xpassword STRING LET cnf.smtphost = 'smtp.office365.com' LET cnf.smtpport = '587' LET cnf.username = 'notificacion@marmotech.com.do' LET cnf.smtpauth = TRUE LET cnf.password = 'Lmmjvsd2020' LET s_protocol = 'smtp' # INFORMACION DEL EMAIL DESTINO LET rcpt_to ='jsoto@marmotech.com.do' LET subject = 'sin subject' LET body = 'PRUEBA' DISPLAY "data to:",rcpt_to," subj ",subject," body:",body," att:",attachment," user:",xusername, " host: ",xsmtphost, " port ",xsmtpport," pass ",xpassword #LET cnf.smtphost = xsmtphost #LET cnf.smtpport = xsmtpport #LET cnf.username = xusername LET cnf.senderemail = 'notificacion@marmotech.com.do' # LET cnf.smtpauth = TRUE # LET cnf.password = xpassword # LET s_protocol = 'smtp' CALL create_cxion_session() RETURNING sesion,password,s_protocol LET m_charset=getCharset() LET msg=MimeMessage.create(sesion) # Email Message creation : add recipients -> From, To, CC and BCC # LET cnf.senderemail = 'notificacion@marmotech.com.do' CALL msg.setFrom(InternetAddress.create(cnf.senderemail)) CALL msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(rcpt_to, FALSE)) CALL msg.setSubject(subject) # Email Message creation : create multipart (for text and attachments) LET multip=MimeMultipart.create() # Email Message creation : add body part LET mbodyp=MimeBodyPart.create() CALL mbodyp.setText(body, m_charset, "html") -- CALL mbodyp.setText(body, m_charset) IF attachment IS NOT NULL THEN CALL mbodyp.attachFile(attachment) END IF CALL multip.addBodyPart(mbodyp) CALL msg.setContent(multip) CALL show_debug_msg(1,"Email Message created.") # Sending the email CALL show_debug_msg(1,SFMT("Connecting to server %1 ...",cnf.hostname)) TRY LET trans = CAST(sesion.getTransport(s_protocol) AS SMTPTransport) CALL trans.connect(cnf.smtphost, cnf.username, password) CALL trans.sendMessage(msg,msg.getAllRecipients()) CALL trans.close() CALL show_debug_msg(1,SFMT("Email sent through server %1.",cnf.hostname)) CATCH CALL show_output_dialog("ERR") CALL mbox_ok("Error", SFMT("Error trying to send email through server %1 (code=%2)",cnf.hostname,STATUS), "stop") END TRY 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 PRIVATE FUNCTION show_debug_msg(lev,msg) DEFINE lev SMALLINT DEFINE msg STRING IF lev>=debug_level THEN DISPLAY msg END IF 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","
",0) LET logtext=strbuf.toString(),"
", # first adds the debug lines without additional format SFMT("",s_color),s_msg,"" # then adds the result message in color 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 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 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 generatemplate() DEFINE location STRING OPEN WINDOW w1 WITH FORM 'wform' INPUT BY NAME location CLOSE WINDOW w1 END FUNCTION