本文整理汇总了Scala中javax.mail.internet.MimeMessage类的典型用法代码示例。如果您正苦于以下问题:Scala MimeMessage类的具体用法?Scala MimeMessage怎么用?Scala MimeMessage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MimeMessage类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Mailer
//设置package包名称以及导入依赖的类
package org.kirhgoff.lastobot
import java.util.Properties
import javax.mail.{Message, Session}
import javax.mail.internet.{InternetAddress, MimeMessage}
import scala.io.Source
object Mailer {
val host = "smtp.gmail.com"
val port = "587"
val address = "[email protected]"
val username = "lastobot"
val password = Source.fromFile(System.getProperty("user.home")
+ "/.lastobot/.mail").getLines.mkString
def sendMail(text:String, subject:String) = {
val properties = new Properties()
properties.put("mail.smtp.port", port)
properties.put("mail.smtp.auth", "true")
properties.put("mail.smtp.starttls.enable", "true")
val session = Session.getDefaultInstance(properties, null)
val message = new MimeMessage(session)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
message.setSubject(subject)
message.setContent(text, "text/html")
val transport = session.getTransport("smtp")
transport.connect(host, username, password)
transport.sendMessage(message, message.getAllRecipients)
}
def main(args:Array[String]) = {
sendMail("aaaa", "bbb")
}
}
示例2: EMail
//设置package包名称以及导入依赖的类
package tools
import javax.mail.internet.{InternetAddress, MimeMessage}
import javax.mail._
import javax.mail._
import java.util.Date
class EMail(email: String, password: String, smtp: String) {
val session = getSession
def sendMail(address:String,title:String,body:String)={
try{
val msg = new MimeMessage(session)
msg.setFrom(new InternetAddress(email))
msg.setRecipients(Message.RecipientType.TO, address)
msg.setSubject(title)
msg.setSentDate(new Date())
if (body.contains("<") && body.contains(">")) {
msg.setContent(body, "text/html;charset=utf-8")
} else {
msg.setText(body)
}
Transport.send(msg)
true
}catch {
case th:Throwable=>th.printStackTrace()
false
}
}
private def getSession() = {
val SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"
val props = System.getProperties()
props.setProperty("mail.smtp.host", smtp)
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY)
props.setProperty("mail.smtp.socketFactory.fallback", "false")
props.setProperty("mail.smtp.port", "465")
props.setProperty("mail.smtp.socketFactory.port", "465")
props.put("mail.smtp.auth", "true")
val ah = new Authenticator() {
override protected def getPasswordAuthentication(): PasswordAuthentication = {
new PasswordAuthentication(email, password)
}
}
Session.getDefaultInstance(props, ah)
}
}
示例3: EmailParser
//设置package包名称以及导入依赖的类
package uk.pkerrigan.dmarcparser
import java.io.ByteArrayInputStream
import java.nio.charset.CodingErrorAction
import java.util.Properties
import java.util.zip.{GZIPInputStream, ZipInputStream}
import javax.activation.DataSource
import javax.mail.Session
import javax.mail.internet.MimeMessage
import scala.collection.JavaConverters._
import org.apache.commons.mail.util.MimeMessageParser
import uk.pkerrigan.dmarcparser.report.Feedback
import scala.io._
class EmailParser(parser: ParserTrait = new Parser()) extends EmailParserTrait{
implicit val codec = Codec("UTF-8")
codec.onMalformedInput(CodingErrorAction.REPLACE)
codec.onUnmappableCharacter(CodingErrorAction.REPLACE)
def parseEmail(email: String): Option[Feedback] = {
val s = Session.getDefaultInstance(new Properties())
val is = new ByteArrayInputStream(email.getBytes)
val message = new MimeMessage(s, is)
val messageParser = new MimeMessageParser(message).parse()
messageParser.getAttachmentList.asScala.headOption.flatMap(extract)
}
private def extract(a: DataSource): Option[Feedback] = a match {
case `a` if a.getContentType.equals("application/gzip") => extractGzip(a)
case `a` if a.getContentType.equals("application/x-gzip") => extractGzip(a)
case `a` if a.getContentType.equals("application/zip") => extractZip(a)
case `a` if a.getContentType.equals("application/x-zip-compressed") => extractZip(a)
case _ => None
}
private def extractZip(a: DataSource): Option[Feedback] = {
val zip = new ZipInputStream(a.getInputStream)
zip.getNextEntry
val rawXml = Source.fromInputStream(zip).mkString
if (rawXml == "") None else Some(parser.parse(rawXml))
}
private def extractGzip(a: DataSource): Option[Feedback] = {
val zip = new GZIPInputStream(a.getInputStream)
val rawXml = Source.fromInputStream(zip).mkString
if (rawXml == "") None else Some(parser.parse(rawXml))
}
}
示例4: EmailTarget
//设置package包名称以及导入依赖的类
package lert.core.rule.target
import java.util.Properties
import javax.inject.Inject
import javax.mail._
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import lert.core.config.ConfigProvider
import lert.core.utils.JavaUtils
import com.typesafe.scalalogging.LazyLogging
class EmailTarget @Inject()(configProvider: ConfigProvider) extends LazyLogging {
def send(recipient: String, subject: String, body: String): Unit = {
val config = configProvider.config
require(config.targetSettings != null, "Email server settings are not specified")
val properties = JavaUtils.toProperties(Map(
"mail.smtp.host" -> config.targetSettings.mailServer.host,
"mail.smtp.port" -> config.targetSettings.mailServer.port.toString,
"mail.smtp.user" -> config.targetSettings.mailServer.username,
"mail.smtp.password" -> config.targetSettings.mailServer.password,
"mail.smtp.auth" -> Option(config.targetSettings.mailServer.auth).map(_.toString).orNull,
"mail.smtp.starttls.enable" -> "true",
"mail.smtp.socketFactory.class" -> "javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.port" -> config.targetSettings.mailServer.port.toString
))
val session = Option(config.targetSettings.mailServer.password)
.map(p => Session.getDefaultInstance(properties, new Authenticator {
override def getPasswordAuthentication(): PasswordAuthentication =
new PasswordAuthentication(config.targetSettings.mailServer.username, p)
})).getOrElse(Session.getDefaultInstance(properties))
val message = new MimeMessage(session)
message.setFrom(new InternetAddress(config.targetSettings.mailServer.username))
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient))
message.setSubject(subject)
message.setText(body)
Transport.send(message)
}
class SMTPAuthenticator extends Authenticator {
def getPasswordAuthentication(mail: String, password: String) = new PasswordAuthentication(mail, password)
}
}
示例5: send
//设置package包名称以及导入依赖的类
package com.github.cuzfrog.maila.server
import java.util.Date
import javax.mail.Message.RecipientType
import javax.mail.internet.{InternetAddress, MimeMessage}
import javax.mail.{Address, MessagingException, Session, Transport}
private[server] trait Sender {
def send(recipients: Seq[String], subject: String, text: String): (Boolean, String)
def close(): Unit
}
private[server] object Sender {
def apply(session: Session, transport: Transport, user: String): Sender = new JmSender(session, transport, user)
private class JmSender(session: Session, transport: Transport, user: String) extends Sender {
def send(recipients: Seq[String], subject: String, text: String): (Boolean, String) = {
val addresses: Array[Address] = recipients.map(new InternetAddress(_)).toArray
val message = new MimeMessage(session)
message.setFrom(new InternetAddress(user))
message.addRecipients(RecipientType.TO, addresses)
message.setSubject(subject)
message.setText(text)
message.setSentDate(new Date())
val msg = s"Sent message[${message.getSubject}] to[${message.getAllRecipients.mkString(",")}]"
try {
transport.sendMessage(message, message.getAllRecipients)
(true, s"$msg successfully.")
} catch {
case e: MessagingException =>
(false, s"$msg Failed with msg:${e.getMessage}")
}
}
def close(): Unit = transport.close()
}
}
示例6: SimpleMessage
//设置package包名称以及导入依赖的类
package com.twitter.diffy.util
import com.twitter.logging.Logger
import com.twitter.util.{FuturePool, Future}
import javax.mail._
import javax.mail.internet.{InternetAddress, MimeMessage}
import java.util.Properties
case class SimpleMessage(
from: String,
to: String,
bcc: String,
subject: String,
body: String)
class EmailSender(log: Logger, send: MimeMessage => Unit = Transport.send) {
private[this] val props = new Properties
props.put("mail.smtp.host", "smtp-gw1.wal-mart.com")
props.put("mail.smtp.auth", "false")
props.put("mail.smtp.port", "25")
private[this] val session = Session.getDefaultInstance(props, null)
def apply(msg: SimpleMessage): Future[Unit] =
FuturePool.unboundedPool {
val message = new MimeMessage(session)
message.setFrom(new InternetAddress(msg.from))
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(msg.to) map { _.asInstanceOf[Address]}
)
message.addRecipients(
Message.RecipientType.BCC,
InternetAddress.parse(msg.bcc) map { _.asInstanceOf[Address]}
)
message.setSubject(msg.subject)
message.setContent(msg.body, "text/html; charset=utf-8")
try {
send(message)
} catch { case e =>
log.error(e, "failed to send message")
}
}
}
示例7: Email
//设置package包名称以及导入依赖的类
package api
import java.util.Properties
import javax.mail.Message.RecipientType
import javax.mail.internet.{InternetAddress, MimeMessage}
import javax.mail._
object Email {
def apply(userName: String, password: String): Email = new Email(userName, password)
}
class Email(userName: String, password: String) {
private val session = getSession
def getSession: Session = {
val props: Properties = new Properties()
props.put("mail.smtp.host", "smtp.gmail.com")
props.put("mail.smtp.socketFactory.port", "465")
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
props.put("mail.smtp.auth", "true")
props.put("mail.smtp.port", "465")
val authenticator: Authenticator = new Authenticator {
override def getPasswordAuthentication: PasswordAuthentication = new PasswordAuthentication(userName, password)
}
Session.getDefaultInstance(props, authenticator)
}
def send(to: List[String], subject: String, body: String): Either[String, Unit] = {
val message: MimeMessage = new MimeMessage(session)
try {
message.setFrom(new InternetAddress("[email protected]", "Asana Weekly"))
message.setReplyTo(Array(new InternetAddress("[email protected]")))
message.setSubject(subject)
message.setContent(body, "text/html; charset=utf-8")
to.foreach(message.addRecipients(RecipientType.BCC, _))
Right(Transport.send(message))
} catch {
case e: Exception => Left(e.getMessage)
}
}
}
示例8: sendMail
//设置package包名称以及导入依赖的类
package wuxi99
import javax.mail.Message
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import java.util.Date
import java.util.Properties
import grizzled.slf4j.Logger
trait Email {
def sendMail(mailTitle:String,mailContent:String)
def sendMailFL(mailTitle:String,fileName:String,line:Int,AppCount:Int){
val mailContent = s"""<b>??:${fileName}</b><br>
<b>??:${line}</b><br>
<b>???:${AppCount}</b>"""
sendMail(mailTitle,mailContent)
}
def sendMailHtml(mailTitle:String,mailContent:String){
val mailContentHtml = s"<b>${mailContent}</b>"
sendMail(mailTitle,mailContentHtml)
}
}
object MailUtility extends Email {
private val smtpServer = "smtp.exmail.qq.com"
private val fromMail = "[email protected]"
private val toMail = "[email protected]"
private val user = fromMail
private val password = "ys123456"
@transient lazy val logger = Logger[this.type]
def sendMail(mailTitle:String,mailContent:String) {
try {
val props = new Properties()
props.put("mail.smtp.host",smtpServer )
props.put("mail.smtp.auth", "true")
val session = Session.getInstance(props)
val message = new MimeMessage(session)
message.setFrom(new InternetAddress(fromMail))
message.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail))
message.setSubject(mailTitle)
message.setContent(mailContent, "text/html;charset=utf-8")
//message.setText(mailContent)
message.setSentDate(new Date())
message.saveChanges()
val transport = session.getTransport("smtp")
transport.connect(user, password)
transport.sendMessage(message, message.getAllRecipients())
transport.close()
} catch {
case _ : Throwable => logger.info("This Mail Didnot Send:[ " + mailTitle + " ] [ " + mailContent + "]" )
}
}
}
示例9: EmailSender
//设置package包名称以及导入依赖的类
package actors.helpers
import javax.mail._
import javax.mail.internet.{InternetAddress, MimeMessage}
import model.b2c.Flat
import play.api.Configuration
class EmailSender (configuration: Configuration) {
val localizationHelper = new LocalizationHelper
val props = new java.util.Properties()
props.put(EmailSender.SmtpPropStartTls, "true")
props.put(EmailSender.SmtpPropSmtpAuth, "true")
props.put(EmailSender.SmtpPropSmtpHost, configuration.get[String](EmailSender.SmtpHost))
props.put(EmailSender.SmtpPropSmtpPort, configuration.get[String](EmailSender.SmtpPort))
val session = Session.getInstance(props,
new javax.mail.Authenticator() {
override protected def getPasswordAuthentication(): javax.mail.PasswordAuthentication = {
return new PasswordAuthentication(configuration.get[String](EmailSender.SmtpUsername),
configuration.get[String](EmailSender.SmtpPassword))
}
});
def sendEmail(flat: Flat) = {
val message = new MimeMessage(session)
message.setText(views.html.Application.notification
.render(flat,localizationHelper.getMessages(Language.EN))
.body,"utf-8", "html")
message.setFrom(new InternetAddress(configuration.get[String](EmailSender.SentFrom)))
message.setSubject(flat.address.get+ ", "+flat.district.get+", "+flat.city.get+", " +flat.price.get+ " EUR")
message.setRecipients(Message.RecipientType.TO,
configuration.get[String](EmailSender.SendToList).split(",")
.map(email => {
val address: Address = new InternetAddress(email)
address
}
).array
)
Transport.send(message)
}
}
object EmailSender {
val SmtpHost = "smtp.host"
val SmtpPort = "smtp.port"
val SmtpUsername = "smtp.username"
val SmtpPassword = "smtp.password"
val SendToList = "smtp.sendto"
val SentFrom = "smtp.sentfrom"
val SmtpPropStartTls = "mail.smtp.starttls.enable"
val SmtpPropSmtpAuth = "mail.smtp.auth"
val SmtpPropSmtpHost = "mail.smtp.host"
val SmtpPropSmtpPort = "mail.smtp.port"
}