Linux下利用mail命令发送邮件


前言

   mail命令实际是调用sendmail程序包,可以使用sendmail的配置参数; 终端下利用mail发送邮件可以实现报警脚本并达到实时监控预警的功能。

一、环境

Linux下Centos 7

[root@localhost /]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@localhost /]# uname -a
Linux localhost 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux


二、方法

1.安装mail程序

已安装mail程序
[root@localhost /]# rpm -qa | grep mail
mailx-12.5-19.el7.x86_64
libreport-plugin-mailx-2.1.11-53.el7.centos.x86_64
mailcap-2.1.41-2.el7.noarch
未安装mail程序
[root@localhost /]# yum -y install mailx

2.关闭其他邮件工具

[root@localhost /]# systemctl stop postfix
[root@localhost /]# systemctl stop sendmail

3.开启SMTP并获得授权码

在任何邮箱中开启对应账号的STMP、POP3、IMAP协议,通过邮箱绑定的手机号发送指定信息给官方,之后会获取到随机的授权码,该授权码用于配置文件/etc/mail.rc中的set smtp-auth-password=xxxx #授权码.

4.获取数字证书

发送邮箱用的是163邮箱,接收邮箱用的是qq邮箱,这里只需获取发送邮箱的数字证书即可.

[root@localhost /]# echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /root/.certs/163.crt
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA
verify return:1
depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = GeoTrust CN RSA CA G1
verify return:1
depth=0 C = CN, ST = Zhejiang, L = Hangzhou, O = "NetEase (Hangzhou) Network Co., Ltd", OU = IT Dept., CN = *.163.com
verify return:1
DONE

[root@localhost /]#  certutil -A -n "GeoTrust SSL CA" -t "C,," -d /root/.certs -i /root/.certs/163.crt
[root@localhost /]#  certutil -A -n "GeoTrust Global CA" -t "C,," -d /root/.certs -i /root/.certs/163.crt
[root@localhost /]#  certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d /root/.certs/./ -i /root/.certs/163.crt
Notice: Trust flag u is set automatically if the private key is present.

[root@localhost /]#  ls /root/.certs/
163.crt  cert8.db  key3.db  secmod.db
[root@localhost /]#  certutil -L -d /root/.certs

Certificate Nickname            Trust Attributes
                                SSL,S/MIME,JAR/XPI
                                
GeoTrust SSL CA                                P,P,P


5.配置/etc/mail.rc

[root@localhost /]# vim /etc/mail.rc
...在配置文件末尾追加如下内容:
set from=xxx1@163.com  #你的邮箱账号
set smtp=smtps://smtp.163.com
set smtp-auth-user=xxx@163.com
set smtp-auth-password=***  #上面获取的授权码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs  #数字证书目录

6.发送邮件方式

(1)直接使用shell当编辑器

[root@localhost /]# mail xxx@qq.com
Subject: test
123456
.
EOT

(2)管道

[root@localhost /]# echo "hello,liuyan.this is your e-mail"|mail -s "TEST" xxx@qq.com

(3) 文件

[root@localhost /]# mail -s "TEST2" xxx@qq.com < mail.txt

(4) 附件

[root@localhost /]# yum install sharutils
[root@localhost /]# uuencode mail.txt mail |mail -s "TEST3" xxx@qq.com 


三、 异常处理

1、端口处理未打开TCP/UDP的53端口

[root@localhost /]# firewall-cmd --add-port=53/tcp --permanent
success
[root@localhost /]# firewall-cmd --add-port=53/udp --permanent
success

2、网络ping不通,出现IP地址能ping通但域名ping不通,就是因为1、出现的问题




  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
收发邮件需要使用JavaMail API,以下是在Linux利用JavaMail收发邮件的步骤: 1.下载JavaMail API,这里建议使用最新的JavaMail 1.6.2版本,下载地址为:https://javaee.github.io/javamail/,下载后解压到任意目录。 2.在Linux系统上安装SMTP和POP3服务,这里以Postfix和Dovecot为例,安装命令为: ``` sudo apt-get install postfix dovecot-core dovecot-imapd dovecot-pop3d ``` 3.配置Postfix和Dovecot,具体配置步骤可以参考相关文档。 4.编写Java程序,实现邮件收发功能,以下是一个简单的JavaMail收发邮件示例代码: ``` import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class MailClient { public static void main(String[] args) throws Exception { String host = "localhost"; String user = "your_username"; String password = "your_password"; // 创建Properties对象,设置邮件服务器的相关信息 Properties props = new Properties(); props.setProperty("mail.smtp.host", host); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.port", "587"); props.setProperty("mail.smtp.starttls.enable", "true"); // 创建Session对象,用于和邮件服务器交互 Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }); // 创建MimeMessage对象,设置邮件的相关信息 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("from@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@example.com")); message.setSubject("Test Email"); message.setText("Hello World!"); // 发送邮件 Transport.send(message); // 接收邮件 Store store = session.getStore("pop3"); store.connect(host, user, password); Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); for (Message msg : messages) { System.out.println("Subject: " + msg.getSubject()); System.out.println("From: " + msg.getFrom()[0]); System.out.println("Content: " + msg.getContent().toString()); } inbox.close(false); store.close(); } } ``` 在上述代码中,需要把`host`、`user`和`password`替换成实际的邮件服务器、用户名和密码。 5.编译并运行程序,如果一切正常,就可以在控制台上看到发送的邮件和接收到的邮件的相关信息了。 注意:在Linux系统中,如果要使用SSL加密连接邮件服务器,需要在Java程序中添加相关的配置信息。具体配置方法可以参考JavaMail API的文档和示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只鱼曼巴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值