0

Salam, I have an application that generate time tabling in a grid pane and i could turn the gridpane to image in order to print it but now i want to send that image in email but i can't find a way I have seen similiar topics but they send image existing in computer by its path but in my case i don't want to save the image i just want to send it directly with java class Image or ImageView Here is my code

 @FXML
    void send_mail(ActionEvent event) throws IOException {

        final String username = "******@gmail.com";
        final String 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");

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

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("*****@yahoo.com"));
            message.setSubject("Testing Subject");
            message.setText("PFA");

            MimeBodyPart messageBodyPart = new MimeBodyPart();

            Multipart multipart = new MimeMultipart();

            messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(image, "IMAGEVIEW");
            // String file = "../emploi/"+fileName;
            messageBodyPart.setText("Emploi de temps");
            //DataSource source = new FileDataSource(file);
            //  messageBodyPart.setDataHandler(new DataHandler((DataSource) file));

            // messageBodyPart.attachFile(file);
            messageBodyPart.setFileName("Emploi de temps");

            multipart.addBodyPart(messageBodyPart);
            MimeBodyPart photoBodyPart = new MimeBodyPart();        

            photoBodyPart = new MimeBodyPart();
            photoBodyPart.setContent(image, "IMAGEVIEW");
            multipart.addBodyPart(photoBodyPart);

            message.setContent(multipart);

            System.out.println("Sending");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

but i get this error

javax.mail.internet.ParseException: In Content-Type string , expected '/', got null at javax.mail.internet.ContentType.(ContentType.java:104) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1510) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1172) at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:522) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1533) at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2271) at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2231) at javax.mail.Transport.send(Transport.java:123)

Isra
  • 13
  • 3
  • doesn't look related to fx (parse exception points at something wrong in the descriptor/location ..) Unrelated: java naming conventions, please – kleopatra Jun 16 '20 at 12:20
  • 1
    Look at the documentation of [`#setContent(Object,String)`](https://javaee.github.io/javaee-spec/javadocs/javax/mail/internet/MimeBodyPart.html#setContent-java.lang.Object-java.lang.String-). You need to pass a mime type for the second argument. And note the requirements for the first argument. – Slaw Jun 16 '20 at 15:33
  • There seems to be a constructor taking `InputStream` which means you can use the approach presented here https://stackoverflow.com/a/27062201/2991525 , write to a `ByteArrayOutputStream`, take the resulting byte array and use it to create a `ByteArrayInputStream`. This will result in the image data being placed twice and the content of the resulting file being placed once in memory though; not sure if this solution is preferable to creating a temporary file, but it's an approach that can be taken... – fabian Jun 16 '20 at 15:58

0 Answers0