JavaMail API - SPLessons

JavaMail Send Inline Image

Home > Lesson > Chapter 11
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JavaMail Send Inline Image

JavaMail Send Inline Image

shape Description

JavaMail Send Inline Image, As everyone know that it is very easy to insert an image in HTML code by using image tags like that through the Java mail API is also an interesting task. Here developer is not using any particular server only through the SMTP server image will be sent to the recipients. Here message body and ContenetType need to be set in the code why because the developer is using HTML code to send an image. Following is an example which describes more regarding how send an image to others.

shape Example

Following is an example which describes more about an JavaMail Send Inline Image. inlineimages.java [java]package firstmail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class inlineimages { public static void main(String[] args) { // Recipient's email ID needs to be mentioned. String to = "enni@splesson.com"; // Sender's email ID needs to be mentioned String from = "hr@splessons.com"; final String username = "saikumar@splessons.com";//change accordingly final String password = "splesson123";//change accordingly // Assuming you are sending email through relay.jangosmtp.net String host = "smtp.gmail.com"; Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("SPlessons image"); // This mail has 2 part, the BODY and the embedded image MimeMultipart multipart = new MimeMultipart("related"); // first part (the html) BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>Hey man find the below image</H1><img src=\"cid:image\">"; messageBodyPart.setContent(htmlText, "text/html"); // add it multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource( "E:/splessons.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID", "<image>"); // add image to the multipart multipart.addBodyPart(messageBodyPart); // put everything together message.setContent(multipart); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } } }[/java] Output When compile the program output will be as follows in the console. [java]Sent message successfully....[/java] Now check the mail.

shape Programming Tips

  • JavaMail Send Inline Image - MimeBodyPart(); needs to be assigned.
  • JavaMail Send Inline Image - Here program executed with the SMTP server not with any particular server.