在这里分享下服务器如何利用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
本文出自 “xiangcun168” 博客,请务必保留此出处http://xiangcun168.blog.51cto.com/4788340/1692765
原文地址:http://xiangcun168.blog.51cto.com/4788340/1692765