Question

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

Was it helpful?

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top