0

I'm trying to figure it out of how to open an email using javax.mail. My goal is to provide a feature where a user clicks on a button and a default email will open with an attachment. So far, I'm using javax.mail and what it does is just sending the email right when the button is click. Is there a way to just open the email without direct sending? If so, how? I'm using Java 8.

I can't use the 'mailto:' because I need to attach a png file when a user opens an email. Also I'm not sure if I should use ProcessBuilder to open outlook because every user's machine will have a different userName within the C Drive or I'm not sure how to use that.

Here's my code just in case if you need it

 String result;
       String to = "....gov";
       String from = "....gov";
       String host = "....gov";

       Properties properties = System.getProperties();
       properties.setProperty("mail.smtp.host", host);
       Session mailSession = Session.getDefaultInstance(properties);
           try{
          MimeMessage message = new MimeMessage(mailSession);
          message.setFrom(new InternetAddress(emailFrom));
         message.addRecipient(Message.RecipientType.TO,new InternetAddress(emailTo));
          message.setSubject("meh!");

          BodyPart messageBodyPart = new MimeBodyPart();
          messageBodyPart.setText("text body mehmehmehmeh");

       // Create a multipar message
             Multipart multipart = new MimeMultipart();

             // Set text message part
             multipart.addBodyPart(messageBodyPart);

             // Part two is attachment
             messageBodyPart = new MimeBodyPart();
             String filename = "testing.png";
             DataSource source = new FileDataSource(filename);
             String imageString = toDataURL.substring("data:image/png;base64," .length());
             byte[] contentdata = imageString.getBytes();
             ByteArrayDataSource ds = new ByteArrayDataSource(contentdata, "image/png");

             messageBodyPart.setDataHandler(new DataHandler(ds));
             messageBodyPart.setFileName(filename);
             multipart.addBodyPart(messageBodyPart);  //

             // Send the complete message parts
             message.setContent(multipart);

          Transport.send(message);
          result = "Sent message successfully....";
       }catch (MessagingException mex) {
          mex.printStackTrace();
          result = "Error: unable to send message....";
       }
timJIN520
  • 299
  • 1
  • 3
  • 15

1 Answers1

1

Is there a way to just open the email without direct sending? If so, how?

Don't call Transport.send. Then follow the steps in this answer. and start with msg.saveChanges(). There is an X-Unsent header in that answer that can be used to toggle some outlook features.

Also I'm not sure if I should use ProcessBuilder to open outlook because every user's machine will have a different userName within the C Drive or I'm not sure how to use that.

You use File.createTempFile as this will account for user names. If you need to save in a different location you can read from System.getProperty​ or if you are only targeting Windows machines you can read from System.getenv. To list all the environment vars you can type set in the command window.

jmehrens
  • 10,580
  • 1
  • 38
  • 47