How to embed base64 image in email using javamail

I am trying to send an email from javamail with an embedded base64 image (img alt = 'image PNG' src = 'data: image / png; base64, iVBORw0KGgoAAAANSUhEUgA ... AElFTkSuQmCC' ")

It works with a small image, but when the image is larger, the image does not appear in the lotus note.

Here is a piece of code

Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage( mailSession );
message.setSubject( subject );
message.setFrom( new InternetAddress( me) );
message.setContent( bodyWithEmbeddedBase64Image, "text/html" );
transport.connect();
transport.sendMessage( message, message.getAllRecipients() );
transport.close();`

I would like to use PreencodedMimeBodyPart to test it, but I don’t know how to use it. Can someone help me :) :)

+3
source share
2 answers

Ok guys, I found the answers, I don’t know if I am doing the right thing, but it works.

Here is my code:

private static final Pattern imgRegExp  = Pattern.compile( "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>" );
public send(email) throws Exception{

   Map<String, String> inlineImage = new HashMap<String, String>();
   String body = email.getBody();
   final Matcher matcher = imgRegExp.matcher( body );
   int i = 0;
   while ( matcher.find() ) {
      String src = matcher.group();
      if ( body.indexOf( src ) != -1 ) {
         String srcToken = "src=\"";
         int x = src.indexOf( srcToken );
         int y = src.indexOf( "\"", x + srcToken.length() );
         String srcText = src.substring( x + srcToken.length(), y );
         String cid = "image" + i;
         String newSrc = src.replace( srcText, "cid:" + cid );
         inlineImage.put( cid, srcText.split( "," )[1] );
         body = body.replace( src, newSrc );
         i++;
      }
   }
   Transport transport = mailSession.getTransport();
   MimeMessage message = new MimeMessage( mailSession );
   message.setSubject( email.getObjet() );
   message.setSender( new InternetAddress( email.getSender() ) );
   message.setFrom( new InternetAddress( email.getFrom()) );
   BodyPart bp = new MimeBodyPart();
   bp.setContent( body, "text/html" );
   MimeMultipart mmp = new MimeMultipart();
   mmp.addBodyPart( bp );
   Iterator<Entry<String, String>> it = inlineImage.entrySet().iterator();
   while ( it.hasNext() ) {
      Entry<String, String> pairs = it.next();
      PreencodedMimeBodyPart pmp = new PreencodedMimeBodyPart( "base64" );
      pmp.setHeader( "Content-ID", "<" + pairs.getKey() + ">" );
      pmp.setDisposition( MimeBodyPart.INLINE );
      pmp.setText( pairs.getValue() );
      mmp.addBodyPart( pmp );
   }
   message.setContent( mmp );
   message.addRecipient( Message.RecipientType.TO, new InternetAddress( email.getTo() ) );
   transport.connect();
   transport.sendMessage( message, message.getAllRecipients() );
   transport.close();
}

Thank you for helping me improve if I need to improve :)

+6

, PreencodedMimeBodyPart .

base64?

multipart/related, , cid: URL.

+1

All Articles