×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Scala
Posted by: marcus bui
Added: Dec 23, 2014 7:32 AM
Views: 16
  1. def sendMailWithAttachment() = {
  2.  
  3.     val message: MimeMessage = buildMimeMessage("ashton@localhost",
  4.       "patricia@localhost",
  5.       "large attachment mail",
  6.       "content",
  7.       greenmail.getSmtp().getServerSetup());
  8.  
  9.     val messageBodyPart: MimeBodyPart = new MimeBodyPart();
  10.     messageBodyPart.setText("Hi");
  11.     //messageBodyPart.setContent(message, "text/html");
  12.  
  13.     val tmpFile: File  = File.createTempFile("foo", ".tmp");
  14.     tmpFile.deleteOnExit();
  15.  
  16.     val f: RandomAccessFile  = new RandomAccessFile(tmpFile, "rw");
  17.     f.setLength(1024 * 1024 * 20);
  18.  
  19.     val source: DataSource  = new FileDataSource(tmpFile);
  20.     val attachPart: MimeBodyPart = new MimeBodyPart();
  21.     attachPart.setDataHandler(new DataHandler(source));
  22.     attachPart.setFileName(tmpFile.getName);
  23.  
  24.     val multiPart: Multipart = new MimeMultipart();
  25.     multiPart.addBodyPart(messageBodyPart);
  26.     multiPart.addBodyPart(attachPart);
  27.  
  28.     message.setContent(multiPart);
  29.  
  30.     Transport.send(message, Array(new InternetAddress("ashton@localhost")));
  31.   }