Sample code for 30+ languages & platforms
Java

Render an Email to MIME in a StringBuilder

See more SMTP Examples

Demonstrates the Chilkat MailMan.RenderToMimeSb method, which renders an Email object as MIME text and appends the result to a StringBuilder, without sending the email. This example renders a message into a StringBuilder and prints it.

Background: This is the StringBuilder version of RenderToMime. Appending into a StringBuilder is more efficient when the MIME is large or when you plan to further inspect or manipulate it — searching, replacing, or accumulating additional text — without creating extra intermediate string copies.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        // Demonstrates the MailMan.RenderToMimeSb method, which renders an Email object as MIME text
        // and appends the result to a StringBuilder (without sending the email).

        MailMan mailman = new MailMan();

        // Build the email to render.
        Email email = new Email();
        email.setSubject("Rendered email");
        email.setFrom("alice@example.com");
        email.addTo("Bob", "bob@example.com");
        email.setBody("This message is rendered to MIME in a StringBuilder.");

        // Render the MIME into a StringBuilder.
        StringBuilder sbMime = new StringBuilder();

        try {
            mailman.renderToMimeSb(email, sbMime);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println(sbMime.getAsString());
    }
}