【发布时间】:2014-10-01 22:12:01
【问题描述】:
我正在尝试从 groovy 脚本发送电子邮件。我给出了以下代码:
import org.apache.commons.net.smtp.*
port = 25
org = 'mycompany.org'
client = new SMTPClient()
client.connect('<server_name>', port)
client.login()
// set sender and recipient
client.sender = "<email_address>"
client.addRecipient("<email_address>")
// create and send header information
header = new SimpleSMTPHeader("<email_address>",
"<email_address>", 'Successful build')
header.addCC("<email_address>")
header.addHeaderField('Organization', org)
writer = new PrintWriter(client.sendMessageData())
writer << header
// send body of message
writer << 'Successful build for ' + new Date()
writer.close()
client.logout()
client.disconnect()
fixture.assertEmailArrived(from: "cruise@$org",
subject: 'Successful build')
我正在使用 apache 的 commons-net-2.0.jar 来运行代码。错误信息如下:
Caught: groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method java.io.PrintWriter#<init>.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[class java.lang.String]
[class java.io.File]
[class java.io.Writer]
[class java.io.OutputStream]
groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method java.io.PrintWriter#<init>.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[class java.lang.String]
[class java.io.File]
[class java.io.Writer]
[class java.io.OutputStream]
at TestEmail.run(TestEmail.groovy:20)
错误似乎来自这部分代码:
writer = new PrintWriter(client.sendMessageData())
我试图打印出 client.sendMessageData() 并且该值显示为 null。理想情况下,它应该是一些表示 Writer 对象的值。请帮我解决问题。
【问题讨论】:
-
您不需要任何身份验证吗?
login方法调用的结果是什么? -
我相信它正在工作。我尝试使用代码
System.out<<client.login()将其打印出来,并打印出true。所以我认为它必须工作。当我打印出client.sendMessageData()时,我得到一个空值。
标签: email groovy apache-commons-net