How to add image in mail body in java

To add an image in the body of an email in Java, you can use the MimeMessage and MimeMultipart classes from the JavaMail API. Here's an example:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class SendEmailWithImage {
    public static void main(String[] args) {
        final String username = "[email protected]";
        final String password = "your-password";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("[email protected]"));
            message.setSubject("Testing Subject");

            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText("This is message body");

            MimeMultipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);

            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(new File("path-to-your-image.jpg"));
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName("image.jpg");
            multipart.addBodyPart(messageBodyPart);

            message.setContent(multipart);

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

In this example, we're sending an email with a text body and an image attachment. The image is added to the email body using the MimeMultipart class.

If you want to embed the image in the email body instead of attaching it, you can use the MimeImage class:

MimeImage image = new MimeImage(new File("path-to-your-image.jpg"));
messageBodyPart = new MimeBodyPart();
image.writeData(messageBodyPart.getDataHandler());
multipart.addBodyPart(messageBodyPart);

Make sure to replace "path-to-your-image.jpg" with the actual path to your image file. Also, replace "[email protected]" and "your-password" with your actual Gmail email and password.

Note that you need to enable less secure apps in your Google account settings to use Gmail SMTP server. Also, be aware that storing passwords in your code is not recommended for production environments. Consider using environment variables or a secure storage mechanism instead.