0

I am trying to send an email from a groovy script. I have given the code below:

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')

I am using the apache's commons-net-2.0.jar to run the code. The error message is as below:

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)

The error seems to come from this part of the code:

writer = new PrintWriter(client.sendMessageData())

I tried to print out the client.sendMessageData() and the value is coming out as null. It should ideally have been some value denoting a Writer object. Please help me resolve the issue.

Rohit Jose
  • 188
  • 3
  • 13

1 Answers1

4

The problem is the following:

new PrintWriter(client.sendMessageData())

I think sendMessageData returns null if the command fails for some reason. When you pass null to the PrintWriter constructor, Groovy's dynamic dispatch mechanism does not have any way to know which constructor you are trying to call. You could do this:

new PrintWriter((Writer)client.sendMessageData())

That will do away with the ambiguous method overloading issue, but still probably isn't what you want. You probably want to check the return value for null before instantiating the PrintWriter to begin with.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • Typecasting the response does resolve the ambiguous method overloading issue. But the code still fails. As you said the sendMessageData() is failing. I have racked my head a lot, but not able to figure out why it is failing. – Rohit Jose Oct 02 '14 at 14:04
  • The issue was on connecting to the SMTP server, there were firewall restrictions for the connection from systems of a different domain. Once that was fixed it works. Thanks @Jeff Scott Brown – Rohit Jose Oct 02 '14 at 14:29
  • 1
    @RohitJose "Typecasting the response does resolve the ambiguous method overloading issue. But the code still fails." - Right. That is what I was getting at when I said "That will do away with the ambiguous method overloading issue, but still probably isn't what you want. You probably want to check the return value for null before instantiating the PrintWriter to begin with.". I would think the solution would involve fixing the SMTP call so it works and in addition to that, reorganizing the logic in your code so that when it fails for some reason, you handle it rather than assume it succeeded – Jeff Scott Brown Oct 02 '14 at 14:47