How to send an email from a Talend Batch Job
The examples below are created using:
- Talend Data Fabric 6.3.1
- Oracle JDK Build 1.8
- Windows 7
- Mailjet SMTP server
An SMTP server setup is required. For this article, we are using the free Mailjet SMTP server.Examples
The usage of tSendMail component will be explained with the help of following examples.Email With Plain Text
For this scenario, we have the following job with two components. The tSendMail component will be sending an email for each row that flows through row2 .
Follow the instructions below to create the job:
- Drop the following components from your Palette to the design workspace: tFixedFlowInput, tSendMail
- Define the tFixedFlowInput component as follows
- Define the tSendMail component as follows
- change the To, From, CC and Bcc address as per the requirements.
- check the option Show Sender's name if you want to the sender's name to appear in the email message instead of just the from email address. Showing the sender's name will show a more professional email look and feel to your emails. If it is left blank, then the from email address will show.
- Cc and BCc fields are optional and can be left blank
- Define the SMTP configuration.
- We are using context variables to configure our components in our examples. The context variables used are as shown below.
- Execute the job and an email will be triggered with 'Sending a test mail' as body.
- Edit the number of rows in the tFixedFlow component to 2 rows, and run the job again. This time you will receive 2 emails.
Email Blacklist Alert
Be careful when sending external emails with your company account. Sending too many emails with the same subject and body will flag your company domain as spam and will cause issues for your business. This should be used for internal emails only. For all external emails, it is recommended that you use a proper emailing system like Marketo or others.Email With Attachment
In this example, we will look at sending an email with a file attachment.
We will create a new job as shown above. Follow the instructions below to do so:
- Drop the following four additional components to your design canvas : tCreateTemporaryFile, tRowGenerator, tFileOutputDelimited, tSendMail
- Connect them as above. Note: If you do not have the ErrorControl section, you can add this later.
- Configure the tCreateTemporaryFile as shown below.
- Define the schema for the tRowGenerator with two columns as shown below.
- Define tFileOutputDelimited basic settings as shown below. This will write the records generated from tRowGenerator into the temporary file created. Note that temporary files gets deleted when the job execution is complete and the java proces is exited.
- Define the tSendMail component as follows. Notice the details given in the attachments section. We can force the job to die if there is no file to attach. As a best practice, we will check the existance of the files to be attached first before sending an email. This can be done through the Run If trigger link.
- Execute the job. An email will be sent with a file as attachment.
- After the job finishes executing, the temporary file is deleted. We can also force the deletion of the temporary file if your OS handles temporary files differently.
It is very easy to send an email with an HTML message format. To achieve this, perform the following:
- Modify the first job to convert the plain text mail to HTML format.
- Go to the advanced settings of the tSendMail component, and change "MIME subtype from the 'text' MIME type" to HTML from "plain text" (as shown below).
- Switch back to the Basic Settings tab. In the "Message" text box, add your HTML construct for the message body.
- Execute the job to send an email in HTML format.
In this example, we will look at how to configure the tSendMail component to connect to Gmail SMTP server. We will modify any of the job above. Perform the following:
- Open the job and change the default context paramenter values as given below. These values are used in the tSendMail component.
- Change the from address in the component to match the gmail address of the account.
- Check SSL Support option to enable the component to open an SSL connection when communicating with Gmail SMTP server.
- Enter the username/password required for connecting to the SMTP server.
- Execute the job to send an email from a gmail account.
SSL and TLS Options
Note that we cannot configure the SSL Support and STARTTLS options through context variables. Hence, we should always mirror the production SMTP configuration in our DEV environment so that we design our jobs to match the target production environment. Checking these options will instruct the Talend Studio to include additional code into the generated Java code, and hence these options cannot be controlled by context variables.SSL, TLS and STARTTLS
Using a secure connection is important to protect your data (email address, file attachments, etc.). Developers very often confuse the use of SSL vs TLS vs STARTTLS.
SSL and TLS are both protocols for encrypting a communication channel between 2 computers. TLS is the successor of SSL and the terms SSL and TLS are often used interchangeably. When you check the SSL option, the JavaMail API will use the com.sun.net.ssl.internal.ssl.Provider . This gets added to the Java security provider for the job. You can check this by switching to the Code tab and looking at the code generated.
- STARTTLS is a way to take an existing insecure connection and upgrade it to a secure connection using SSL/TLS. This is generally driven by the server capability to establish a secure connection. The client, i.e. the tSendMail component in this case, is only recommending to the server to elevate to SSL/TLS if the server can support. Very often the server may not be able to support this elevated security and the connection is established in plain text. Hence, STARTTLS does not enforced the connection to be encrypted. It is perfectly normal to have plain text communication on the channel if the server was not able to elevate to SSL/TLS. Note that despite having TLS in the name, STARTTLS doesn't mean you have to use TLS, it can also use SSL to secure the connection When you check the STARTTLS option, the JavaMail API will use set the mail.smtp.starttls.enable property to true . You can check the generated code to see how this is done.
Please refer to the JavaMail API documentation for all the options possible when sending email with the Java API. As you will see, there are too many options in the JavaMail API which makes it confusing to use. Talend removes the confusion and increase your productivity by providing an easy way to send email while leveraging the important options that matters most.