import java.io.*; import java.net.*; /* javaMailTo.java Send email via mailto: URL RJM Programming February, 2018 Usage: Text in Body Usage: java javaMailTo [] [ [ [ []]]] Attachment Usage: uuencode [] | java javaMailTo [] [ [ []]] Attachments Usage: ( uuencode []; uuencode [] ) | java javaMailTo [] [ [ []]] Attachments Zipped Usage: zip [ [ ]]; uuencode [] | java javaMailTo [] [ [ []]] Attachment with Text Body Usage: ( echo 'Text of Body'; uuencode [] ) | java javaMailTo [] [ [ []]] Attachments with Text Body Usage: ( echo 'Text of Body'; uuencode []; uuencode [] ) | java javaMailTo [] [ [ []]] Attachments Zipped with Text Body Usage: zip [ [ ]]; ( echo 'Text of Body'; uuencode [] ) | java javaMailTo [] [ [ []]] */ public class javaMailTo { public static void main(String[] args) { try { int next = 0; BufferedReader myIn = null; if (args.length >= (next + 1)) { if (args[next].indexOf("@") == -1) { System.getProperties().put("mail.host", args[next]); next++; } } String myFrom = ""; String myTo = ""; String mySubject = ""; String myBody = ""; String mySubjectSpare = ""; String myBodySpare = ""; String replyTo = ""; String cc = ""; String bcc = ""; if (args.length >= (next + 1)) { myFrom = args[next]; next++; } else { if (myIn == null) myIn = new BufferedReader(new InputStreamReader(System.in)); System.out.print("From: "); myFrom = myIn.readLine(); } if (args.length >= (next + 1)) { myTo = args[next]; next++; } else { if (myIn == null) myIn = new BufferedReader(new InputStreamReader(System.in)); System.out.print("To: "); myTo = myIn.readLine(); } if (args.length >= (next + 1)) { mySubjectSpare = args[next]; next++; if (mySubjectSpare.indexOf("@") == -1) { mySubject = "" + mySubjectSpare; } else if (cc == "") { if (replyTo == "") replyTo = "Reply-To: " + mySubjectSpare; cc = "CC: " + mySubjectSpare; if (args.length >= (next + 1)) { mySubjectSpare = args[next]; next++; if (mySubjectSpare.indexOf("@") == -1) { mySubject = "" + mySubjectSpare; } else if (bcc == "") { bcc = "BCC: " + mySubjectSpare; } } } } if (args.length >= (next + 1) || mySubject != "") { if (mySubject == "") { mySubject = args[next]; next++; } } else { if (myIn == null) myIn = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Subject: "); mySubject = myIn.readLine(); } URL myU = new URL("mailto:" + myTo); URLConnection myC = myU.openConnection(); myC.setDoInput(false); myC.setDoOutput(true); System.out.println("Connecting..."); System.out.flush(); myC.connect(); PrintWriter myOut = new PrintWriter(new OutputStreamWriter(myC.getOutputStream())); if (args.length >= (next + 1) || myBody != "") { if (myBody == "") { myBody = args[next]; next++; } myOut.println("From: \"" + myFrom + "\" <" + System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName() + ">"); /* if (replyTo != "") myOut.println(replyTo); */ myOut.println("To: " + myTo); if (cc != "") myOut.println(cc); if (bcc != "") myOut.println(bcc); myOut.println("Subject: " + mySubject); myOut.println(); myOut.println(myBody); myOut.close(); System.out.println("Message sent now."); System.out.flush(); } else { myOut.println("From: \"" + myFrom + "\" <" + System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName() + ">"); /* if (replyTo != "") myOut.println(replyTo); */ myOut.println("To: " + myTo); if (cc != "") myOut.println(cc); if (bcc != "") myOut.println(bcc); myOut.println("Subject: " + mySubject); myOut.println(); System.out.println("Enter the message. " + "End with a '.' on a line by itself."); if (myIn == null) myIn = new BufferedReader(new InputStreamReader(System.in)); String myLine; for (;;) { myLine = myIn.readLine(); if ((myLine == null) || myLine.equals(".")) break; myOut.println(myLine); } myOut.close(); System.out.println("Message sent."); System.out.flush(); } } catch (Exception err) { System.err.println(err); System.err.println("Text Body Usage: java javaMailTo [] [ [ [ []]]]"); System.err.println("Attachment Usage: uuencode [] | java javaMailTo [] [ [ [ []]]]"); System.err.println("Attachments Usage: ( uuencode []; uuencode [] ) | java javaMailTo [] [ [ [ []]]]"); System.err.println("Attachments Zipped Usage: zip [ [ ]]; uuencode [] | java javaMailTo [] [ [ [ []]]]"); System.err.println("Attachment with Text Body Usage: ( echo 'Text of Body'; uuencode [] ) | java javaMailTo [] [ [ []]]"); System.err.println("Attachments with Text Body Usage: ( echo 'Text of Body'; uuencode []; uuencode [] ) | java javaMailTo [] [ [ []]]"); System.err.println("Attachments Zipped with Text Body Usage: zip [ [ ]]; ( echo 'Text of Body'; uuencode [] ) | java javaMailTo [] [ [ []]]"); } } }