sendmail - Java's System.getRuntime().exec not behaving as if the shell user called it -
i running java application console on hp-ux machine. in it, generate reports, zip them, , email them. working, except email.
i using mail binary send mail command line. since it's hp-ux, it's bit different standard gnu sendmail.
this code i'm using send mail:
public static void emailreports(string[] recipients, string reportarchive, string subject){ simpledateformat dateformat = new simpledateformat("mm-dd-yyyy"); string today = dateformat.format(new date()); file tempemailfile; bufferedwriter emailwriter; try { tempemailfile = file.createtempfile("report_email_" + today, "msg"); emailwriter = new bufferedwriter(new filewriter(tempemailfile)); } catch (ioexception e) { e.printstacktrace(); system.out.println("failed send email. not create temporary file."); return; } try { emailwriter.write("subject: " + subject + "\n"); emailwriter.write("from: " + + "\n"); emailwriter.write(body + "\n"); emailwriter.close(); } catch (ioexception e) { e.printstacktrace(); system.out.println("failed send email. not write temporary file."); } //read archive in try { fileinputstream archiveis = new fileinputstream(new file(reportarchive)); outputstream archiveencoder = mimeutility.encode(new fileoutputstream(tempemailfile, true), "uuencode", zipper.getarchivename(reportarchive)); //read archive byte[] buffer = new byte[archiveis.available()]; //these should never more megabyte or two, storing in memory no big deal. archiveis.read(buffer); //encode archive archiveencoder.write(buffer); //close both archiveis.close(); archiveencoder.close(); } catch (filenotfoundexception e) { system.out.println("failed send email. not find archive email."); e.printstacktrace(); } catch (messagingexception e) { system.out.println("failed send email. not encode archive."); e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); system.out.println("failed send email. not encode archive."); } system.out.println("sending '" + subject + "' email."); try { process p = runtime.getruntime().exec("mail me@example.com < " + tempemailfile.getabsolutepath()); system.out.println("mail me@example.com < " + tempemailfile.getabsolutepath()); stringbuffer buffer = new stringbuffer(); while(p.geterrorstream().available() > 0){ buffer.append((char) p.geterrorstream().read()); } system.out.println("stderr: " + buffer.tostring()); buffer = new stringbuffer(); while(p.getinputstream().available() > 0){ buffer.append((char) p.getinputstream().read()); } system.out.println("stdout: " + buffer.tostring()); } catch (ioexception e) { e.printstacktrace(); system.out.println("failed send email. not access shell."); } }
when run program, , sends email, blank email, no subject, no body, no attachment, , it's user@hostname hp-ux box instead of email specified in from
.
however, when run same line runs (see command printed out after call exec), correct email, correct user, subject, body, , attachment.
stdout , stderr both empty. it's if i'm sending mail blank file, when print file before call exec, it's there.
what's going on here?
edit: attempts made:
using ksh:
try { string cmd = "mail me@example.com.com < " + tempemailfile.getabsolutepath(); runtime.getruntime().exec(new string[] {"/usr/bin/ksh", cmd}); } catch (ioexception e) { e.printstacktrace(); system.out.println("failed send email. not access shell."); }
using stdin:
try { system.out.println("mail me@example.com < " + tempemailfile.getabsolutepath()); process p = runtime.getruntime().exec("mail me@example.com "); fileinputstream infile = new fileinputstream(tempemailfile); byte[] bytebuffer = new byte[infile.available()]; infile.read(bytebuffer); p.getoutputstream().write(bytebuffer); infile.close(); p.getoutputstream().close(); stringbuffer buffer = new stringbuffer(); while(p.geterrorstream().available() > 0){ buffer.append((char) p.geterrorstream().read()); } system.out.println("stderr: " + buffer.tostring()); buffer = new stringbuffer(); while(p.getinputstream().available() > 0){ buffer.append((char) p.getinputstream().read()); } system.out.println("stdout: " + buffer.tostring()); } catch (ioexception e) { e.printstacktrace(); system.out.println("failed send email. not access shell."); }
i suspect problem redirection. that's handled shell - , there's no shell here.
either need execute process , process's standard input stream , write java, or (probably simpler) run /bin/sh
(or whatever) shell redirection.
Comments
Post a Comment