在这里分享下Linux服务器如何利用Java邮件报警
前提:服务器先安装配置好java环境
一、JAVA环境安装
[root@Server2 ~]# tar -zxvf jdk-7u75-linux-x64.gz -C /usr/local
[root@Server2 ~]# vi /etc/profile
export JAVA_HOME=/usr/local/jdk1.7.0_75
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
[root@Server2 ~]# source /etc/profile 快速生效
[root@Server2 ~]# java -version
java version "1.7.0_45"
OpenJDK Runtime Environment (rhel-2.4.3.3.el6-x86_64 u45-b15)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
二、脚本配置
上传附件中的脚本目录到/var/scripts后解压
修改tomail.sh
#!/bin/bash
export LANG=en_US.UTF-8
HOST="smtp.163.com"
SENDER="monitor@163.com"
PASSWORD="xxxxxxxx"
RECEIVER="youremail@yourdomain.com"
#报警内容
SUBJECT="request timeout,Please check"
CONTENT="this eamil from monitor server"
#发送邮件
/usr/local/jdk1.7.0_75/bin/java -cp /var/scripts/monitor:/var/scripts/lib/* SendMails $HOST $SENDER $PASSWORD "$SUBJECT" "$CONTENT" $RECEIVER
配置好的后,执行tomail.sh发送邮件就可以了。
如果报以下错误,请配置下hosts
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 501 Syntax: HELO hostname
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at MailUtil.send(MailUtil.java:44)
at SendMails.main(SendMails.java:36)
[root@Server2 ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.22 Server2
附件java to mail下载:
------------------------------------------分割线------------------------------------------
具体下载目录在 /2015年资料/9月/8日/Linux服务器报警之利用Java邮件报警/
------------------------------------------分割线------------------------------------------