Skip to content

Instantly share code, notes, and snippets.

@limansky

limansky/build.sbt

Created Jan 23, 2013
Embed
What would you like to do?
name := "Testmail"
version := "0.0.1"
scalaVersion := "2.9.1"
// you can also add multiple repositories at the same time
resolvers ++= Seq(
"Scala Tools Releases" at "https://oss.sonatype.org/content/groups/scala-tools/"
)
// if you have issues pulling dependencies from the scala-tools repositories (checksums don't match), you can disable checksums
// checksums := Nil
// Dependencies:
// Lift:
libraryDependencies ++= {
val liftVersion = "2.5-M4" // Put the current/latest lift version here
Seq(
"net.liftweb" %% "lift-webkit" % liftVersion % "compile->default"
)
}
// Jetty and other envinroment
libraryDependencies ++= Seq(
"ch.qos.logback" % "logback-classic" % "1.0.9" % "compile->default" // Logging
)
import net.liftweb.util.Mailer
import net.liftweb.common._
import javax.mail._
import javax.mail.internet._
object Runme extends App with Mailer {
println("Hello")
customProperties = {
authenticator = Full(new Authenticator() {
override def getPasswordAuthentication =
new PasswordAuthentication("username", "password")
})
Map(
"mail.smtp.host" -> "smtp.gmail.com",
"mail.smtp.port" -> "587",
"mail.smtp.starttls.enable" -> "true",
"mail.smtp.auth" -> "true",
"mail.smtp.connectiontimeout" -> "30000" // 30 seconds
)
}
val pdf = scala.io.Source.fromFile("report.pdf")(scala.io.Codec.ISO8859).map(_.toByte).toArray
val png = scala.io.Source.fromFile("image.png")(scala.io.Codec.ISO8859).map(_.toByte).toArray
val body = XHTMLPlusImages(<html><h1>Hello my friend</h1><img src="image.png" /><p>See the report!</p></html>,
PlusImageHolder("readme.pdf", "application/pdf", pdf, true),
PlusImageHolder("image.png", "image/png", png))
blockingSendMail(From("your.mail@gmail.com"), Subject("Test mail with attach broken"), To("recipient@domain.com"), body)
protected override def buildMailBody(tab: MailBodyType): BodyPart = {
val bp = new MimeBodyPart
tab match {
case PlainMailBodyType(txt) => bp.setText(txt, "UTF-8")
case PlainPlusBodyType(txt, charset) => bp.setText(txt, charset)
case XHTMLMailBodyType(html) => bp.setContent(encodeHtmlBodyPart(html), "text/html; charset=" + charSet)
case XHTMLPlusImages(html, img@_*) =>
val html_mp = new MimeMultipart("related")
val bp2 = new MimeBodyPart
val (attachs, images) = img.partition(_.attachment)
bp2.setContent(encodeHtmlBodyPart(html), "text/html; charset=" + charSet)
html_mp.addBodyPart(bp2)
images.foreach {
i => html_mp.addBodyPart(buildAttachment(i))
}
if (attachs.isEmpty) {
bp.setContent(html_mp)
} else {
val mixed_mp = new MimeMultipart("mixed")
val html_p = new MimeBodyPart
html_p.setContent(html_mp)
mixed_mp.addBodyPart(html_p)
attachs.foreach {
i => mixed_mp.addBodyPart(buildAttachment(i))
}
bp.setContent(mixed_mp)
}
}
bp
}
private def buildAttachment(holder: PlusImageHolder) = {
val part = new MimeBodyPart
part.setFileName(holder.name)
part.setContentID(holder.name)
part.setDisposition(if (holder.attachment) Part.ATTACHMENT else Part.INLINE)
part.setDataHandler(new javax.activation.DataHandler(new javax.activation.DataSource {
def getContentType = holder.mimeType
def getInputStream = new java.io.ByteArrayInputStream(holder.bytes)
def getName = holder.name
def getOutputStream = throw new java.io.IOException("Unable to write to item")
}))
part
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment