Reputation: 333
I am working on this project where I need to get the table data in a spark dataframe and send it in a mail. The language to be used is Scala.
The dataframe to hold the table data is as follows:-
val sqlDfUT = hiveCon.sql("select * from UserTable")
I need to send "sqlDfUT" as a message body in a mail.
The code for sending the mail is:
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)
}
} }
However, I am getting the error in line
msg.setRecipients(Message.RecipientType.TO, address)
and the error message is
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])
I will be really glad if I could get any guidance. Thank you
Upvotes: 3
Views: 12897
Reputation: 59
@Ritesh,
I tried similar thing using javamailAPI thing but in my case its not sending an dataframe and its expecting a String to be a message body
Below is my code
var bodyText = "Delta File"
val username = "[email protected]"
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)
in case of text message its working fine
Upvotes: 1
Reputation: 333
var address:Address = new InternetAddress(toMailId).asInstanceOf[Address]
this worked for me!! Thank you Raphael :)
Upvotes: 3