Advertisement
samiulsaki

SendEmail_PDF.groovy

Dec 10th, 2019
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.91 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.net.URL;
  4. import java.nio.channels.Channels;
  5. import java.nio.channels.ReadableByteChannel;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8.  
  9. import com.atlassian.confluence.pages.PageManager
  10. import com.atlassian.sal.api.component.ComponentLocator
  11. import com.atlassian.mail.Email;
  12. import com.atlassian.mail.server.SMTPMailServer;
  13. import com.atlassian.confluence.mail.ConfluenceMailServerManager
  14.  
  15. import javax.activation.DataHandler;
  16. import javax.activation.DataSource;
  17. import javax.activation.FileDataSource;
  18. import javax.mail.Multipart;
  19. import javax.mail.internet.MimeBodyPart;
  20. import javax.mail.internet.MimeMultipart;
  21.  
  22. URL website =
  23.  new URL("https://<confluence URL>/spaces/flyingpdf/pdfpageexport.action?pageId=<page ID number>");
  24.  ReadableByteChannel rbc = Channels.newChannel(website.openStream());
  25.  FileOutputStream fos = new FileOutputStream("export.pdf");
  26.  fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  27.  
  28.  Path p1 = Paths.get("export.pdf");
  29.  File attachment = p1.toFile();
  30.  def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
  31.  SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();
  32.  if (mailServer != null) {
  33.  Email email = new Email("<e-mail address goes here>");
  34.  email.setSubject("test");
  35.  email.setBody("test");
  36.  
  37.  Multipart multipart = new MimeMultipart();
  38.  //set attachment
  39.  MimeBodyPart attachmentBodyPart = new MimeBodyPart();
  40.  DataSource source = new FileDataSource(attachment);
  41.  attachmentBodyPart.setDataHandler(new DataHandler(source));
  42.  
  43.  attachmentBodyPart.setFileName(attachment.getName());
  44.  attachmentBodyPart.setContent("hello world", "text/plain;charset=utf-8");
  45.  
  46.  multipart.addBodyPart(attachmentBodyPart);
  47.  
  48.  email.setMultipart(multipart);
  49.  
  50.  mailServer.send(email);
  51.  } else {
  52.  throw new RuntimeException("no mail server!!!");
  53.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement