0

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

nandini
  • 428
  • 2
  • 9
  • 20

2 Answers2

1

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.

Community
  • 1
  • 1
Anuj Arora
  • 3,017
  • 3
  • 29
  • 43
0

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

Dónal
  • 185,044
  • 174
  • 569
  • 824