Sample code for 30+ languages & platforms
Java

Send a Bundle of Emails via SMTP

See more SMTP Examples

Demonstrates the Chilkat MailMan.SendBundle method, which sends each email in an EmailBundle. This is equivalent to calling SendEmail once for each email in the bundle. This example builds a bundle of two messages and sends them.

Background: Sending a bundle lets Chilkat reuse a single SMTP connection for all the messages, avoiding the connect/authenticate overhead of sending them one at a time — the same efficiency as the explicit open/send-loop/close pattern, in one call. It is handy for dispatching a queued batch of distinct messages (as opposed to one message to many recipients).

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.EmailBundle;
import com.chilkatsoft.chilkat2.MailMan;

public class ChilkatExample {

    public static void main(String[] args) {
        // Demonstrates the MailMan.SendBundle method, which sends each email in an EmailBundle.
        // This is equivalent to calling SendEmail once for each email in the bundle.

        MailMan mailman = new MailMan();

        // Configure the SMTP server connection.
        mailman.setSmtpHost("smtp.example.com");
        mailman.setSmtpPort(465);
        mailman.setSmtpSsl(true);
        mailman.setSmtpUsername("user@example.com");
        mailman.setSmtpPassword("myPassword");

        // Build a bundle of emails to send.
        EmailBundle bundle = new EmailBundle();

        Email email1 = new Email();
        email1.setSubject("Message 1");
        email1.setFrom("alice@example.com");
        email1.addTo("Bob", "bob@example.com");
        email1.setBody("This is the first message.");
        bundle.addEmail(email1);

        Email email2 = new Email();
        email2.setSubject("Message 2");
        email2.setFrom("alice@example.com");
        email2.addTo("Carol", "carol@example.com");
        email2.setBody("This is the second message.");
        bundle.addEmail(email2);

        // Send every email in the bundle.

        try {
            mailman.sendBundle(bundle);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Sent " + bundle.getMessageCount() + " emails.");

        // Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
        // connects and authenticates -- using the property settings above -- whenever a server
        // operation requires it.  Calling the explicit connect/authenticate methods can still be
        // helpful to determine whether a failure occurs while connecting or while authenticating.
    }
}