从scala发送电子邮件 - 身份验证失败的异常

时间:2017-03-02 01:36:01

标签: scala smtp

我正在尝试从scala发送电子邮件(gmail)。这是我到目前为止所拥有的 -

import javax.mail._
import javax.mail.internet._

// Get the user's message
var bodyText = "Hello World!"


// Set up the mail object
val props = System.getProperties
props.setProperty("mail.smtp.host", "smtp.gmail.com")
props.setProperty("mail.smtp.user","user");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.debug", "true");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable","true");
props.setProperty("mail.smtp.EnableSSL.enable","true");

val session = Session.getInstance(props)
val message = new MimeMessage(session)

// Set the from, to, subject, body text
message.setFrom(new InternetAddress("myemail@gmail.com"))
message.setRecipients(Message.RecipientType.TO, "myemail@gmail.com")
message.setSubject("First email")
message.setText(bodyText)

// And send it
Transport.send(message)

我得到的错误只是

javax.mail.AuthenticationFailedException

我知道我可能需要在某个地方提供密码,但我正在按照教程http://langref.org/scala/networking/smtp/send-an-email进行操作,而且他们不需要密码。

1 个答案:

答案 0 :(得分:0)

根据http://www.oracle.com/technetwork/java/faq-135477.html#smtpauth,您应该使用Transport.send(message, user, password)