Java Command Line Email Primer Tutorial

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.

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 *