简体   繁体   中英

Sending email with attachment using scala and Liftweb

This is the first time i am integrating Email service with liftweb

I want to send Email with attachments(Like:- Documents,Images,Pdfs) my code looking like below

       case class CSVFile(bytes: Array[Byte],filename: String = "file.csv",
                     mime: String = "text/csv; charset=utf8; header=present" )

       val attach = CSVFile(fileupload.mkString.getBytes("utf8"))

       val body = <p>Please research the enclosed.</p>

       val msg = XHTMLPlusImages(body,
                  PlusImageHolder(attach.filename, attach.mime, attach.bytes)) 

      Mailer.sendMail(
      From("vyz@gmail.com"),
      Subject(subject(0)),
      To(to(0)),
     )

this code is taken from LiftCookbook its not working like my requirement its working but only the Attached file name is coming(file.csv) no data in it(i uploaded this file (gsy.docx))

Best Regards GSY

You don't specify what type fileupload is, but assuming it is of type net.liftweb.http. FileParamHolder net.liftweb.http. FileParamHolder then the issue is that you can't just call mkString and expect it to have any data since there is no data in the object, just a fileStream method for retrieving it (either from disk or memory).

The easiest to accomplish what you want would be to use a ByteArrayInputStream and copy the data to it. I haven't tested it, but the code below should solve your issue. For brevity, it uses Apache IO Commons to copy the streams, but you could just as easily do it natively.

val data = {
  val os = new ByteArrayOutputStream()
  IOUtils.copy(fileupload.fileStream, os)
  os.toByteArray
}

val attach = CSVFile(data) 

BTW, you say you are uploading a Word (DOCX) file and expecting it to automatically be CSV when the extension is changed? You will just get a DOCX file with a csv extension unless you actually do some conversion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM