Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- how to send body message in RTF format using JavaMail API?
- 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;
- }
- messageBodyPart.setDisposition(Part.INLINE);
Add Comment
Please, Sign In to add comment