Guest User

Untitled

a guest
Aug 19th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. how to send body message in RTF format using JavaMail API?
  2. public boolean run() {
  3. System.out.println("START SENDING");
  4.  
  5.  
  6.  
  7. Transport transport = null;
  8. Session mailSession = null;
  9. Properties props = null;
  10. try {
  11.  
  12. props = new Properties();
  13. props.put("mail.smtp.starttls.enable", true);
  14. props.put("mail.smtp.host", smtpServer);
  15. props.put("mail.smtp.port", port);
  16. props.put("mail.smtp.user", user);
  17. props.put("mail.smtp.password", password);
  18. props.put("mail.smtp.auth", auth);
  19. props.put("mail.smtp.ssl.enable", false);
  20. props.put("mail.smtp.ssl.trust", "*");
  21.  
  22.  
  23. mailSession = Session.getInstance(props, new SMTPAuthenticator(user, password));
  24.  
  25. transport = mailSession.getTransport("smtp");
  26. MimeMessage message = prepareMessage(mailSession, "UTF-8",
  27. user, subject, HtmlMessage, recipientsString);
  28. BodyPart messageBodyPart = new MimeBodyPart();
  29. messageBodyPart.setText(HtmlMessage);
  30. Multipart multipart = new MimeMultipart();
  31. multipart.addBodyPart(messageBodyPart);
  32. messageBodyPart = new MimeBodyPart();
  33.  
  34. if (file != null) {
  35. DataSource source = new FileDataSource(file.getAbsoluteFile());
  36. messageBodyPart.setDataHandler(new DataHandler(source));
  37. messageBodyPart.setFileName(file.getName());
  38. multipart.addBodyPart(messageBodyPart);
  39. message.setContent(multipart);
  40. } else {
  41.  
  42. /*HtmlMessage (String)*/
  43. message.setContent(HtmlMessage, "text/rtf");
  44. }
  45. transport.connect();
  46. Transport.send(message);
  47. System.out.println("SEND DONE");
  48.  
  49. } catch (Exception ex) {
  50. JOptionPane.showMessageDialog(this, "Unable to send Message.");
  51. ex.printStackTrace();
  52. } finally {
  53.  
  54. System.out.println(mailSession.getProperty("mail.smtp.user"));
  55. mailSession = null;
  56.  
  57. props.clear();
  58. smtpServer = null;
  59. user = null;
  60. password = null;
  61. if (t != null && t.isAlive()) {
  62. t.interrupt();
  63. t = null;
  64. }
  65. this.dispose();
  66. }
  67. return false;
  68. }
  69.  
  70. messageBodyPart.setDisposition(Part.INLINE);
Add Comment
Please, Sign In to add comment