nandini
nandini

Reputation: 428

Attach a CSV in Mail

I want to attach a csv file in mail(grails)

The file in the path is already present. I am using the following code

sendMail {
    multipart true
    from "$senderName <$fromAddress>"
    to toAddress
    cc message.cc
    subject message.subject
    body content.plaintext
    html content.html
    attachBytes './web-app/ReadyOrdersFor-${vendor.name}','text/csv', new File('./web-app/ReadyOrdersFor-${vendor.name}').readBytes()
}

Error prompted is.
java.io.FileNotFoundException: ./web-app/ReadyOrdersFor-${vendor.name}.csv (No such file or directory)

neither this works prompting the same error

attachBytes './web-app/ReadyOrdersFor-${vendor.name}.csv','text/csv', new File('./web-app/ReadyOrdersFor-${vendor.name}.csv').readBytes()

Upvotes: 0

Views: 576

Answers (2)

D&#243;nal
D&#243;nal

Reputation: 187529

Instead of trying to get a File reference using new File(path), use the Spring ResourceLoader interface. The ApplicationContext implements this interface, so you can get a reference to it from a controller (for example) like this:

class MyController implements ApplicationContextAware {

  private ResourceLoader resourceLoader

  void setApplicationContext(ApplicationContext applicationContext) {
    resourceLoader = applicationContext  
  }

  def someAction() {
    String path = "classpath:/ReadyOrdersFor-${vendor.name}"
    File csvFile = resourceLoader.getResource(path).file
  }
}

I'm not 100% sure the path value above is correct, you may need to remove the '/'

Upvotes: 0

Anuj Arora
Anuj Arora

Reputation: 3047

The issue is that you trying you use the file path string as a GStringImpl, but the string is enclosed in single quotes. GStringImpl is natively supported in groovy in double quotes.

You code should be

attachBytes "./web-app/ReadyOrdersFor-${vendor.name}",'text/csv', new File("./web-app/ReadyOrdersFor-${vendor.name}").readBytes()

This link should help you understand the difference between using single and double quotes in groovy.

Upvotes: 1

Related Questions