I think it's a classloader issue to do with the way GroovyConsole runs...
If I add the following @Grab
and @GrabcConfig
to the script, it works...
@Grab( 'javax.mail:mail:1.4.7' )
@GrabConfig(systemClassLoader=true, initContextClassLoader=true)
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
def props = new Properties().with { p ->
p.'mail.smtp.host' = 'my mail server'
p
}
def session = Session.getDefaultInstance( props )
def message = new MimeMessage( session )
message.addRecipient( Message.RecipientType.TO, new InternetAddress( 'to address' ) )
message.subject = 'This is the subject'
message.text = 'This is the message #1'
message.from = new InternetAddress( 'from address' )
def textpart = new MimeBodyPart()
textpart.text = 'This is the message #2'
def attachment = new MimeBodyPart()
attachment.dataHandler = new DataHandler( new FileDataSource( '/path/to/file.txt' ) )
attachment.fileName = 'file.txt'
def multi = new MimeMultipart()
multi.addBodyPart( textpart )
multi.addBodyPart( attachment )
message.content = multi
Transport.send( message )
Or, remove the two @Grab
and @GrabConfig
lines, and run it from the command line with:
groovy -cp /path/to/mail-1.4.7.jar:/path/to/activation-1.1.jar mail.groovy