如何使用scala和liftweb将多个文件附加到电子邮件

时间:2013-11-26 14:37:20

标签: scala lift

我正在尝试附加我正在编写的多个文件REST为此服务但文件未加载
我的代码如下所示: -

HTML: -

<form action="DemoEmailAttachment" method="post">
    To: <input type="text" name="to"><br>
    Subject: <input type="text" name="subject"><br>
    Body:<textarea rows="4" cols="50" name="body"></textarea><br>
    AttachFiles:<input type="file" name="fileupload" multiple="multiple"><br>
    <input type="submit" value="Send">       
</form>

SCALA: -

object demoAttachment {

 def sendAtchMail(request: Req) = {

val obj = request._params
println(obj)
LiftRules.handleMimeFile = OnDiskFileParamHolder.apply
val fileupload = request.uploadedFiles
println(fileupload)

val to = (obj.get("to").get)(0).split(",")
val subject = obj.get("subject").get
val body = obj.get("body").get

case class CSVFile(bytes: Array[Byte],filename:String,mime:String )
var data = Array[Byte]()
println(data.length)

val ls =  fileupload.map(c => c match {
  case ff:OnDiskFileParamHolder => {  ff.fileStream.read(data)

    val attach =  CSVFile(data,ff.fileName,ff.mimeType)
    val msg =  XHTMLPlusImages(<p>Please research the enclosed.               </p>,PlusImageHolder(attach.filename, attach.mime, attach.bytes))
    Mailer.sendMail(
      From("536@gmail.com"),
      Subject(subject(0)),
      To(to(0)),

      msg
    )
  }
  case _ => "there are no files"
} )
"success"
}
}

1 个答案:

答案 0 :(得分:1)

XHTMLPlusImages构造函数对附件参数采用vararg。所以,只需传递多个附件,如下所示:

XHTMLPlusImages(NodeSeq, attach1, attach2, attach3, ...)

此外,您应该验证data实际上是否包含您要发送的内容。除非我弄错了,你从流中读取数据的方式只会读取Array[Byte]可以容纳的许多字节,这可能是0.有一个很好的答案here讨论从InputStreamByteArray