Sample code for 30+ languages & platforms
Java

Render an Email to MIME Bytes in a BinData

See more SMTP Examples

Demonstrates the Chilkat MailMan.RenderToMimeBd method, which renders an Email object as MIME bytes and appends the result to a BinData, without sending the email. This example renders a message into a BinData and prints the byte count.

Background: This is the binary version of RenderToMime. A BinData holds raw bytes, which is the right container when you want the rendered MIME as binary — to write it directly to a file or socket, hash it, or hand it to another API expecting a byte buffer — rather than as text.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        // Demonstrates the MailMan.RenderToMimeBd method, which renders an Email object as MIME
        // bytes and appends the result to a BinData (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 bytes in a BinData.");

        // Render the MIME into a BinData.
        BinData bdMime = new BinData();

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

        System.out.println("Rendered MIME size (bytes) = " + bdMime.getNumBytes());
    }
}