Problem in receiving HTML attachement with images in Java mail program.


I have written a mailer program in java, which send html as attachment. Iam getting the attachment file as it is send if the html file doesnt have any images. If it contains any images, then received attchment file doesnt show the images. Following the code fragment.

public boolean SendMail(String recipientsAddr,String ccAddr, String bccAddr,String subject, String messageBody,
                   String fromAddr,String hostAddr, String strUserName, String strPwd)     {
        boolean debug = false;
        try   {
                Properties props = new Properties();
                props.put("", hostAddr);
                              Session session = Session.getDefaultInstance(props, null);

                Message message = new MimeMessage(session);
                InternetAddress from_internet_address = new InternetAddress(fromAddr);
                String To = recipientsAddr;

                //Added here CC and Bcc

                 message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccAddr));

                 message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bccAddr));

                message.setSentDate(new Date());
         // create the message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();

            //fill message
            messageBodyPart.setText("Hi hello this is the msg in mail");
            Multipart multipart = new MimeMultipart();

            // Part two is attachment
            messageBodyPart = new MimeBodyPart();

            BodyPart alt_bp1 = getFileBodyPart("C:\\test.htm");

              return true;
          catch(MessagingException me) {

          catch(Exception ex) {


     public BodyPart getFileBodyPart(String filename)
     throws javax.mail.MessagingException {
        BodyPart bp = new MimeBodyPart();
        bp.setDataHandler(new DataHandler
           (new FileDataSource(filename)));
        return bp;

Is there any solution for attaching the html file which has some image, say company logo, and receiving it as it is?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You must use a different MIME.
To see what is the correct to use, try to send to your e-mail address (the one you see using javax.mail API) a message with the structure you want.
From that you can see what are the correct setting for the e-mail.

Hope this bring you in the right direction.
This is a code you can use to see the parties of the message
in String ret you can find the result of reading, attachment =true if an attachament is found, content is the message content when it is not a String:
                  javax.mail.internet.MimeMultipart mmp = (javax.mail.internet.MimeMultipart) content;
                  try {
                        int count = mmp.getCount();
                        for (int i = 0; i < count; i++) {
                              javax.mail.Part part = mmp.getBodyPart(i);
                              String disposition = part.getDisposition();
                              if ((disposition != null) && ((disposition.equals(javax.mail.Part.ATTACHMENT)) || (disposition.equals(javax.mail.Part.INLINE)))) {
                                    //System.out.println("Attachment presente "+part.getFileName() + " " + part.getInputStream());
                                    attachment = true;
                              } //disposition !null
                              if (disposition == null) {
                                    // Check if plain
                                    javax.mail.internet.MimeBodyPart mbp = (javax.mail.internet.MimeBodyPart) part;
                                    if (mbp.isMimeType("text/plain")) {
                                          //System.out.println("Handle plain");
                                          ret = (String) mbp.getContent();
                                    } else {
                                          System.out.println("Special non-attachment cases here of image/gif, text/html, ...");//your case I believe
                                          //ret = (String) mbp.getContent();
                                          attachment = true;
                              } //disposition null
                        } //for i
                  } catch (Exception ex) {
                        ret="ERROR reading message " + ex;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.