Skip to main content

Actions

The ballerina/email package exposes the following clients:

ClientPurpose
SMTP ClientSends emails via SMTP with support for text/HTML bodies, attachments, and custom headers.
IMAP ClientReceives emails from an IMAP server with configurable folder and timeout.
POP ClientReceives emails from a POP3 server with configurable folder and timeout.

For event-driven integration, see the Trigger Reference.


SMTP client​

Sends emails via SMTP with support for text/HTML bodies, attachments, and custom headers.

Configuration​

FieldTypeDefaultDescription
hoststringRequiredHost address of the SMTP server (e.g., smtp.gmail.com).
usernamestring?()Username for SMTP authentication.
passwordstring?()Password for SMTP authentication.
portint465Port number of the SMTP server.
securitySecuritySSLType of security channel (SSL, START_TLS_AUTO, START_TLS_ALWAYS, or START_TLS_NEVER).
secureSocketSecureSocket()SSL/TLS secure socket configuration.

Initializing the client​

import ballerina/email;

configurable string senderAddress = ?;
configurable string senderPassword = ?;

email:SmtpClient smtpClient = check new ("smtp.gmail.com", senderAddress, senderPassword);

Operations​

Send operations​

sendMessage

Sends an email using a structured Message record.

Parameters:

NameTypeRequiredDescription
emailMessageYesThe email message record containing to, subject, body, and other fields.

Returns: Error?

note

If contentType is set in the Message record, it must be a text/* MIME type (e.g., "text/plain", "text/html"). Non-text content types will return an email:Error at runtime.

Sample code:

email:Message emailMessage = {
to: "[email protected]",
subject: "Sample Email Title",
body: "This is a sample email text body."
};
check smtpClient->sendMessage(emailMessage);
send

Sends an email using individual parameters for convenience.

Parameters:

NameTypeRequiredDescription
tostring|string[]YesRecipient email address(es).
subjectstringYesSubject of the email.
'fromstringYesSender's email address.
bodystringYesText body of the email.
htmlBodystringNoHTML body of the email.
ccstring|string[]NoCC recipient address(es).
bccstring|string[]NoBCC recipient address(es).
replyTostring|string[]NoReply-To address(es).
senderstringNoSender address (if different from from).
contentTypestringNoContent type of the body.
headersmap<string>NoCustom email headers.
attachmentsmime:Entity|Attachment|(mime:Entity|Attachment)[]NoFile attachments.

Returns: Error?

Sample code:

check smtpClient->send(
"[email protected]",
"Meeting Reminder",
"[email protected]",
"Don't forget about our meeting tomorrow at 10 AM.",
cc = "[email protected]"
);

IMAP client​

Receives emails from an IMAP server with configurable folder and timeout.

Configuration​

FieldTypeDefaultDescription
hoststringRequiredHost address of the IMAP server (e.g., imap.gmail.com).
usernamestringRequiredUsername for IMAP authentication.
passwordstringRequiredPassword for IMAP authentication.
portint993Port number of the IMAP server.
securitySecuritySSLType of security channel (SSL, START_TLS_AUTO, START_TLS_ALWAYS, or START_TLS_NEVER).
secureSocketSecureSocket()SSL/TLS secure socket configuration.

Initializing the client​

import ballerina/email;

configurable string imapHost = ?;
configurable string username = ?;
configurable string password = ?;

email:ImapClient imapClient = check new (imapHost, username, password);

Operations​

Receive operations​

receiveMessage

Reads an email message from the specified folder. Returns () if no unread emails are available.

Parameters:

NameTypeRequiredDescription
folderstringNoMailbox folder to read from (default: "INBOX").
timeoutdecimalNoTimeout in seconds to wait for an email (default: 30).

Returns: Message|Error?

Sample code:

email:Message? message = check imapClient->receiveMessage();

Sample response:

{"to": "[email protected]", "subject": "Hello", "from": "[email protected]", "body": "Hi there, this is a test email.", "contentType": "text/plain"}
close

Closes the IMAP client connection and releases resources.

Returns: Error?

Sample code:

check imapClient->close();

POP client​

Receives emails from a POP3 server with configurable folder and timeout.

Configuration​

FieldTypeDefaultDescription
hoststringRequiredHost address of the POP3 server (e.g., pop.gmail.com).
usernamestringRequiredUsername for POP3 authentication.
passwordstringRequiredPassword for POP3 authentication.
portint995Port number of the POP3 server.
securitySecuritySSLType of security channel (SSL, START_TLS_AUTO, START_TLS_ALWAYS, or START_TLS_NEVER).
secureSocketSecureSocket()SSL/TLS secure socket configuration.

Initializing the client​

import ballerina/email;

configurable string popHost = ?;
configurable string username = ?;
configurable string password = ?;

email:PopClient popClient = check new (popHost, username, password);

Operations​

Receive operations​

receiveMessage

Reads an email message from the specified folder. Returns () if no unread emails are available.

Parameters:

NameTypeRequiredDescription
folderstringNoMailbox folder to read from (default: "INBOX").
timeoutdecimalNoTimeout in seconds to wait for an email (default: 30).

Returns: Message|Error?

Sample code:

email:Message? message = check popClient->receiveMessage();

Sample response:

{"to": "[email protected]", "subject": "Hello", "from": "[email protected]", "body": "Hi there, this is a test email.", "contentType": "text/plain"}
close

Closes the POP3 client connection and releases resources.

Returns: Error?

Sample code:

check popClient->close();