Mailx是一个智能邮件处理系统,提供POSIX mailx命令功能,提供MIME、IMAP、POP3、SMTP和S/MIME扩展,通过调用sendmail来发送邮件。

安装mailx:

# yum install mailx

mailx语法

mailx [-BDdEFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops] [-A account] [-S variable[=value]] to-addr . . .
mailx [-BDdeEHiInNRv~] [-T name] [-A account] [-S variable[=value]] -f [name]
mailx [-BDdeEinNRv~] [-A account] [-S variable[=value]] [-u user]

发送邮件

Options

选项 说明
-s subject 主题
-a attachment 附件
-c cc-addr 抄送地址
-b bcc-addr 密送地址
-r from-addr 发件人地址
-h hops 用指定的hop count调用sendmail(hopcount_limit),当使用SMTP时无效
-A account 执行一account命令
to-addr 收件人地址

说明:
hop是源到目标路径中的一个部分,数据包通过路由器、网关等传输,每经过的一个网络设备即为一个hop。
利用mailx和Office365 SMTP发送邮件_第1张图片

发送本地邮件

$ echo "Hello COCO" | mail -s "Hello COCO" ec2-user@localhost

以上命令向本地用户ec2-user发送了一个邮件,邮件保存在文件/var/mail/ec2-user内,内容如下:

From [email protected]  Wed Nov  7 09:44:20 2018
Return-Path: 
X-Original-To: ec2-user@localhost
Delivered-To: [email protected]
Received: by test.itrunner.org (Postfix, from userid 1000)
        id 7C28999A79; Wed,  7 Nov 2018 09:44:20 +0000 (UTC)
Date: Wed, 07 Nov 2018 09:44:20 +0000
To: [email protected]
Subject: Hello COCO
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <[email protected]>
From: [email protected] (Cloud User)

Hello COCO

从文件读取内容发送:

$ mail -s "Hello COCO" ec2-user@localhost < hello.txt
或
$ cat hello.txt | mail -s "Hello COCO" ec2-user@localhost

向多人发送:

$ mail -s "Hello COCO" [email protected],[email protected] < hello.txt

发送附件:

$ echo "Hello COCO" | mail -s "Hello COCO" -a hello.txt [email protected]

指定Reply To:

$ echo "Hello COCO" | mail -s "Hello COCO" -S replyto="Jason"  [email protected]

读取邮件

Options

选项 说明
-T name 文件名,将邮件信息中'Message-Id' 和 'Article-Id'写入文件
-f [name] 从指定文件读取邮件

执行不带任何参数的mailx命令,将读取当前用户的邮件,输入quit退出。输出内容如下:

Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/ec2-user": 1 message
>   1 Cloud User            Thu Nov  8 05:41  32/1017  "Hello COCO"
&

从文件读取邮件并将Message-Id写入文件

$ mailx -T test -f /var/mail/ec2-user

使用Office365 SMTP发送邮件

SMTP Options

使用外部SMTP服务器时,需用-S设定SMTP参数:

$ echo "Hello COCO" | mail -r "[email protected]" -s "Hello COCO" -S smtp="smtp.office365.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="password" -S ssl-verify=ignore -S nss-config-dir=~/.mozilla/firefox/default.clm [email protected]
选项 说明
smtp 指定SMTP地址(host or ip:port)
smtp-use-starttls 使用TLS加密来保证通讯安全
smtp-auth 设置SMTP验证方法,可选值: login、cram-md5、plain
smtp-auth-user SMTP验证用户名
smtp-auth-password SMTP密码
ssl-verify SSL证书验证出错时的处理方式,可选值:strict、ask、warn、ignore
nss-config-dir 包含文件certN.db、keyN.db、secmod.db的目录

nss-config-dir: 从certN.db中检索证书, 从keyN.db中检索私钥,N为数字。系统如安装了Firefox,其profile中包含这些文件。

运行如上命令可以发送邮件,但有错误:Error in certificate: Peer's certificate issuer is not recognized。原因是未导入office365的证书。上例nss-config-dir配置的是firefox profile目录,虽然mailx仅读取其内容不会修改,但若mailx正在运行时其内容被firefox修改时,会导致错误。因此实际应用中,需将certN.db、keyN.db、secmod.db复制到新的目录,或使用certutil创建这些文件。

certutil

certutil是证书和密钥管理工具
语法:

certutil [options] [[arguments]]
Options
选项 说明
-A 添加证书
-D 删除证书
-F 删除私钥
-G 生成密钥对
-L 列出所有证书
-M 修改certificate的trust属性,需使用-t参数
-N 创建证书和密钥数据库
-V 检查证书

Arguments
-d 包含证书和密钥数据库文件的目录
-n nickname,指定证书或密钥的nickname
-t trust参数

创建证书和密钥数据库

$ mkdir ~/.certs
$ certutil -N -d ~/.certs
Enter a password which will be used to encrypt your keys.
The password should be at least 8 characters long,
and should contain at least one non-alphabetic character.

Enter new password:
Re-enter password:

获取office 365证书

$ echo -n | openssl s_client -starttls smtp -showcerts -crlf -connect smtp.office365.com:587 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/office365.crt

运行以上命令会得到两个证书,修改一下只保留第二个CA证书,然后将CA证书导入:

$ certutil -A -n "DigiCert Global Root CA" -t "C,P,C" -d ~/.certs -i ~/.certs/office365.crt

说明:
-t trust参数,对于每个证书有三类trust参数,顺序是:SSL、email、object signing,参数可以使用none、any或以下属性代码:

  • p - Valid peer
  • P - Trusted peer (implies p)
  • c - Valid CA
  • C - Trusted CA (implies c)
  • T - trusted CA for client authentication (ssl server only)

检查证书:

$ certutil -L -d ~/.certs

再次执行如下命令将完全正常的发送邮件:

$ echo "Hello COCO" | mail -r "[email protected]" -s "Hello COCO" -S smtp="smtp.office365.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="password" -S ssl-verify=ignore -S nss-config-dir=~/.certs [email protected]

配置mail.rc

如发送邮件时每次都配置SMTP参数,比较繁琐,可以在文件/etc/mail.rc设置默认值,在其末尾添加如下内容:

set [email protected]
set smtp=smtp.office365.com:587
set smtp-auth=login
set [email protected]
set smtp-auth-password=password
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=~/.certs

再次运行如下命令测试:

$ echo "Hello COCO" | mail  -s "Hello COCO" [email protected]

Java调用mailx

package org.itrunner.tests.utils;

public class MailSender {
    private MailSender() {
    }

    public static void send(MailInfo mailInfo) throws Exception {
        ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", getCommand(mailInfo));
        Process process = processBuilder.start();
        process.waitFor();
    }

    private static String getCommand(MailInfo mail) {
        String sendEmailCommand = "echo \"" + mail.getMessage() + "\" | mail";

        if (mail.getCc() != null) {
            sendEmailCommand += " -c" + mail.getCc();
        }

        if (mail.getBcc() != null) {
            sendEmailCommand += " -b" + mail.getBcc();
        }

        sendEmailCommand += " -s \"" + mail.getSubject() + "\" " + mail.getTo();
        return sendEmailCommand;
    }
}

参考文档

Linux mailx command
Linux sendmail command
certutil - Linux Man Pages
9 mail/mailx command examples to send emails from command line on Linux