0

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!:)

Lain
  • 2,166
  • 4
  • 23
  • 47
irishlad
  • 21
  • 3

2 Answers2

1

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!:)

irishlad
  • 21
  • 3
0

You're creating a multipart message with exactly one part and that one part isn't a text part, it's an image part. While that's perfectly legal according to the MIME spec, it's "unusual", and perhaps Facebook email isn't prepared to handle such a message.

When you did the same thing with PHP, did you create a message with the same structure?

Try NOT creating a multipart message. Instead, just set the image as the content of the message itself.

Also, try creating a multipart message with a first part that's plain text and a second part that's the image.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Well, the library that I used in PHP was a little bit different, so I simply used something like attachFile(). "Also, try creating a multipart message with a first part that's plain text and a second part that's the image." - not working, same effect as I have already:) – irishlad Jul 11 '12 at 10:54
  • "Try NOT creating a multipart message. Instead, just set the image as the content of the message itself." - well, I've tried something like this: MimeBodyPart mbp = new MimeBodyPart(); mbp.attachFile(file); but I get The function attachFile(String) does not exists. But in the reference page I see this: http://javamail.kenai.com/nonav/javadocs/javax/mail/internet/MimeBodyPart.html – irishlad Jul 11 '12 at 10:54
  • You created **two** MimeBodyParts and added both of them to the MimeMultipart, right? The attachFile method is on newer versions of JavaMail. If you're not finding it you're using a very old version. Try changing your PHP code to send the message to your normal INBOX, then use your mailer's "view source" feature to examine the MIME structure of the message. You should be able to duplicate that structure with JavaMail, whatever it is. If you do that and Facebook still doesn't like it, I don't know what could be going wrong. – Bill Shannon Jul 11 '12 at 17:23