Sending Emails via Gmail

Teneo Studio is very flexible in adding further functionalities to projects via Groovy or Java code. I would like to share with you an example on how an Email Sending functionality can be created. In this specific case, from a Gmail mailbox. Before you implement the code in your solution, you need to change the following configuration in your Gmail and Google account.

Gmail Account configuration

Please click the configuration icon at top-right and choose See all settings
all settings

Choose Forwarding and POP/IMAP, select Enable POP for all mail , then click on Save Changes

Please double check that the status has been changed to POP is enabled for all mail

Google Account configuration

Please click on your google profile at top right and choose Manage your Google Account
manage Google account

Confirm if you have already enabled 2-step Verification from Security > 2-Step Verification . If not, please enable it.

After 2-step Verification is enabled, please click on App passwords . Choose Mail and Windows Computer , then click on Generate

You will see a 16-digit password. You don’t need to remember it. Every time you need App password you can re-open this window and copy the password.

Now your Gmail account is ready for sending emails via Groovy code. Please move on to the next step: importing the required resource files into your solution.

Resource files to import in your solution

Please download the following jar files:

  • commons-email-1.5.jar, download from here;
  • javax.mail-1.6.2.jar, download from here;
  • javax.activation-1.2.0.jar, download from here

and import them into your solution.

Code for sending email

Please add the following code in Globals > Scripts > Solution loaded .

import javax.mail.*;
import org.apache.commons.mail.*;

class Email {
    public static void sendMail(String recipient, String subject, String message, String mailUser, String mailPassWord) throws EmailException {

         HtmlEmail mail = new HtmlEmail();

         mail.setHostName("smtp.gmail.com");
         mail.setSmtpPort(465);
         mail.setAuthenticator(new DefaultAuthenticator(mailUser, mailPassWord));
         mail.setSSLOnConnect(true);
         mail.setFrom(mailUser);
         mail.addTo(recipient);
         mail.setSubject(subject);
         mail.setHtmlMsg(message);

         mail.send();

    }
}

Use the following code to send emails. You can put the code in any kind of script nodes in your solution, for example session scope global scripts, flow script nodes or integrations.

Email.sendMail(sRecipient, sSubject, sMessage, sMailUser, sMailPassWord)

This method has 5 arguments:

  • sRecipient: The email address which will receive the email
  • sSubject: The subject of the email
  • sMessage: The body text of the email
  • sMailUser: The email sender
  • sMailPassWord: The password of the email sender. In case of Gmail, please use the App password instead of your Google account password.

Now the implementation of sending emails via Gmail in your solution has been completed. If you see the error message Sending the email to the following server failed while running your code, please check the configuration of your Gmail and Google account.

The code in this post is just an example on how you can set up email functionality in your Teneo solution. You can use similar code for other mailboxes by changing the host name in the code and the proper configuration in your mailbox. I hope this code example comes in handy for your project!

2 Likes