接收和发送电子邮件与Scala
发布时间:2020-12-16 09:20:36 所属栏目:安全 来源:网络整理
导读:我正在计划使用 Scala和Akka来构建一个将要依赖电子邮件的服务.事实上,与我的服务的大部分沟通将通过发送信件并获得答复来完成.我想这意味着我需要一个可靠的电子邮件服务器和与Scala通信的方法. 问题是,这样做最好的做法是什么?我应该选择哪个电子邮件服务
我正在计划使用
Scala和Akka来构建一个将要依赖电子邮件的服务.事实上,与我的服务的大部分沟通将通过发送信件并获得答复来完成.我想这意味着我需要一个可靠的电子邮件服务器和与Scala通信的方法.
问题是,这样做最好的做法是什么?我应该选择哪个电子邮件服务器,什么样的Scala解决方案可以完成这项工作? 解决方法
通常使用
JavaMail API.在您的项目中,您可以将其包装在您自己的Scala库中或使用现有的Scala库.这是一个
example在Lift框架中使用现有的Mailer API:
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.") } } } } 许多Web框架将为您处理MIME类型,附件等实施方便的方法. 不用说发送电子邮件绝对不可靠.它更像是火,忘了操作.默认情况下邮件协议中没有确认或错误传播,这通常是正常接受的. 如果您使用SMTP邮件发件人,您可以将其连接到任何邮件服务器,无论是外部的,如gmail,还是本地安装的后缀. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |