简体   繁体   中英

Groovy Attach multiple files with AntBuilder mail

I've got the below code. My problem is I want to send zero (or more) files, and I'm not sure how to do it. Ant requires you to set a base directory for your fileset, but for this method I don't know what that would be. How can I add an arbitrary list of zero or more files to be attached?

 public void send(File[] files){
        ant.mail (
            from: "IMSBatch@vanguard.com",
            tolist: to,
            message: msg,
            subject : subject,
            mailhost: host,
            messagemimetype: 'text/html'
        ){
            attachments(){
                fileset(dir: ????){
                    include(arbitrary list of files)
                }
            }
        }
    } 

Side note, I ran into a bug where this code actually broke when I had a setAttachments() method elsewhere in my class. I think either Ant or Groovy got names mixed up between that and the attachments method of AntBuilder.

How about like this?

@Grab(group='org.apache.ant', module='ant-javamail', version='1.9.4')
@Grab(group='javax.activation', module='activation', version='1.1.1')
@Grab(group='javax.mail', module='mail', version='1.4.7')
@GrabConfig(systemClassLoader=true)

// ...

public void send(File[] files) {
    String filesString = ""
    for (int i = 0; i < files.size(); i++) {
        filesString += f.canonicalPath
        if (files.size() > 1 && i < files.size() -1)
            filesString += ","
    }
    ant.mail(
        from: "IMSBatch@vanguard.com",
        tolist: to,
        message: msg,
        subject: subject,
        mailhost: host,
        messagemimetype: "text/html",
        files: filesString
    )
}

There might be a Groovier way of populating filesString , I'm open to suggestions to improve the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM