2 Replies Latest reply: Dec 24, 2011 11:28 AM by Bill Shannon-Oracle RSS

    Send mail using JavaMail: One of the attachment is missing in the sent mail

    907310
      Hi,

      Here is my issue:

      I am sending mail with body part of the mail as html and along with two attachments.
      But the issue is, mail is getting only one file (i.e., 2nd attachment), it does not getting the 1st file ? and the body part is fine.

      This is my code to mail:

      import javax.mail.util.*
      -
      -
      -
      -
      Properties properties = System.getProperties();
      String host = "mail.gmail.com";
      properties.setProperty("mail.smtp.host", host);
      Session session = Session.getDefaultInstance(properties);

      BodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setContent("<html> here i am using some images and links to be displayed in the body part of the mail </html>", "*text/html*"); // issue
      messageBodyPart.setContent("some text", "*text/plain*"); // fine, no issues

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

      DataSource dataSourceCsv1 = new ByteArrayDataSource("a,b,c,d", "text/comma-separated-values");
      MimeBodyPart attachment = new MimeBodyPart();
      attachment.setDataHandler(new DataHandler(dataSourceCsv1));
      attachment.setFileName("Test1.csv");
      attachment.setDisposition(Part.ATTACHMENT);
      multipart.addBodyPart(attachment);

      DataSource dataSourceCsv2 = new ByteArrayDataSource("a,b,c,d", "text/comma-separated-values");
      attachment = new MimeBodyPart();
      attachment.setDataHandler(new DataHandler(dataSourceCsv2));
      attachment.setFileName("Test2.csv");
      attachment.setDisposition(Part.ATTACHMENT);
      multipart.addBodyPart(attachment);

      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("abc@gmail.com"));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(''abc123@gmail.com"));
      message.setSubject("Subject,,,...");
      message.setContent(multipart);

      Transport.send(message);

      After trying lot of times to fix this issue i observed that:
      if the body part is "text/plain", the attachments are in the sent mail are fine.
      if the body part is "text/html", the attachment is missing in the sent mail.

      Please can you tell me why i am getting this issue. Thanks in advance


      --
      Santosh Kumar K
        • 1. Re: Send mail using JavaMail: One of the attachment is missing in the sent mail
          tschodt
          This is a mime multipart layout issue.

          Check the mime multipart layout of a message that works
          (a multipart html message with attachments that you received, does not have to have anything to do with java mail)

          You will observe a mime multipart layout something along the lines of

          | mime multipart | | | |
          | | mime alternative | | |
          | | | body text/plain | |
          | | | mime related | |
          | | | | body text/html |
          | | | | image |
          | | | | image |
          | | file attachment | | |
          | | file attachment | | |

          compare the mime multipart layout you are sending from java mail
          to the mime multipart layout in a known good html message with attachments.
          • 2. Re: Send mail using JavaMail: One of the attachment is missing in the sent mail
            Bill Shannon-Oracle
            The simple version in the existing code should work as well. The more complex version described above allows you to send both html and plain text (so the recipient can choose which they want to see) and allows the html text to reference images that are included in the message.