Base64 in Email and MIME: How Attachments Work
Have you ever wondered how you can attach a 5MB PDF to an email when the underlying email protocol (SMTP) was designed in the 1980s to only handle plain 7-bit ASCII text?
The answer is MIME (Multipurpose Internet Mail Extensions) and Base64 encoding.
The SMTP Limitation
The Simple Mail Transfer Protocol (SMTP) fundamentally expects text. If you try to send raw binary bytes (like a compiled JPEG image) directly over a standard SMTP socket, the servers will likely misinterpret the bytes as control characters, corrupting the file or crashing the transmission.
The MIME Standard
MIME was introduced to allow emails to contain multiple parts (text, HTML, and file attachments). When an email client attaches a file, it reads the binary data of the file and encodes it into a Base64 string.
The 76-Character Limit
MIME enforces a specific, strict rule on its Base64 implementation: Lines must not exceed 76 characters in length. To achieve this, a carriage return and line feed (\r\n) are inserted into the Base64 string every 76 characters.
Here is what the raw source of an email with a tiny image attachment looks like:
Content-Type: image/jpeg; name="photo.jpg"
Content-Disposition: attachment; filename="photo.jpg"
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEASABIAAD/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEA
AIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAAKADAAQAAAAB
AAAAOAAAAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJ
CAgKCAcHCg0KCgsNDQ0NBzkQERQNEQ4NDQA=
Decoding MIME Base64 in Code
When you are writing software that parses incoming emails (like a customer support ticketing system), you must extract this text block and decode it back into the binary file.
Because MIME Base64 contains hard line breaks (\r\n), strict standard decoders will often throw an "invalid character" error if you attempt to decode it directly.
The Solution: You must either use a dedicated MIME decoder, or strip all whitespace from the string before decoding it.
import java.util.Base64;
// Using Java's built-in MIME decoder which safely ignores
Base64.Decoder mimeDecoder = Base64.getMimeDecoder();
byte[] fileBytes = mimeDecoder.decode(rawMimeString);
// Node.js Buffer ignores whitespace automatically during Base64 decoding
const fileBytes = Buffer.from(rawMimeString, 'base64');
import base64
# Python's b64decode ignores newline characters safely
file_bytes = base64.b64decode(raw_mime_string)
The Cost of Email Attachments
Because Base64 fundamentally uses 4 ASCII characters to represent every 3 bytes of binary data, it incurs a strict 33% size overhead.
This means a 10MB PDF on your hard drive will consume approximately 13.3MB of network bandwidth when sent as an email attachment. Furthermore, the addition of the \r\n line breaks every 76 characters adds an additional ~2% overhead.
This mathematical inflation is why email providers enforce strict attachment size limits (usually 25MB). A 25MB limit prevents the encoded MIME payload from exceeding reasonable server processing limits.
Conclusion
Base64 is the invisible glue that holds modern email together. By standardizing the way binary files are represented as 76-character blocks of ASCII text, MIME ensures that images, PDFs, and archives can safely traverse the ancient, text-only infrastructure of SMTP.
If you need to manually inspect a suspicious email attachment, copy the Base64 block from the email source and paste it into our Base64 Decoder.