Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Sending Emails with the Email Connector

The Email connector can send messages over SMTP and SMTPS.

Configuring the Connector

Separate configuration types are available for listing emails stored in a server:

  • SMTP

  • SMTPS (secured by TLS)

SMTP Configuration Type

Here is an example of an SMTP configuration:

SMTP Configuration

          
       
1
2
3
<email:smtp-config name="smtp">
    <email:stmp-connection host="smtp.gmail.com" port="995" user="pablo.musumeci@mulesoft.com" password="#netherlands!"/>
</email:smtp-config>

Secured Configuration Type

The SMTPS connection type provides a TLS Configuration field in which you can set the TLS context. The configuration enables SSL/TLS encryption and sends encrypted messages over the secured version of the SMTP protocol, SMTPS.

Here is an SMTPS example:

SMTP Configuration with TLS

          
       
1
2
3
4
5
6
7
8
<email:smtp-config name="smtp">
    <email:stmps-connection host="pop.gmail.com" port="995" user="pablo.musumeci@mulesoft.com" password="#netherlands!"/>
        <tls:context enabledProtocols="TLSv1.2,SSLv3">
            <tls:key-store path="aKeystore" password="password"/>
            <tls:trust-store path="aTruststore.jks" password="changeit"/>
        </tls:context>
    </email:smtps-connection>
</email:smtp-config>

Sending an Email

This example that sends an email:


         
      
1
2
3
4
5
6
7
8
9
<email:send config-ref="smtp" subject="IMPORTANT!" fromAddress="musumeci@mulesoft.com">
    <email:to-addresses>
        <email:to-address value="ale.gamarra@mulesoft.com"/>
        <email:to-address value="agustin.celentandios@mulesoft.com"/>
    </email:to-addresses>
    <email:body contentType="text/html">
        <email:content>"<h1>Hello this is an important message</h1>"</email:content>
    </email:body>
</email:send>

In the example above, to-addresses contains the primary recipient addresses of the outgoing email. The fromAddress attribute is the email sender address, and the subject is the email subject.

The body is composed of content text and a contentType that specifies the mime type of the content (for example, text/xml or text/plain). If no body is specified, the content of the message payload is used by default. If that payload is not text, the operation will fail with an EMAIL:SEND error.

Sending Attachments

When sending attachments over SMTP, you need to use DataWeave to handle the attachments.

In this example, a flow reads a JSON file using the File connector, then uses the Email connector to send the contents of the file as an attachment over SMTP:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<flow name="attachment">
  <file:read path="/file.json"/>
  <email:send config-ref="config">
      <email:to-addresses>
          <email:to-address value="agusto.celentandios@mulesoft.com"/>
      </email:to-addresses>
      <email:body>
          <email:content>Sending attachments!</email:content>
      </email:body>
      <email:attachments>
        #[{
          'json-attachment' : payload
        }]
      </email:attachments>
  </email:send>
</flow>

As you can see, email:attachments expects a DataWeave expression in which each element is an attachment. For instance, the example above adds a new attachment named json-attachment. Notice that payload is the content of the JSON file that was read by the File connector.