javaemailjakarta-mailprocessingfacebook-timeline

Updating status with picture via email. (JavaMail)


I'm writing a standalone application in Processing and I need to publish sketch screenshots on FB page timeline via JavaMail. So I wrote this:

void sendMail() {

  String host="smtp.gmail.com";
  Properties props=new Properties();

  props.put("mail.transport.protocol", "smtp");
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.port", "587");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.starttls.enable","true");

  Session session = Session.getDefaultInstance(props, new Auth());

  try
  {

    MimeMessage message = new MimeMessage(session);

    message.setFrom(new InternetAddress("xxxxx@gmail.com", "xxxxx"));

    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xxxxxxxxxx@m.facebook.com", false));


    message.setSubject("ok");

    BodyPart mbp = new MimeBodyPart();
    DataSource fds = new FileDataSource(file);
    mbp.setDataHandler(new DataHandler(fds));
    mbp.setFileName("screen.png");
    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp);
    message.setContent(mp);
    message.setSentDate(new Date());

    Transport.send(message);
    println("Mail sent!");
  }
  catch(Exception e)
  {
    println(e);
  }
}

Now, when I write down my gmail e-mail as recipient - method works perfectly (I receive only subject and attached photo), but when I use my FB page e-mail - only subject appears in my timeline, no photo.

I've done the same thing with PHP before and it worked. Maybe I have missed something?

Thank You in advance!:)


Solution

  • Well, I've looked on the content of the original message and noticed this:

    Content-Type: application/octet-stream; name=screen.png
    

    So I just added a third line to my code:

    MimeBodyPart mbp = new MimeBodyPart();
    mbp.attachFile(new File(file));
    mbp.setHeader("Content-Type", "image/png");
    

    Then I got:

    Content-Type: image/png
    

    and now everything works perfectly!:)