import ballerina/email;
import ballerina/io;

public function main() {
    // Creates an SMTP client with the connection parameters, host, username,
    // and password. Default port number `465` is used over SSL with these
    // configurations.
    email:SmtpClient smtpClient = new ("smtp.email.com", "sender@email.com"
        , "pass123");

    // Define the email that is required to be sent.
    email:Email email = {
        // "TO", "CC" and "BCC" address lists are added as follows.
        // Only "TO" address list is mandatory out of these three.
        to: ["receiver1@email.com", "receiver2@email.com"],
        cc: ["receiver3@email.com", "receiver4@email.com"],
        bcc: ["receiver5@email.com"],
        // Subject of the email is added as follows. This field is mandatory.
        subject: "Sample Email",
        // Body content of the email is added as follows.
        // This field is mandatory.
        body: "This is a sample email.",
        // Email author's address is added as follows. This field is mandatory.
        'from: "author@email.com",
        // Email sender service address is added as follows.
        // This field is optional. `Sender` is same as the `'from` when the
        // email author himself sends the email.
        sender: "sender@email.com",
        // List of recipients when replying to the email is added as follows.
        // This field is optional. These addresses are required when the emails
        // are to be replied to some other address(es) other than the sender or
        // the author.
        replyTo: ["replyTo1@email.com", "replyTo2@email.com"]
    };

    // Send the email with the client.
    email:Error? response = smtpClient->send(email);
    if (response is email:Error) {
        io:println("Error while sending the email: "
            + <string> response.detail()["message"]);
    }

}
import ballerina/email;
import ballerina/io;

public function main() {
    // Create the client with the connection parameters, host, username, and
    // password. An error is received in failure. Default port number `995` is
    // used over SSL with these configurations.
    email:PopClient|email:Error popClient = new ("pop.email.com",
        "reader@email.com", "pass456");
    if (popClient is email:PopClient) {
        // Read the first unseen email received by the POP3 server. Nil is
        // returned when there are no new unseen emails. In error cases an
        // error is returned.
        email:Email|email:Error? emailResponse = popClient->read();
        if (emailResponse is email:Email) {
            io:println("Email Subject: ", emailResponse.subject);
            io:println("Email Body: ", emailResponse.body);
        // When no emails are available in the server, nil is returned.
        } else if (emailResponse is ()) {
            io:println("There are no emails in the INBOX.");
        } else {
            io:println("Error while getting getting response: "
                + <string> emailResponse.detail()["message"]);
        }
    } else {
        io:println("Error while creating client: "
            + <string> popClient.detail()["message"]);
    }
}
import ballerina/email;
import ballerina/io;

public function main() {
    // Create the client with the connection parameters, host, username, and
    // password. An error is received in failure. Default port number `993` is
    // used over SSL with these configurations.
    email:ImapClient|email:Error imapClient = new ("imap.email.com",
        "reader@email.com", "pass456");
    if (imapClient is email:ImapClient) {
        // Read the first unseen email received by the IMAP4 server. Nil is
        // returned when there are no new unseen emails. In error cases an
        // error is returned.
        email:Email|email:Error? emailResponse = imapClient->read();
        if (emailResponse is email:Email) {
            io:println("Email Subject: ", emailResponse.subject);
            io:println("Email Body: ", emailResponse.body);
        // When no emails are available in the server, nil is returned.
        } else if (emailResponse is ()) {
            io:println("There are no emails in the INBOX.");
        } else {
            io:println("Error while getting getting response: "
                + <string> emailResponse.detail()["message"]);
        }
    } else {
        io:println("Error while creating client: "
            + <string> imapClient.detail()["message"]);
    }
}

Send and Receive Emails

The Email Connector is used to send (with SMTP) and receive (with POP3 or IMAP4) emails using the SSL or STARTTLS protocols. This sample includes sending and receiving emails with default configurations over SSL using default ports.

import ballerina/email;
import ballerina/io;
public function main() {
    email:SmtpClient smtpClient = new ("smtp.email.com", "sender@email.com"
        , "pass123");

Creates an SMTP client with the connection parameters, host, username, and password. Default port number 465 is used over SSL with these configurations.

    email:Email email = {

Define the email that is required to be sent.

        to: ["receiver1@email.com", "receiver2@email.com"],
        cc: ["receiver3@email.com", "receiver4@email.com"],
        bcc: ["receiver5@email.com"],

“TO”, “CC” and “BCC” address lists are added as follows. Only “TO” address list is mandatory out of these three.

        subject: "Sample Email",

Subject of the email is added as follows. This field is mandatory.

        body: "This is a sample email.",

Body content of the email is added as follows. This field is mandatory.

        'from: "author@email.com",

Email author’s address is added as follows. This field is mandatory.

        sender: "sender@email.com",

Email sender service address is added as follows. This field is optional. Sender is same as the 'from when the email author himself sends the email.

        replyTo: ["replyTo1@email.com", "replyTo2@email.com"]
    };

List of recipients when replying to the email is added as follows. This field is optional. These addresses are required when the emails are to be replied to some other address(es) other than the sender or the author.

    email:Error? response = smtpClient->send(email);
    if (response is email:Error) {
        io:println("Error while sending the email: "
            + <string> response.detail()["message"]);
    }

Send the email with the client.

}
# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run send_email.bal
# Check the inbox to view the email.
import ballerina/email;
import ballerina/io;
public function main() {
    email:PopClient|email:Error popClient = new ("pop.email.com",
        "reader@email.com", "pass456");
    if (popClient is email:PopClient) {

Create the client with the connection parameters, host, username, and password. An error is received in failure. Default port number 995 is used over SSL with these configurations.

        email:Email|email:Error? emailResponse = popClient->read();
        if (emailResponse is email:Email) {
            io:println("Email Subject: ", emailResponse.subject);
            io:println("Email Body: ", emailResponse.body);

Read the first unseen email received by the POP3 server. Nil is returned when there are no new unseen emails. In error cases an error is returned.

        } else if (emailResponse is ()) {
            io:println("There are no emails in the INBOX.");
        } else {
            io:println("Error while getting getting response: "
                + <string> emailResponse.detail()["message"]);
        }
    } else {
        io:println("Error while creating client: "
            + <string> popClient.detail()["message"]);
    }
}

When no emails are available in the server, nil is returned.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run receive_email.bal
# Subject and the content body of the email would be printed.
import ballerina/email;
import ballerina/io;
public function main() {
    email:ImapClient|email:Error imapClient = new ("imap.email.com",
        "reader@email.com", "pass456");
    if (imapClient is email:ImapClient) {

Create the client with the connection parameters, host, username, and password. An error is received in failure. Default port number 993 is used over SSL with these configurations.

        email:Email|email:Error? emailResponse = imapClient->read();
        if (emailResponse is email:Email) {
            io:println("Email Subject: ", emailResponse.subject);
            io:println("Email Body: ", emailResponse.body);

Read the first unseen email received by the IMAP4 server. Nil is returned when there are no new unseen emails. In error cases an error is returned.

        } else if (emailResponse is ()) {
            io:println("There are no emails in the INBOX.");
        } else {
            io:println("Error while getting getting response: "
                + <string> emailResponse.detail()["message"]);
        }
    } else {
        io:println("Error while creating client: "
            + <string> imapClient.detail()["message"]);
    }
}

When no emails are available in the server, nil is returned.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run receive_email.bal
# Subject and the content body of the email will be printed.