0

while sending the email with the attachment, i am getting the subjected error. it is working fine with local system file location(eg. C://PrepaidEMFiles//invoice123.pdf) but when i am using (http://182.18.177.27:78/PrepaidEMFiles/invoice1547033786877.pdf) location i am getting the error.below is my code.

try{
 `enter code here`....

  String samplePdf = 
  "http://182.18.177.27:78/PrepaidEMFiles/invoice1547033786877.pdf";

   FileSystemResource file = new FileSystemResource(samplePdf); 
   helper.addAttachment(file.getFilename(), file);

  } catch (MessagingException e) {
   LOGGER.error("Exception in sending mail", e);
   exceptionHandlerDao.exceptionHandlineCode(e, "SENDING EMAIL FOR " + 
   inventoryName + " FOR RECHARGE A DEVICE", "");
   }

    sender.send(message);
    }

I have tried with varies way to ignore "\" like(

eg.http:\\\\182.18.177.27:78\\PrepaidEMFiles\\invoice1547033786877.pdf) but still getting the same error.Could someone please help me with that error.

thanks in advance!!

i have tried various ways to get the file with URL url = inputstream example. but i am getting the binary file as a attachment.Still no use.

Complete error: java.nio.file.InvalidPathException: Illegal char <:> at index 4:

      http:\182.18.177.27:78\PrepaidEMFiles\invoice1547033786877.pdf
     at 
       sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
       at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
      at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
      at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
      at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
      at java.io.File.toPath(File.java:2234)
      at org.springframework.core.io.FileSystemResource.<init> 
     (FileSystemResource.java:82)
     at

I need that remotely located pdf file to send it to email attachment.

K.Bhanuprakash
  • 3
  • 1
  • 1
  • 3
  • sir, can i use a that ClassLoader solution to get a file from remote location to send it as a email attachment.( or i mean,is there a any better way to get the .pdf file from my server to send it as a attachement to mails). thanks in advance – K.Bhanuprakash Jan 11 '19 at 17:17
  • Sir, the above code copies the file from one location to other, but i should not do that i just have to take the file from remote location and send to email( not to store) – K.Bhanuprakash Jan 11 '19 at 17:24

1 Answers1

1

The problem is that you are using a FileSystemResource and this isn't on the file system. Its a URL hosted externally. You probablyly should be using a URLResource instead of a FileResource.

See more in here documentation

The URLResource should be able to make sense of the HTTP URL that you have.

Nicolás Alarcón Rapela
  • 2,714
  • 1
  • 18
  • 29