Mail Sender In Scala Not Working

While sending a mail through Scala I am getting the below error.
Please also find the code being used

java.lang.NullPointerException
at com.m.executor.Executor$.run(Executor.scala:43)
at com.m.App$.main(App.scala:25)
at com.m.App.main(App.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:736)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
javax.mail.internet.AddressException: Illegal address in string ``’’
at javax.mail.internet.InternetAddress.(InternetAddress.java:114)
at com.m.utils.EmailSender$$anonfun$sendEmail$1.apply(EmailSender.scala:32)
at com.m.utils.EmailSender$$anonfun$sendEmail$1.apply(EmailSender.scala:32)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
at com.m.utils.EmailSender$.sendEmail(EmailSender.scala:32)
at com.m.utils.EmailSender$.sendToUsers(EmailSender.scala:12)
at com.m.App$.main(App.scala:101)
at com.m.App.main(App.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:736)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
20/03/17 08:29:20 INFO SparkContext: Invoking stop() from shutdown hook

===========

package com.m

import javax.mail.internet.{InternetAddress, MimeMessage}
import javax.mail.{Message, MessagingException, Session, Transport}

object EmailSender{

private val developerMEmailID=Array(“abc@mm.com”)
private val sender="xyz@mm.com"
def sendToUsers(subject:String,emailBody:String,userEmailIDs:Array[String]): Unit ={
sendEmail(sender,userEmailIDs,developerMEmailID,subject,emailBody)
}

def sendEmail(sender:String, recipients:Array[String], toAddresses:Array[String], subject:String, body:String): Unit ={
// using host as localhost
val host = “”

// Getting system properties
val properties = System.getProperties
// Setting up mail server
properties.setProperty("mail.smtp.host", host)
// creating session object to get properties
val session = Session.getDefaultInstance(properties)
try {
  // MimeMessage object.
  val message = new MimeMessage(session)
  // Set From Field: adding senders email to from field.
  message.setFrom(new InternetAddress(sender))
  // Set To Field: adding recipient's email to from field.
  recipients.foreach(x=> message.addRecipient(Message.RecipientType.TO, new InternetAddress(x)))
  toAddresses.foreach(x=> message.addRecipient(Message.RecipientType.CC, new InternetAddress(x)))
  // Set Subject: subject of the email
  message.setSubject(subject)
  // set body of the email.
  message.setContent(body,"text/html")
  // Send email.
  Transport.send(message)
  println("Email successfully sent..")

}catch {
case mex: MessagingException =>
println(“Exception in Email Sender”)
mex.printStackTrace()
}
}

}

It looks like you have two exceptions. The first one

java.lang.NullPointerException

may be due to an initialization order issue (this is the most common case) and you can compile with -Xcheckinit to get a better error message if this is the case.

The second exception

javax.mail.internet.AddressException: Illegal address in string ``’’

seems to be saying that one of your addresses (sender or an element of recipients or toAddresses) is the empty string, so you should check for that.

1 Like