send email without resource

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

send email without resource

#1

Postby Segi » Fri Feb 10, 2017 11:25 pm

I seem to remember a post on this forum on how you can send an email without using an email resource.

Can anyone provide a template on how to do this ?
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Mon Feb 13, 2017 9:48 am

Try this: http://forum.ebasetech.com/forum/viewtopic.php?t=776. Why do you need to do this?
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#3

Postby Segi » Mon Feb 13, 2017 4:40 pm

Jon,

I have a function that gets called whenever a critical error occurs in any of my applications which will trigger an email to me so I can identify the problem. I want to send the email without using an email resource because I don't want to have to create an email resource that gets attached to every single form in order to be available to that application.

Instead, I have a function defined as:

Code: Select all

function sendAdminEmail(subject,body)
which accepts the subject and body so I can easily build a custom email without having to bother with an email resource.
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#4

Postby Segi » Mon Feb 13, 2017 5:16 pm

Jon,

We have an internal SMTP server that I use for email alerts that does not require authentication. I changed the code to read as follows:

Code: Select all

var _java = JavaImporter(javax.mail, javax.mail.internet, java.util); 
/* --------------------- 
   Create smtp variables 
 ---------------------*/ 
   var smtpHost = 'MY_SMTP_SERVER_IP_ADDRESS_GOES_HERE'; 
   var smtpPort = 25; 

/* ------------------------- 
   Populate smtp properities 
 -------------------------*/    
   var props = new _java.Properties(); 
   props.put("mail.smtp.host", smtpHost); 
   props.put("mail.smtp.port", smtpPort); 
   props.put("mail.smtp.auth", "false"); 
   props.put("mail.smtp.socketFactory.port", smtpPort); 
   props.put("mail.smtp.socketFactory.class", 
             "javax.net.ssl.SSLSocketFactory"); 
   props.put("mail.smtp.socketFactory.fallback", "false"); 
   props.put("mail.smtp.ssl", "false"); 
/* -------------------------- 
   Create a new SMTP session 
 --------------------------*/ 
   var session = _java.Session.getInstance(props); 
/* ----------------------------- 
   Create the message to be sent 
 -----------------------------*/    
   var message = _java.MimeMessage(session); 
   message.setFrom( 
       new _java.InternetAddress("FROM EMAIL ADDRESS GOES HERE","FROM NAME GOES HERE") 
   ); 
   message.setRecipients( 
       _java.Message.RecipientType.TO, 
       [new _java.InternetAddress("FROM EMAIL ADDRESS GOES HERE", true)] 
   ); 
   message.setText(body); 
   message.setSubject(subject); 
   message.setSentDate(new Date()); 
/* ------------------------------------------------ 
   Connect to the SMTP session and sent the message 
 ------------------------------------------------*/    
   var transport = session.getTransport("smtps"); 

   try { 
        transport.connect (smtpHost, smtpPort,"",""); 
        transport.sendMessage(message, message.getAllRecipients()); 
        transport.close(); 
   } catch (e) {
        alert("Failed to send email with the error " + e);	
   } finally {
   	
   }
As you can see, I removed the username and password and changed the property mail.smtp.auth from true to false.

I put placeholders for the actual IP of the SMTP server, the from name and the from email address but the correct values are in my actual function.

The catch catches this error:

Code: Select all

Failed to send email with the error JavaException: javax.mail.MessagingException: Could not connect to SMTP host: MY_SMTP_SERVER_IP_ADDRESS_GOES_HERE, port: 25
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#5

Postby Jon » Mon Feb 13, 2017 5:33 pm

The "mail.smtp.host" property needs to be set with the ip address or host name of the email server.
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#6

Postby Segi » Mon Feb 13, 2017 5:34 pm

Jon,

It is. the var smtpHost contains the IP address. I set it in the line props.put("mail.smtp.host", smtpHost); which is already in my code
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#7

Postby Segi » Mon Feb 13, 2017 7:10 pm

I got it to work by commenting out the mail.smtp.socketFactory.class property line.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#8

Postby Jon » Tue Feb 14, 2017 10:33 am

Well done. I'm not very fond of this example - I think it contains a lot of unnecessary code. But I don't have a better example to hand..
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#9

Postby Segi » Tue Feb 14, 2017 4:51 pm

Jon,

Here's the working function for sending anonymous email. I'm not sure what code here would be unnecessary:

Code: Select all

function sendAdminEmail(subject,body) {
     var _java = JavaImporter(javax.mail, javax.mail.internet, java.util); 
     //Create smtp variables 
     var smtpHost = getSystemVar("SMTP_Address"); // Get the system var value of the SMTP server instead of hardcoding it
     smtpHost=smtpHost.substring(0,smtpHost.indexOf(":")); // The SMTP server is stored in the form IPADDRESS:PORT This returns everything before the :
     
     var smtpPort = smtpHost.substring(smtpHost.indexOf(":") + 1); // The SMTP server is stored as IPADDRESS:PORT This returns everything after the :
     var systemAdminName=getSystemVar("SystemAdminName");
     
     var systemAdminEmailAddress=getSystemVar("SystemAdminEmailAddress");

     //Populate smtp properities 
     var props = new _java.Properties(); 
     props.put("mail.smtp.host", smtpHost); 
     props.put("mail.smtp.port", smtpPort); 
     props.put("mail.smtp.auth", "false"); 
     props.put("mail.smtp.socketFactory.port", smtpPort); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 

     //Create a new SMTP session 
     var session = _java.Session.getInstance(props); 

     //Create the message to be sent 

     var message = _java.MimeMessage(session); 
     message.setFrom(new _java.InternetAddress(systemAdminEmailAddress,systemAdminName)); 

     message.setRecipients(_java.Message.RecipientType.TO,[new _java.InternetAddress(systemAdminEmailAddress, true)]); 
     message.setText(body); 
     message.setSubject(subject); 
     message.setSentDate(new Date()); 

     try { 
          _java.Transport.send(message);
   } catch (e) {
        alert("Failed to send email with the error " + e);	
   } finally {   	
   }
}
0 x


Who is online

Users browsing this forum: No registered users and 44 guests