使用 Scala 接收和发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22282398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Receive and send email with Scala
提问by src091
I'm planning to build a service using Scala and Akka that is going to depend on e-mail heavily. In fact, most of the communication with my service will be done by sending letters to it and getting a replies. I guess this means I need a reliable email server and ways to communicate with it from Scala.
我计划使用 Scala 和 Akka 构建一个严重依赖电子邮件的服务。事实上,与我的服务的大部分沟通都是通过向它发送信件并获得回复来完成的。我想这意味着我需要一个可靠的电子邮件服务器以及从 Scala 与之通信的方法。
Question is, what are the best practices for doing this? Which email server should I choose and what Scala solutions are there to accomplish this task?
问题是,这样做的最佳实践是什么?我应该选择哪个电子邮件服务器以及有哪些 Scala 解决方案来完成此任务?
采纳答案by y?s??la
Usually JavaMail APIis used. In your project you can wrap it in your own Scala library or use existing one. Here is an exampleof using existing MailerAPI in Lift framework:
通常使用JavaMail API。在您的项目中,您可以将其包装在您自己的 Scala 库中或使用现有的库。以下是在 Lift 框架中使用现有API的示例Mailer:
package code
package config
import javax.mail.{Authenticator, PasswordAuthentication}
import javax.mail.internet.MimeMessage
import net.liftweb._
import common._
import util._
/*
* A Mailer config object that uses Props and auto configures for gmail
* if detected.
*/
object SmtpMailer extends Loggable {
def init(): Unit = {
var isAuth = Props.get("mail.smtp.auth", "false").toBoolean
Mailer.customProperties = Props.get("mail.smtp.host", "localhost") match {
case "smtp.gmail.com" => // auto configure for gmail
isAuth = true
Map(
"mail.smtp.host" -> "smtp.gmail.com",
"mail.smtp.port" -> "587",
"mail.smtp.auth" -> "true",
"mail.smtp.starttls.enable" -> "true"
)
case h => Map(
"mail.smtp.host" -> h,
"mail.smtp.port" -> Props.get("mail.smtp.port", "25"),
"mail.smtp.auth" -> isAuth.toString
)
}
//Mailer.devModeSend.default.set((m : MimeMessage) => logger.info("Sending Mime Message: "+m))
if (isAuth) {
(Props.get("mail.smtp.user"), Props.get("mail.smtp.pass")) match {
case (Full(username), Full(password)) =>
logger.info("Smtp user: %s".format(username))
logger.info("Smtp password length: %s".format(password.length))
Mailer.authenticator = Full(new Authenticator() {
override def getPasswordAuthentication = new
PasswordAuthentication(username, password)
})
logger.info("SmtpMailer inited")
case _ => logger.error("Username/password not supplied for Mailer.")
}
}
}
}
Many web frameworks would implement conveniece methods for you to deal with mime types, attachments, etc.
许多 Web 框架会为您实现方便的方法来处理 mime 类型、附件等。
Needless to say that sending email is never 100% reliable. It's more like fire and forget operation. There is no confirmation or error propagation in mail protocols by default which is usually accepted as normal.
不用说,发送电子邮件从来都不是 100% 可靠的。这更像是火后忘记操作。默认情况下,邮件协议中没有确认或错误传播,这通常被视为正常。
If you use SMTP mail sender you can connect it to any mail server whether it's an external one like gmail, or locally installed postfix.
如果您使用 SMTP 邮件发件人,您可以将其连接到任何邮件服务器,无论是外部邮件服务器,如 gmail,还是本地安装的 postfix。
回答by Tvaroh
You can try courierwhich is a Scala layer on top of Java Mail that gives more fluent API. Unfortunately, at the moment there are no non-blocking solution for email sending on the JVM (correct me if I'm wrong).
您可以尝试使用courier,它是 Java Mail 之上的 Scala 层,可提供更流畅的 API。不幸的是,目前在 JVM 上发送电子邮件没有非阻塞解决方案(如果我错了,请纠正我)。

