斯卡拉 - javax.mail.AuthenticationFailedException
scala - javax.mail.AuthenticationFailedException
我的目标是通过电子邮件发送数据帧。下面是我的一段代码:
import javax.mail._
import javax.mail.internet._
import org.apache.spark.{SparkConf, SparkContext}
object EmailAlert {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName("E-mail Alert").setMaster("local")
val sc = new SparkContext(sparkConf)
var bodyText = "Test mail"
// Set up the mail object
val properties = System.getProperties
properties.put("mail.smtp.host", "email-smtp.us-east-1.amazonaws.com")
properties.put("mail.smtp.user", "********");
properties.put("mail.smtp.password", "********");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "587")
properties.put("mail.smtp.starttls.enable", "true");
val session = Session.getInstance(properties)
val message = new MimeMessage(session)
def getPasswordAuthentication(username:String, password:String):Authenticator=
{
new Authenticator(){
override def getPasswordAuthentication():PasswordAuthentication = {
new PasswordAuthentication(username, password);
}}
}
// Set the from, to, subject, body text
message.setFrom(new InternetAddress("no-reply@****.com"))
message.setRecipients(Message.RecipientType.TO, "ajayv@****.com")
message.setSubject("Count of DeviceIDs we are sent daily")
message.setText(bodyText)
// And send it
Transport.send(message)
}
}
但是当我执行代码时出现以下错误:
Exception in thread "main" javax.mail.AuthenticationFailedException
我在这里错过了什么。
删除 Authenticator(根本未使用)和 mail.smtp.user
、mail.smtp.password
和 mail.smtp.auth
属性,然后调用 Transport.send method that takes a user name and password. If it still doesn't work, post the JavaMail debug output .
我需要将身份验证传递给会话,这是我错过的,下面的代码对我有用:
val sparkConf = new SparkConf().setAppName("E-mail Alert").setMaster("local")
val sc = new SparkContext(sparkConf)
var bodyText = "Test mail"
val username = "*****************"
val password = "************************"
val smtpHost = "email-smtp.us-east-1.amazonaws.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("no-reply@*****.com"))
message.setRecipients(Message.RecipientType.TO, "ajayv@****.com")
message.setSubject("Count of DeviceIDs we are sent daily")
message.setText(bodyText)
// And send it
Transport.send(message)
我的目标是通过电子邮件发送数据帧。下面是我的一段代码:
import javax.mail._
import javax.mail.internet._
import org.apache.spark.{SparkConf, SparkContext}
object EmailAlert {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName("E-mail Alert").setMaster("local")
val sc = new SparkContext(sparkConf)
var bodyText = "Test mail"
// Set up the mail object
val properties = System.getProperties
properties.put("mail.smtp.host", "email-smtp.us-east-1.amazonaws.com")
properties.put("mail.smtp.user", "********");
properties.put("mail.smtp.password", "********");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "587")
properties.put("mail.smtp.starttls.enable", "true");
val session = Session.getInstance(properties)
val message = new MimeMessage(session)
def getPasswordAuthentication(username:String, password:String):Authenticator=
{
new Authenticator(){
override def getPasswordAuthentication():PasswordAuthentication = {
new PasswordAuthentication(username, password);
}}
}
// Set the from, to, subject, body text
message.setFrom(new InternetAddress("no-reply@****.com"))
message.setRecipients(Message.RecipientType.TO, "ajayv@****.com")
message.setSubject("Count of DeviceIDs we are sent daily")
message.setText(bodyText)
// And send it
Transport.send(message)
}
}
但是当我执行代码时出现以下错误:
Exception in thread "main" javax.mail.AuthenticationFailedException
我在这里错过了什么。
删除 Authenticator(根本未使用)和 mail.smtp.user
、mail.smtp.password
和 mail.smtp.auth
属性,然后调用 Transport.send method that takes a user name and password. If it still doesn't work, post the JavaMail debug output .
我需要将身份验证传递给会话,这是我错过的,下面的代码对我有用:
val sparkConf = new SparkConf().setAppName("E-mail Alert").setMaster("local")
val sc = new SparkContext(sparkConf)
var bodyText = "Test mail"
val username = "*****************"
val password = "************************"
val smtpHost = "email-smtp.us-east-1.amazonaws.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("no-reply@*****.com"))
message.setRecipients(Message.RecipientType.TO, "ajayv@****.com")
message.setSubject("Count of DeviceIDs we are sent daily")
message.setText(bodyText)
// And send it
Transport.send(message)