So I will create a utility class with some utility methods to send emails and then I will use this utility method with different SMTP servers. The trick is to attach the image file like any other attachment and then set the Content-ID header for image file and then use the same content id in the email message body with /** * Utility method to send image in email body * @param session * @param to Email * @param subject * @param body */ public static void send Image Email(Session session, String to Email, String subject, String body) That’s all for Java Mail example to send mail in java using SMTP server with different authentication protocols, attachment and images.

Our Email Util class that has a single method to send email looks like below, it requires I hope it will solve all your needs for sending emails in java programs.

Following is a series of introductory tutorials on Prime Faces, a popular third-party component library for JSF 2.

This is intended as a beginner's tutorial, aimed at developers who already know JSF 2, but who are new to Prime Faces.

Java Mail API supports both TLS and SSL authentication for sending emails. Actually I kept them to show later and keep it simple at start of the tutorial.

Today we will learn how to use Java Mail API to send emails using SMTP server with no authentication, TLS and SSL authentication and how to send attachments and attach and use images in the email body. To send a file as attachment, we need to create an object of /** * Utility method to send email with attachment * @param session * @param to Email * @param subject * @param body */ public static void send Attachment Email(Session session, String to Email, String subject, String body) The program might look complex at first look but it’s simple, just create a body part for text message and another body part for attachment and then add them to the multipart.

Download the latest version of the Java Mail reference implementation and include it in your project build path. If you are using Maven based project, just add below dependency in your project. But sometimes we want to attach the image in the email and then use it in the email body itself.

The logic to create session differs based on the type of SMTP server, for example if SMTP server doesn’t require any authentication we can create the Session object with some simple properties whereas if it requires TLS or SSL authentication, then logic to create will differ. You must have seen so many emails that have image attachments and are also used in the email message.

For TLS and SSL authentication, I am using GMail SMTP server because it supports both of them. You can extend this method to attach multiple files too.

Java Mail API is not part of standard JDK, so you will have to download it from it’s official website i.e Java Mail Home Page. Since we can create HTML body message, if the image file is located at some server location we can use img element to show them in the message.

Prime Faces is at least as powerful as the competing component libraries, and is significant simpler to get started with than most of them.