MuleSoft SMTP Email Connector

The Email Connector lets your Mule application send emails via SMTP and read emails via IMAP or POP3. Use it to send order confirmations, error alerts, daily reports, and workflow notifications directly from your integration flows.

Email Connector Configuration

Create a connector configuration for your email server. Most companies use Gmail (for development) or a corporate SMTP server like Microsoft Exchange or SendGrid in production.

SMTP Configuration — Gmail Example

Email SMTP Config Name: SMTP_Gmail_Config
  Host:      smtp.gmail.com
  Port:      587
  Username:  noreply@mycompany.com
  Password:  ${email.appPassword}  ← use an App Password, not account password
  TLS:       STARTTLS

Email SMTP Config — Corporate Exchange:
  Host:      smtp.mycompany.com
  Port:      25 (or 587 with TLS)
  Username:  mule-notifications@mycompany.com
  Password:  ${email.password}
  TLS:       None (if internal network) or STARTTLS

Sending a Simple Email

Order Confirmation Email Flow

[Database: INSERT order → returns orderId as 10042]
      │
      ▼
[Email: Send]
  Config:   SMTP_Gmail_Config
  From:     noreply@mycompany.com
  To:       #[vars.customerEmail]
  Subject:  "Your Order #[vars.orderId] has been placed!"
  Content:  text/html
  Body:
    <h2>Thank you for your order!</h2>
    <p>Order ID: <strong>#[vars.orderId]</strong></p>
    <p>Total: $#[payload.total]</p>
    <p>Estimated delivery: #[vars.deliveryDate]</p>

Sending Emails with Attachments

Daily Sales Report Email

[Scheduler: every day at 7 AM]
      │
      ▼
[Database: SELECT product, SUM(qty) FROM sales WHERE date = :today GROUP BY product]
      │
      ▼
[Transform: convert to CSV]
      │
      ▼
[Set Variable: reportCsv = payload as String]
      │
      ▼
[Email: Send]
  To:       managers@mycompany.com
  CC:       ceo@mycompany.com
  Subject:  "Daily Sales Report - #[now() as String {format:'yyyy-MM-dd'}]"
  Body:     "Please find today's sales report attached."
  Attachments:
    - Name:         daily_report.csv
      Content:      #[vars.reportCsv]
      Content-Type: text/csv

Sending to Multiple Recipients

[Email: Send]
  To:   ["alice@company.com", "bob@company.com", "carol@company.com"]
  CC:   ["manager@company.com"]
  BCC:  ["audit@company.com"]
  
  Or dynamically from a DataWeave expression:
  To:   #[vars.recipientList]
  (where vars.recipientList is ["a@x.com", "b@x.com"])

Reading Emails with IMAP

Use the IMAP configuration to read incoming emails. MuleSoft can poll an inbox, process each email, extract attachments, and trigger workflows based on email content.

IMAP Configuration and Read Flow

Email IMAP Config:
  Host:     imap.gmail.com
  Port:     993
  Username: orders@mycompany.com
  Password: ${email.imapPassword}
  TLS:      true

Flow: processIncomingOrderEmailsFlow

[Email: List Messages]
  Config:        IMAP config
  Folder:        INBOX
  Matcher:       subject contains "ORDER:"
  Mark as Read:  true
      │
      ▼ (payload = list of emails)
[For Each: process each email]
  │
  ├── [Set Variable: subject = payload.attributes.subject]
  ├── [Set Variable: sender  = payload.attributes.fromAddresses[0]]
  ├── [Transform: parse order number from subject]
  └── [Database: log email receipt for audit trail]

Error Handling for Email Operations

Error Handler:

On Error Propagate: EMAIL:CONNECTIVITY
  [Logger: "Cannot reach email server"]
  [Set Variable: httpStatus = 503]

On Error Propagate: EMAIL:AUTHENTICATION
  [Logger: "Email authentication failed — check credentials"]

On Error Continue: ANY
  [Logger: "Non-critical email failed: #[error.description]"]
  (do not fail the main flow if a notification email fails)

Using Email for Error Notifications

Add email alerts in global error handlers to notify operations teams when critical integrations fail. Include the error type, description, application name, and timestamp to make it easy to diagnose problems quickly.

Global Error Handler:
  On Error Propagate: ANY
    [Email: Send]
      To:      ops-team@mycompany.com
      Subject: "ALERT: MuleSoft Error in #[app.name]"
      Body:    "
        Application: #[app.name]
        Error Type:  #[error.errorType.identifier]
        Message:     #[error.description]
        Time:        #[now() as String]
        Flow:        #[flow.name]
      "

Template-Based Email Bodies

For professional HTML emails with consistent branding, store email templates as files in src/main/resources/email-templates/. Load the template with the File Connector, substitute variables using DataWeave string replacement, and pass the result to the Email Send component.

Template file (order-confirmation.html):
  <p>Hello {{customerName}},</p>
  <p>Your order {{orderId}} totaling {{orderTotal}} is confirmed.</p>

Flow:
[File: Read "classpath://email-templates/order-confirmation.html"]
      │
      ▼
[Transform: substitute variables]
%dw 2.0
output text/plain
---
payload 
  replace "{{customerName}}" with vars.customerName
  replace "{{orderId}}"      with vars.orderId
  replace "{{orderTotal}}"   with ("$" ++ vars.total as String)
      │
      ▼
[Email: Send] Content-Type: text/html, Body: #[payload]

Leave a Comment

Your email address will not be published. Required fields are marked *