I know there is the command mail
in linux to send emails via command line. How can I send an simple email with one line from the terminal though?
For example:
mail [email protected] [subject] [body]
And have the email sent without any confirmation or prompts to the user?
The reason is, I want to send a brief message via email to myself when a specific event happens in a java
program. The idea is that I will use Runtime.getRuntime()
… etc. to send the mail command
from my java
program.
I used cron
to do something similar in the past, but the current implementation doesn't use cron
, so I need to try this out instead.
You can also use sendmail:
/usr/sbin/sendmail [email protected] < /file/to/send
You can install the mail package in Ubuntu with below command.
For Ubuntu -:
$ sudo apt-get install -y mailutils
For CentOs-:
$ sudo yum install -y mailx
Test Mail command-:
$ echo "Mail test" | mail -s "Subject" [email protected]
You can use an echo with a pipe to avoid prompts or confirmation.
echo "This is the body" | mail -s "This is the subject" [email protected]
$ mail -s "test message from centos" [email protected]
hello from centos linux command line
Ctrl+D to finish
For Ubuntu users: First You need to install mailutils
sudo apt-get install mailutils
Setup an email server, if you are using gmail or smtp. follow this link. then use this command to send email.
echo "this is a test mail" | mail -s "Subject of mail" [email protected]
In case you are using gmail and still you are getting some authentication error then you need to change setting of gmail:
Turn on Access for less secure apps from here
echo "Subject: test" | /usr/sbin/sendmail [email protected]
This enables you to do it within one command line without having to echo a text file. This answer builds on top of @mti2935's answer. So credit goes there.
Source: Stackoverflow.com