2016-12-20 1 views
0

Mein Ziel ist es, einen Datenrahmen per E-Mail zu senden. unten ist mein Stück Code: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("[email protected]****.com")) 
message.setRecipients(Message.RecipientType.TO, "[email protected]****.com") 
message.setSubject("Count of DeviceIDs we are sent daily") 
message.setText(bodyText) 

// And send it 
Transport.send(message) 
    } 
} 

aber ich die folgenden Fehler, wenn ich den Code ausführen:

Exception in thread "main" javax.mail.AuthenticationFailedException

was Vermisse ich hier.

Antwort

0

Ich muss die Session-Authentifizierung übergeben, das, was ich ist bin verpasst, Below-Code für mich gearbeitet:

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("[email protected]*****.com")) 
message.setRecipients(Message.RecipientType.TO, "[email protected]****.com") 
message.setSubject("Count of DeviceIDs we are sent daily") 
message.setText(bodyText) 

// And send it 
Transport.send(message) 
1

Sie sich von der Authenticator befreien (die überhaupt nicht verwendet werden) und die mail.smtp.user, mail.smtp.password und mail.smtp.auth Eigenschaften, dann die Transport.send method that takes a user name and password nennen. Wenn es immer noch nicht funktioniert, poste die JavaMail debug output.

+0

Warum haben Sie sagen, dass sie gar nicht benutzt, wenn ja, was kann ich sie machen benutze – toofrellik

+0

Soweit ich das beurteilen kann (und ich bin kein Scala-Experte), wird der Authentifizierer, den du deklarierst, niemals benutzt. Normalerweise würde es an die Methode Session.getInstance übergeben. Wie auch immer, tu was ich gesagt habe; [Sie brauchen den Authentifikator nicht] (http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes). –