packagecom.star.networktest;import java.io.*;importjava.net.HttpURLConnection;importjava.net.URL;importjava.text.SimpleDateFormat;import java.util.*;importjavax.mail.BodyPart;importjavax.mail.Message;importjavax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;import javax.mail.internet.*;import javax.activation.*;public classAccess {private String host = ""; //smtp服务器
private String from = ""; //发件人地址
private String to = ""; //收件人地址
private String affix = ""; //附件地址
private String affixName = ""; //附件名称
private String user = ""; //用户名
private String pwd = ""; //密码
private String subject = ""; //邮件标题
public static void main(String[] args) throwsIOException {
Thread t=newThread() {public voidrun() {try{
Timer();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
} };
t.start();
}public static void printlog(String msg) throwsIOException {
Date date= newDate();
File f= new File("F:/java/workspace/log/log.txt");
FileOutputStream fop= new FileOutputStream(f,true);//构建FileOutputStream对象,文件不存在会自动新建
OutputStreamWriter writer= new OutputStreamWriter(fop, "UTF-8");//构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
writer.append(date.toString()+"\t");
writer.append(msg);//写入到缓冲区
writer.append("\r\n");//换行//writer.append("English");//刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入
writer.close();//关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
fop.close();//关闭输出流,释放系统资源
FileInputStream fip= newFileInputStream(f);//构建FileInputStream对象
InputStreamReader reader= new InputStreamReader(fip, "UTF-8");//构建InputStreamReader对象,编码与写入相同
StringBuffer sb= newStringBuffer();while(reader.ready()) {
sb.append((char) reader.read());//转成char加到StringBuffer对象中
}
System.out.println(sb.toString());
reader.close();//关闭读取流
fip.close();//关闭输入流,释放系统资源
}public voidsetAddress(String from, String to, String subject) {this.from =from;this.to =to;this.subject =subject;
}public voidsetAffix(String affix, String affixName) {this.affix =affix;this.affixName =affixName;
}public voidsend(String host, String user, String pwd) {this.host =host;this.user =user;this.pwd =pwd;
Properties props= newProperties();//设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
props.put("mail.smtp.host", host);//需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
props.put("mail.smtp.auth", "true");//用刚刚设置好的props对象构建一个session
Session session =Session.getDefaultInstance(props);//有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使//用(你可以在控制台(console)上看到发送邮件的过程)
session.setDebug(true);//用session为参数定义消息对象
MimeMessage message = newMimeMessage(session);try{//加载发件人地址
message.setFrom(newInternetAddress(from));//加载收件人地址
message.addRecipient(Message.RecipientType.TO, newInternetAddress(to));//加载标题
message.setSubject(subject);//向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = newMimeMultipart();//设置邮件的文本内容
BodyPart contentPart = newMimeBodyPart();
contentPart.setText("当前网址https://idp.tjut.edu.cn/idp/shibboleth 不能访问,请尽快修复");
multipart.addBodyPart(contentPart);//添加附件
BodyPart messageBodyPart = newMimeBodyPart();
DataSource source= newFileDataSource(affix);//添加附件的内容
messageBodyPart.setDataHandler(newDataHandler(source));//添加附件的标题//这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码//报错参考https://jingyan.baidu.com/article/e73e26c0a2617a24adb6a7d4.html
sun.misc.BASE64Encoder enc = newsun.misc.BASE64Encoder();
messageBodyPart.setFileName("=?GBK?B?"
+ enc.encode(affixName.getBytes()) + "?=");
multipart.addBodyPart(messageBodyPart);//将multipart对象放到message中
message.setContent(multipart);//保存邮件
message.saveChanges();//发送邮件
Transport transport = session.getTransport("smtp");//连接服务器的邮箱
transport.connect(host, user, pwd);//把邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch(Exception e) {
e.printStackTrace();
}
}//实现定时器
public static void Timer() throwsIOException {while(true) {try{
Thread.sleep(59000);
SimpleDateFormat d=new SimpleDateFormat("mm");//if(d.format(new Date()).equals("01")) {
if(d.format(new Date()).equals("24")) {
StringBuffer error= newStringBuffer();long begintime =System.currentTimeMillis();try{//PKIX path building failed 自己的Java环境没有相关网站的证书 去网站下载证书导入到自己的java环境
URL url = new URL("https://idp.tjut.edu.cn/idp/shibboleth");
HttpURLConnection urlcon=(HttpURLConnection) url.openConnection();
urlcon.connect();//获取连接
InputStream is=urlcon.getInputStream();
System.out.println(urlcon.getResponseCode());//获取连接状态码
System.out.println(urlcon.getResponseMessage()); //获取HTTP响应消息
printlog("访问正常");
}catch(IOException e) {
System.out.println(e);
Access cn= newAccess();//设置发件人地址、收件人地址和邮件标题
cn.setAddress("发送者@tom.com", "接受者1@qq.com", "网络连通状态检测报告");//设置要发送附件的位置和标题
cn.setAffix("F:/java/workspace/log/log.txt", "log.txt");
cn.send("smtp.tom.com", "发送者@tom.com", "****");
cn.setAddress("starzhai@tom.com", "接收者2@qq.com", "网络连通状态检测报告");//设置要发送附件的位置和标题
cn.setAffix("F:/java/workspace/log/log.txt", "log.txt");
cn.send("smtp.tom.com", "发送者@tom.com", "******");
printlog("访问异常,已将报告发送至邮箱");
}
}
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}