Mix plain text and HTML content in a mailTag(s): Networking


   Properties props = new Properties();
   props.put("mail.smtp.host", "MYMAILSERVER");
   Session session = Session.getInstance(props,null);
   MimeMessage message = new MimeMessage(session);

   InternetAddress from = new InternetAddress("from@me.com");
   InternetAddress to = new InternetAddress("to@you.com");

   message.setSubject("I am a multipart text/html email" );
   message.setFrom(from);
   message.addRecipient(Message.RecipientType.TO, to);

   Multipart multipart = new MimeMultipart();

   // PLAIN TEXT
   BodyPart messageBodyPart = new MimeBodyPart();
   messageBodyPart.setText("Here is your plain text message");
   multipart.addBodyPart(messageBodyPart);

   // HTML TEXT
   messageBodyPart = new MimeBodyPart();
   String htmlText = "<H1>I am the html part</H1>";
   messageBodyPart.setContent(htmlText, "text/html");
   multipart.addBodyPart(messageBodyPart);

   message.setContent(multipart);
   Transport.send(message);

blog comments powered by Disqus