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 ?
send email without resource
Moderators: Jon, Steve, Ian, Dave
-
- Moderator
- Posts: 1342
- Joined: Wed Sep 12, 2007 12:49 pm
-
- Ebase User
- Posts: 649
- Joined: Mon Dec 09, 2013 6:37 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:
which accepts the subject and body so I can easily build a custom email without having to bother with an email resource.
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)
0 x
-
- Ebase User
- Posts: 649
- Joined: Mon Dec 09, 2013 6:37 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:
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:
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 {
}
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
-
- Moderator
- Posts: 1342
- Joined: Wed Sep 12, 2007 12:49 pm
-
- Ebase User
- Posts: 649
- Joined: Mon Dec 09, 2013 6:37 pm
-
- Ebase User
- Posts: 649
- Joined: Mon Dec 09, 2013 6:37 pm
-
- Moderator
- Posts: 1342
- Joined: Wed Sep 12, 2007 12:49 pm
-
- Ebase User
- Posts: 649
- Joined: Mon Dec 09, 2013 6:37 pm
Jon,
Here's the working function for sending anonymous email. I'm not sure what code here would be unnecessary:
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 3 guests