Giap Hiep

I'm Giap Hiep

I'm a web developer, a gymer. I enjoy share something i know that help people's work!
Giap Hiep

SMTP email attachment without saving file without dependency Spring Boot

Here is samle code & instuctions to send email attachment by converting the InputStream to byte array then converting byte array to Base64.

CSV atachment data

Lets initial data for csv atachment

List<String[]> data = new ArrayList<>();
        data.add(new String[]
                {"Id", "Content", "Score"});
        data.add(new String[]
                {"1", "Alpha", "10"});
        data.add(new String[]
                {"2", "Beta", "20"});

Load csv file data into StringWriter

StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
private String convertStringToCSV(String[] data) {
    return Stream.of(data)
            .map(s -> escapeSpecialCharacters(s))

Send email attachment

// setup SMTP
Properties props = new Properties();
props.put("", "smtp host");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "port");

Session session = Session.getInstance(props,
        new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("smtp account", "smtp password");

Message message = new MimeMessage(session);

addListRecipient(sendToEmails, message, Message.RecipientType.TO);

message.setFrom(new InternetAddress("sender email"));

// StringWriter to InputStream
InputStream inputStream = new ByteArrayInputStream(stringWriter.toString().getBytes());
// Convert to byte array
byte[] byteArray = IOUtils.toByteArray(inputStream);
// Convert to Base64
byte[] base64ByteArray = java.util.Base64.getEncoder().encode(byteArray);

InternetHeaders fileHeaders = new InternetHeaders();
fileHeaders.setHeader(StringConst.CONTENT_TRANSFER_ENCODING, StringConst.BASE64);

// Body content
MimeBodyPart mimeContent = new MimeBodyPart();
mimeContent.setContent("Email content here", "text/html;charset="utf-8"");

// Attachment
MimeBodyPart mimeAttachment = new MimeBodyPart(fileHeaders, base64ByteArray);

Multipart multipart = new MimeMultipart();