0
我正在研究這個項目,我需要在spark數據框中獲取表數據並將其發送到郵件中。要使用的語言是Scala。通過電子郵件發送Spark DataFrame
數據框,以保存表數據如下: -
val sqlDfUT = hiveCon.sql("select * from UserTable")
我需要發送「sqlDfUT」作爲郵件正文中的郵件。
用於發送郵件的代碼是:
sendScalaMail("[email protected]","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])
我會真的很高興,如果我能得到任何指導。謝謝
錯誤不與Spark連接,但與郵件API。順便說一句,你只需要一個很長的,沒有完整的DataFrame –
更新了這個問題,我想發送郵件中包含完整表格數據的數據幀(記錄更少)。 –
「地址」的類型不正確,應該是數組[地址]' –