从linux终端发送邮件一行 [英] send mail from linux terminal in one line
问题描述
我知道linux中有命令 mail
通过命令行发送电子邮件。如何从终端发送一条简单的电子邮件?
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?
例如:
mail user@gmail.com [subject] [body]
发送电子邮件没有任何确认或提示给用户?
And have the email sent without any confirmation or prompts to the user?
原因是,当在 java
程序中发生特定事件时,我想通过电子邮件向我发送简短的消息。这个想法是,我将使用 Runtime.getRuntime()
...等从邮件命令
从我的 java
程序。
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.
我使用 cron
在过去做类似的事情,但是当前的实现不使用 cron
,所以我需要尝试这个。
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.
推荐答案
可以在linux系统上代表相当多的程序。您要背后的是 sendmail 或后缀。我推荐后者。
mail can represent quite a couple of programs on a linux system. What you want behind it is either sendmail or postfix. I recommend the latter.
您可以通过您最喜欢的软件包管理器进行安装。那么你必须配置它,一旦你这样做,你可以发送这样的电子邮件:
You can install it via your favorite package manager. Then you have to configure it, and once you have done that, you can send email like this:
echo "My message" | mail -s subject user@gmail.com
有关详细信息,请参阅手册。
See the manual for more information.
就配置后缀而言,互联网上有很多关于如何做的文章。
除非您在具有注册域名的公共服务器上,否则您通常希望将电子邮件转发到可以从中发送电子邮件的SMTP服务器。
As far as configuring postfix goes, there's plenty of articles on the internet on how to do it. Unless you're on a public server with a registered domain, you generally want to forward the email to a SMTP server that you can send email from.
例如,对于Gmail,请按照
http://rtcamp.com/tutorials/linux/ ubuntu-postfix-gmail-smtp /
或任何其他类似的教程。
For gmail, for example, follow http://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/ or any other similar tutorial.
这篇关于从linux终端发送邮件一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!