groovy - Sending an email: no object DCH for MIME type multipart/mixed -
i'm trying send email using java code running groovyconsole. works fine when send email without attachments fails add in multipart logic attachments.
edit: i'm using javamail version 1.4.7
this error i'm getting.
javax.activation.unsupporteddatatypeexception: no object dch mime type multipart/mixed; boundary="----=_part_16_24710054.1375885523061" @ javax.activation.objectdatacontenthandler.writeto(datahandler.java:891)
and it's happening on line transport.send(mimemsg) below.
import java.util.properties; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; properties properties = new properties() session session mimemessage mimemsg properties.put("mail.smtp.host", "[my host ip]") session = session.getdefaultinstance(properties) mimemsg = new mimemessage(session) string recipient = "[to email address]" mimemsg.addrecipient(message.recipienttype.to, new internetaddress(recipient)) mimemsg.setsubject("this subject") mimemsg.settext("this message #1") mimemsg.setfrom(new internetaddress("[from email address]")) bodypart messagebodypart = new mimebodypart() messagebodypart.settext(mimemsg.getcontent()) multipart multipart = new mimemultipart() string filepath = "[full & correct path test.txt]" datasource source = new filedatasource(filepath) messagebodypart.setdatahandler(new datahandler(source)) string filename = "test.txt" messagebodypart.setfilename("test.txt") multipart.addbodypart(messagebodypart) mimemsg.setcontent(multipart) transport.send(mimemsg) println "message sent!"
i think it's classloader issue way groovyconsole runs...
if add following @grab
, @grabcconfig
script, 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 subject' message.text = 'this message #1' message.from = new internetaddress( 'from address' ) def textpart = new mimebodypart() textpart.text = 'this 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 2 @grab
, @grabconfig
lines, , run command line with:
groovy -cp /path/to/mail-1.4.7.jar:/path/to/activation-1.1.jar mail.groovy
Comments
Post a Comment