Java - 简单邮件

时间:2012-09-01 12:43:48

标签: java email

我想用java发送电子邮件。

我正在使用Simple Java Mail

我的代码:

final Email email = new Email();
email.setFromAddress("Name", "username@test.com");
email.setSubject("hey");
email.addRecipient("Name", "myemail@test.com", RecipientType.TO);
email.setText("Testing...");
new Mailer("smtpout.secureserver.net", 25, "username@test.com", "password").sendMail(email);

我总是收到错误

[MailException: Generic error: null]

new Mailer

引发

我不知道导致这种情况的原因,我希望你能解决这个问题。

更新1:

好的,我得到AuthenticationFailedException。这很奇怪。

smtp服务器正确,端口正确,用户名和密码也正确。

org.codemonkey.simplejavamail.MailException: Generic error: null
    at org.codemonkey.simplejavamail.Mailer.sendMail(Mailer.java:249)
    at controllers.SignUp.blank(SignUp.java:46)
    at Routes$$anonfun$routes$1$$anonfun$apply$9$$anonfun$apply$10.apply(routes_routing.scala:156)
    at Routes$$anonfun$routes$1$$anonfun$apply$9$$anonfun$apply$10.apply(routes_routing.scala:156)
    at play.core.Router$HandlerInvoker$$anon$4$$anon$1.invocation(Router.scala:1086)
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:33)
    at play.GlobalSettings$1.call(GlobalSettings.java:57)
    at actions.UserAction.call(UserAction.java:18)
    at play.core.j.JavaAction$class.apply(JavaAction.scala:74)
    at play.core.Router$HandlerInvoker$$anon$4$$anon$1.apply(Router.scala:1085)
    at play.core.ActionInvoker$$anonfun$receive$1$$anonfun$6.apply(Invoker.scala:126)
    at play.core.ActionInvoker$$anonfun$receive$1$$anonfun$6.apply(Invoker.scala:126)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:17)
    at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:125)
    at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115)
    at akka.actor.Actor$class.apply(Actor.scala:318)
    at play.core.ActionInvoker.apply(Invoker.scala:113)
    at akka.actor.ActorCell.invoke(ActorCell.scala:626)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197)
    at akka.dispatch.Mailbox.run(Mailbox.scala:179)
    at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:516)
    at akka.jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:259)
    at akka.jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975)
    at akka.jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1479)
    at akka.jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)
Caused by: javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:319)
    at javax.mail.Service.connect(Service.java:169)
    at javax.mail.Service.connect(Service.java:118)
    at org.codemonkey.simplejavamail.Mailer.sendMail(Mailer.java:241)
    ... 24 more

1 个答案:

答案 0 :(得分:2)

此代码在我的机器上运行正常(我在sendMail上获取给定参数的ConnectException,但这是正常的)。您确定所有必需的依赖项都在您的类路径上吗?您需要在类路径上http://code.google.com/p/simple-java-mail/downloads/list处的所有5个罐子。