发送电子邮件到Facebook

时间:2014-07-06 15:10:07

标签: java email javamail

我正在尝试向我的Facebook帐户发送包含附件(图像)的电子邮件(用于在我的页面墙上发布此图像)。

电子邮件发送完美,但我的墙上没有显示任何内容。

如果我直接通过Gmail发送,则效果非常好。我做错了什么?

这是我的代码:

private static final String HOST = "smtp.gmail.com";
private static final String USERNAME = "myusername"; // Without @gmail.com
private static final String PASSWORD = "mypassword";

private static final Properties PARAMETERS = new Properties() {
    private static final long serialVersionUID = 1L; {
        put("mail.smtp.starttls.enable", "true");
        put("mail.smtp.host", HOST);
        put("mail.smtp.user", USERNAME);
        put("mail.smtp.password", PASSWORD);
        put("mail.smtp.port", "587");
        put("mail.smtp.auth", "true");
    }
};

// Some code...

final File newest = new File(COMICS_DIR, COMICS_DIR.list().length - 1 + ".png"); // The file I want to upload.
final Session session = Session.getInstance(PARAMETERS);
final MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(USERNAME));
message.addRecipient(RecipientType.TO, new InternetAddress(FACEBOOK_MAIL)); // My facebook mail.
message.setSubject(title);
final Multipart multipart = new MimeMultipart();
final BodyPart body = new MimeBodyPart();
body.setDataHandler(new DataHandler(new FileDataSource(newest)));
body.setFileName(newest.getName());
multipart.addBodyPart(body);
message.setContent(multipart);
final Transport transport = session.getTransport("smtp");
transport.connect(HOST, USERNAME, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());

感谢您的时间,并抱歉我的英语不好。

编辑:我对Tumblr有同样的错误,但它适用于Twitpics。

1 个答案:

答案 0 :(得分:1)

试试这个:

替换

final Multipart multipart = new MimeMultipart();
final BodyPart body = new MimeBodyPart();
body.setDataHandler(new DataHandler(new FileDataSource(newest)));
body.setFileName(newest.getName());
multipart.addBodyPart(body);
message.setContent(multipart);

final Multipart multipart = new MimeMultipart();
multipart.setDataHandler(new DataHandler(new FileDataSource(newest)));
multipart.setDisposition(Part.ATTACHMENT);
multipart.setFileName(newest.getName());
message.setContent(multipart);
message.saveChanges();