2 Replies Latest reply on Dec 24, 2011 5:28 PM by Bill Shannon-Oracle

    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