通过电子邮件发送Spark DataFrame

时间:2016-12-02 09:30:57

标签: scala javamail

我正在研究这个项目,我需要在火花数据帧中获取表格数据并通过邮件发送。要使用的语言是Scala。

保存表数据的数据帧如下: -

val sqlDfUT = hiveCon.sql("select * from UserTable")

我需要在邮件中发送“sqlDfUT”作为邮件正文。

发送邮件的代码是:

sendScalaMail("monitor@foo.com","Users Data Sent : \n " + sqlDfUT +
                    ",\nMail sent from host: " + java.net.InetAddress.getLocalHost().getHostName(),
                    "12525","Hive Data Checking completed for given User: 12525" )

def sendScalaMail (mailSender:String, strMailBody:String, mailIdList:String, strMailSubj:String)={

if ((mailIdList == null) && (mailIdList.equals(""))){

  writeToLog("Email ID not defined")
}
writeToLog("<----Sender---->"+mailSender)
writeToLog("<----strMailBody---->"+strMailBody)
writeToLog("<----mailIdList---->"+mailIdList)
writeToLog("<----strMailSubj---->"+strMailSubj)

val smtpHost:String = "mail.foo.com"
val prop:Properties = new Properties()
prop.put("mail.smtp.host", smtpHost)
prop.put("mail.debug", "false")
var session:Session = Session.getInstance(prop)
var toPersonList:Array[String] = mailIdList.split(",")

var toMailListSB:StringBuffer = new StringBuffer()
var toPersonName:String = ""
var toMailId:String = ""
var index:Int = 0

for(index <- 0 to toPersonList.length){

  toPersonName = toPersonList(index).asInstanceOf[String]
  toMailId = toPersonName+"@mail.foo.com"
  toMailListSB.append(toMailId)
  toMailListSB.append(";")

}
try{
  var msg:MimeMessage = new MimeMessage(session)
  msg.setFrom(new InternetAddress(mailSender))
  var toList:Array[String] = toMailListSB.toString().split(",")
  var address:Array[InternetAddress] = new InternetAddress(toList.length.toString()).asInstanceOf[Array[InternetAddress]]
  var i:Int = 0
  for(i <- 0 to toList.length){
    address(i) = new InternetAddress(toList(i))
  }
  msg.setRecipients(Message.RecipientType.TO, address)
  msg.setHeader("Content-Type", "text/html")
  msg.setSubject(strMailSubj)
  msg.setSentDate(new Date())       
  msg.setContent(strMailBody, "text/html")

  Transport.send(msg)

}
catch{
  case me:MessagingException =>{
    me.printStackTrace()
    writeToLog("<---Error in method sendScalaMail--->"+me)
  }
} }

但是,我收到了错误

msg.setRecipients(Message.RecipientType.TO, address)

,错误消息是

overloaded method value setRecipients with alternatives: (x$1: javax.mail.Message.RecipientType,x$2: String)Unit <and> (x$1: javax.mail.Message.RecipientType,x$2: Array[javax.mail.Address])Unit cannot be applied to (javax.mail.Message.RecipientType, Array[javax.mail.internet.InternetAddress])

如果能得到任何指导,我将非常高兴。谢谢

2 个答案:

答案 0 :(得分:2)

var address:Address = new InternetAddress(toMailId).asInstanceOf[Address]
这对我有用!!谢谢拉斐尔:)

答案 1 :(得分:0)

@Ritesh,

我尝试使用javamailAPI做类似的事情,但是在我的情况下,它没有发送数据帧,并且期望String作为消息正文

下面是我的代码

  var bodyText = "Delta File"
  val username = "test@gmail.com"
  val password = "test"
  val smtpHost = "smtp.gmail.com"

 // Set up the mail object
 val properties = System.getProperties
 properties.put("mail.smtp.host", smtpHost)
 properties.put("mail.smtp.user", username);
 properties.put("mail.smtp.password", password);
 properties.put("mail.smtp.auth", "true");
 properties.put("mail.smtp.port", "587")
 properties.put("mail.smtp.starttls.enable", "true");

 val auth:Authenticator = new Authenticator() {
 override def getPasswordAuthentication = new
    PasswordAuthentication(username, password)
 }

 val session = Session.getInstance(properties,auth)
val message = new MimeMessage(session)

 // Set the from, to, subject, body text
 message.setFrom(new InternetAddress("test@****.com"))
 message.setRecipients(Message.RecipientType.TO, "sim@****.com")
 message.setHeader("Content-Type", "text/html")
 message.setSubject("Count of DeviceIDs we are sent daily")
 df = spark.sql("select * from test table")
 message.setContent(df, "text/html")

 // And send it
 Transport.send(message) 

在发短信的情况下可以正常工作