I am able to send emails in Linux via commandline:
cat < x.txt | mail -s "SUBJECT" email@email.com
This works perfectly. Note: The body is in x.txt
.
Now, I want to execute exactly this command with Java.
Process p = Runtime.getRuntime().exec(
new String[] { "cat < x.txt", "|", "mail -s", "SUBJECT",
"email@email.com" });
p.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
String output = "";
while ((line = buf.readLine()) != null) {
output += line + "\n";
}
System.out.println(output);
Well, It is not working and I am getting following error.
Exception in thread "main" java.io.IOException: Cannot run program "cat < x.txt |": error=2, Datei oder Verzeichnis nicht gefunden
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:485)
at test.main(test.java:9) Caused by: java.io.IOException: error=2, Datei oder Verzeichnis nicht gefunden
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 3 more
Am I splitting those command(s) wrong?
How can I execute this command properly?