Java Command Line Email Attachment Tutorial

Java Command Line Email Attachment Tutorial

Java Command Line Email Attachment Tutorial

Call me simple headed, but I believe the concept of an attachment in an email is one of the best concepts to have come out of Information Technology ever since … email. Attachments represent to us that there really was something to say in an email that benefits from what an email’s best role is. Attachments also help we owners of small time web servers with limited storage capacity, where an email attachment solution to a “sharing” issue can save storing files permanently on these smaller web server resources.

Yesterday’s Java Command Line Email Primer Tutorial didn’t mention the idea that the Java program we wrote can be piped to or get its input via a < redirection (for input) also, to make it a command line “tool” (or at least what we think of a “tool” as).

Don’t know about you, but whenever we write a piece of software that feels like a “tool” a certain sense of satisfaction prevails. We get great satisfaction out of command line …

  • piping
  • redirection

… because these features of the operating system, to us, add a feeling of interconnectedness, and we being “beings” from the time when you thought a lot about “processes” (and procedures) with desktop applications, though “piping” and “redirection” syntax can look odd, somehow to me (and in all reality probably true) to involve these two in the achievement of software aims, means you are being efficient with how you use the computer’s CPU and memory?!

We find, explaining “piping” and “redirection” is to try to explain “input/output”, in simple terms …

  • piping symbol | when on the left of a (sub)command represents input and when on the right, output (relative to that (sub)command) in question) … and the thing about a “pipe”, as you know from life, is that one (sub)command’s “view” of a pipe can be different to another (sub)command’s “view” of that same pipe (in other words the same “pipe” can represent one (sub)command’s output and also represent the next (sub)command’s input) … cute, huh?!
  • redirection < is input so that the (sub)command on the left of the < gets its input from the (whattttevvvvvver … but usually a) file on the right of the <
  • redirection > is output so that the (sub)command on the left of the > sends its output to the (whattttevvvvvver … but often a) file on the right of the > … and >> is to all intents and purposes the same idea, but the output is appended to any pre-existant (file) data

So, let’s use some “piping” to show you some ways to use … after compiling …


javac javaMailTo.java # changed this way to cater for CC: and BCC: recipient scenarios

… as per …

Text in Body of Email Usage: java javaMailTo [<mailhost>] [<From> [<To> [<CC>] [<BCC>] [<Subject> [<TextOfBody>]]]]

Attachment Usage: uuencode <file.nam> [<attachmentfile.nam>] | java javaMailTo [<mailhost>] [<From> [<To> [<CC>] [<BCC>] [<Subject>]]]
Attachments Usage: ( uuencode <file.nam> [<attachmentfile.nam>];uuencode <file2.nam> [<attachmentfile2.nam>] ) | java javaMailTo [<mailhost>] [<From> [<To> [<CC>] [<BCC>] [<Subject>]]]
Attachments Zipped Usage: zip <zipfile.nam> <file1.nam> [ <file2.nam>[ <file3.nam>]]; uuencode <zipfile.nam> [<attachmentzipfile.nam>] | java javaMailTo [<mailhost>] [<From> [<To> [<CC>] [<BCC>] [<Subject>]]]
Attachment with Text Body Usage: ( echo ‘Text of Body'; uuencode <file.nam> [<attachmentfile.nam>] ) | java javaMailTo [<mailhost>] [<From> [<To> [<CC>] [<BCC>] [<Subject>]]]
Attachments with Text Body Usage: ( echo ‘Text of Body'; uuencode <file.nam> [<attachmentfile.nam>]; uuencode <file2.nam> [<attachmentfile2.nam>] ) | java javaMailTo [<mailhost>] [<From> [<To> [<CC>] [<BCC>] [<Subject>]]]
Attachments Zipped with Text Body Usage: zip <zipfile.nam> <file1.nam> [<file2.nam>[ <file3.nam>]]; ( echo ‘Text of Body'; uuencode <zipfile.nam> [<attachmentzipfile.nam>] ) | java javaMailTo [<mailhost>] [<From> [<To> [<CC>] [<BCC>] [<Subject>]]]

… noting that where we go, just, echo ‘Text of Body'; for textual body (of email) content, doesn’t mean you can’t go (something like) cat aTextFile.txt; to do textual body (of email) content of several lines perhaps.


Previous relevant Java Command Line Email Primer Tutorial is shown below.

Java Command Line Email Primer Tutorial

Java Command Line Email Primer Tutorial

Became glad that we’d talked about the incredibly useful Linux mailx command when we presented Linux mailx Primer Tutorial. To wait this long to have presented some information about this gem of a (Linux or Unix or Mac OS X (like BSD)) command line command would be pretty bad. All that water under the bridge to today, and we come at a command line (for us, a Mac OS X Terminal application one) Java (URLConnection mailto: URL (ie. email client)) emailer.

Our emailer in Java compiled via the incredibly simple mandatory and more than likely needed and optionally put here or prompted for during the Java program execution part command …


javac javaMailTo.java
java javaMailTo mail.optusnet.com.au User@users-macbook-pro-2.local rmetcalfe15@gmail.com "The Subject of it all at Once" "The Body of it all at Once"

… those arguments being …

  • java … Java engine
  • javaMailTo … Java class (program) that is the output of the Java “javac” compiler’s work
  • mail.optusnet.com.au … mailhost to use, which in our case points at our ISP (Optus) mail server (for a Mac OS X Mail application setup on this MacBook Pro we are sending emails from (the command line of)) … as looked up at this Optus advice link
  • User@users-macbook-pro-2.local … a From: value (email address) that reflects our username of use … initially got this wrong as being the same as To: below, and in that scenario the email went to the Spam folder for me, and tweaked that this From: should be that Spam email’s sender … do that and the email no longer goes to Spam
  • rmetcalfe15@gmail.com … a To: email address
  • “The Subject of it all at Once” … a Subject: of several words can be encased by double quotes
  • “The Body of it all at Once” … a Body (of email) section finishes off the requirements

Not as cute to use as mailx but tailorable for your own purposes perhaps?!

We would like to thank the wonderful Java Examples in a Nutshell by David Flanagan (ISBN: 1-56592-371-5) for the basis in code of today’s Java.

You might want to also read Windows and Mac Command Line Gmail Email Primer Tutorial.


Previous relevant Linux mailx Primer Tutorial is shown below.

Linux mailx Primer Tutorial

Linux mailx Primer Tutorial

Think Linux and you may not associate it with email, but Linux can be excellent for many email requirements, especially using mailx and uuencode. The use of uuencode allows you to give your email an attachment, and the mailx -s switch for the subject of the email can be used for both the purpose it was meant for, that is to establish an email subject, and with a stretch of imagination of usage, the body text, because of the quirk whereby anything after the first line of the subject will spill over into the body text of the email. So, am sure there would be limits to the length of this body text, but you can often be brief when there is a good attachment to provide your email recipient. Naturally, in the day and age of worry about computer viruses, your attachment will most likely be vetted by the receiving email client software for viability, so be aware of this. Other than all that, this method of sending emails is potentially very powerful. Notice that great way Linux and Unix can use `cat body.txt` type of syntax to embed one command within another … sometimes Linux is so simple, powerful, brilliant! You may have noticed how short all the commands in Linux and Unix tend to be, and that is because it was intended to be short and powerful, and this is brilliant.

Background reading for tutorial:

Here is a tutorial that introduces you to some email with attachment, subject and body text from the Linux command line.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>