By default, the mail command in Linux will make use of the local SMTP server running on port 25 for sending mails. But if you want to send mails using external SMTP server such as smtp.gmail.com or SMTP server of your organization, then you can use mailx command. Using mailx command is quite easy, all you need to do is install mailx package and pass few SMTP related arguments to the command
Install mailx on RHEL/CentOS:
# yum install mailx
Install mailx on Debian/Ubuntu:
# apt-get install mailutils
Send mails with External SMTP server using mailx command:
# echo "The actual message goes here" | mailx -v -r "user@domain.com" -s "The actual subject line goes here" -S smtp="smtp.domain.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="user@domain.com" -S smtp-auth-password="password123" -S ssl-verify=ignore the_recipient_email@domain.com
Note: Make necessary changes in the above command – such as smtp server address, port, connection type, smtp username and password.