Как отправить тело сообщения в формате RTF, используя JavaMail API? - PullRequest
0 голосов
/ 18 октября 2011

Я работаю над приложением для отправки электронной почты. Я хочу отправить сообщение в формате RTF. Когда я пытаюсь отправить сообщение в формате RTF, он создает файл вложения, а не тело сообщения.

public boolean run() {
    System.out.println("START SENDING");



    Transport transport = null;
    Session mailSession = null;
    Properties props = null;
    try {

        props = new Properties();
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.host", smtpServer);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.user", user);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.auth", auth);
        props.put("mail.smtp.ssl.enable", false);
        props.put("mail.smtp.ssl.trust", "*");


        mailSession = Session.getInstance(props, new SMTPAuthenticator(user, password));

        transport = mailSession.getTransport("smtp");
        MimeMessage message = prepareMessage(mailSession, "UTF-8",
                user, subject, HtmlMessage, recipientsString);
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(HtmlMessage);
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        messageBodyPart = new MimeBodyPart();

        if (file != null) {
            DataSource source = new FileDataSource(file.getAbsoluteFile());
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(file.getName());
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
        } else {

            /*HtmlMessage (String)*/
            message.setContent(HtmlMessage, "text/rtf");
        }
        transport.connect();
         Transport.send(message);
        System.out.println("SEND DONE");

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Unable to send Message.");
        ex.printStackTrace();
    } finally {

        System.out.println(mailSession.getProperty("mail.smtp.user"));
        mailSession = null;

        props.clear();
        smtpServer = null;
        user = null;
        password = null;
        if (t != null && t.isAlive()) {
            t.interrupt();
            t = null;
        }
        this.dispose();
    }
    return false;
}

1 Ответ

0 голосов
/ 18 октября 2011

Чтобы часть тела сообщения была встроенной, а не вложением, сделайте следующее:

messageBodyPart.setDisposition(Part.INLINE);
...