代码不精,封装无力。。邮件方面的知识不懂,核心原理照着这位大佬的博客敲的,目前只能发给一个邮箱,只能发文本(html也成),密码会暴露在控制台下。下次更新待修改。
下载器类
package just4test2;
import java.io.*;
import java.net.*;
import org.jsoup.*;
import org.jsoup.nodes.*;
import org.jsoup.select.*;
public class Downloader {
private String base;
public Downloader(String abase) {
base=abase;
}
public void download(String src,String path) throws Exception {
File fp = new File (path);
if (!fp.exists())
fp.mkdirs();
int pos = src.lastIndexOf('/');
String filename = src.substring(pos);
URL url = new URL(src);
InputStream cin = url.openStream();//开启连接,同时返回输入流对象
OutputStream cout = new BufferedOutputStream(new FileOutputStream (path+filename));//得到文件输出流对象
byte[] buffer = cin.readAllBytes();//将文件写入缓冲数组
cout.write(buffer);//写到文件中
cin.close();
cout.close();
}
public void downPic(String path) throws Exception{
//用jsoup获取连接,设置超时防止卡死,模拟浏览器为Chrome
Connection con = Jsoup.connect(base).timeout(40000).userAgent("Chrome");
Document doc = con.get();//得到document对象
// 查找所有img标签
Elements imgs = doc.getElementsByTag("img");//根据img标签抓元素,得到一个element元素集elements,很形象
int j=1;
for (Element x:imgs) {
String imgSrc = x.attr("abs:src");//获取src属性的绝对路径
imgSrc = imgSrc.replaceAll("\\s","");//把空白符替换掉
System.out.printf("正在下载第%d个文件",j++);
System.out.print(",地址:");
System.out.println(imgSrc);
download(imgSrc,path);
}
}
public String getPicURL() throws Exception{
String res = "";
Connection con = Jsoup.connect(base);//用jsoup获取连接
Document doc = con.get();//发送get请求,得到响应,将该响应解析得到document对象
// 查找所有img标签
Elements imgs = doc.getElementsByTag("img");//根据img标签抓元素,得到一个element元素集elements,很形象
for (Element x:imgs) {
String imgSrc = x.attr("abs:src");//获取绝对路径
imgSrc = imgSrc.replaceAll("\\s","");//把空白符替换掉
res += ("<img src=\""+imgSrc+ "\"" + "><br><br><br><br>");//手动添加html标签
}
return res;
}
public static void main(String[] args)throws Exception {
Connection con = Jsoup.connect("https://www.baidu.com/s?wd=emm&pn=10&oq=emm&tn=baiduhome_pg&ie=utf-8&usm=1&rsv_idx=2&rsv_pq=d9987a9100019e58&rsv_t=6182mCdG68LYFKW8uOWnNZtMAxJI2cuJx1Jf1KYoBLNHJq9pOBobv%2FRW3FNBsPfXmgMc").timeout(40000);
//con.data("page","2");
Document doc = con.get();
System.out.println(doc);
}
}
邮件发送器类
package just4test3;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/*
* 简易邮件发送器,只能发字符串,只能发给一个人(但可以用html标签,所以理论上可以全部实现正常邮件应有的功能)
*/
public class EzSender {
private Session session;//会话对象
private String name,pwd,sub,body,mynick="Simon",thonick="MyBF";//基本属性
public EzSender (String aname,String smtp,String apwd) throws Exception {
name = aname;
pwd = apwd;//密码是可以看到的,下一步需要修改
// 1. 创建参数配置, 用于连接邮件服务器的参数配置
Properties props = new Properties(); // 参数配置
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", smtp); // 发件人的邮箱的 SMTP 服务器地址
props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
session = Session.getInstance(props);
session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log
}
public void setContent(String asub,String abody) {
sub = asub;body = abody;
}
public void setNick(String amy,String atho) {
mynick = amy;
thonick = atho;
}
public void sendTo(String to) throws Exception {
//创建邮件
MimeMessage message = new MailCreater(session,name,to,sub,body,mynick,thonick).getMessage();
//根据Session获取文件传输对象
Transport transport = session.getTransport();
//开启连接!
transport.connect(name,pwd);
//获取所有收件人,发送邮件!
transport.sendMessage(message, message.getAllRecipients());
//关闭连接
transport.close();
}
}
邮件创建器类
package just4test3;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
public class MailCreater {
private MimeMessage message;
public MailCreater (Session session,String from,String to,String sub,String body,String mynick,String thonick)
throws Exception {
message = new MimeMessage(session); // 创建邮件对象
//其中 InternetAddress 的三个参数分别为: 邮箱, 显示的昵称(只用于显示, 没有特别的要求), 昵称的字符集编码可省略
message.setFrom(new InternetAddress(from,mynick));
//收件人
message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(to,thonick));
message.setSubject(sub);//主题
message.setContent(body, "text/html;charset=UTF-8");//正文,编码
message.setSentDate(new Date());//日期,直接new一个返回的就是今天的date对象
message.saveChanges();//保存文件
}
/*
* 存放到本地,默认保存到项目目录下
*/
public void saveMail() throws Exception{
OutputStream cout = new FileOutputStream("MyEmail.eml");
message.writeTo(cout);
cout.flush();
cout.close();
}
public MimeMessage getMessage() {
return message;
}
}
主类
package just4test3;
import java.util.*;
import just4test2.*;
public class Main {
public static void main(String[] args){
try{
Scanner cin = new Scanner (System.in);
System.out.println("输入用户名,smtp服务器地址,密码");
String name = cin.next(),smtp = cin.next(),pwd = cin.next();
EzSender ez = new EzSender(name,smtp,pwd);
System.out.println("输入发件人和收件人昵称,输入0 0为默认昵称");
String nick1 = cin.next(),nick2 = cin.next();
if (!nick1.equals("0"))
ez.setNick(nick1,nick2);
System.out.println("输入主题");
String sub = cin.next();
System.out.println("输入要爬取的网页");
String url = cin.next();
Downloader down = new Downloader(url);
String res = down.getPicURL();
ez.setContent(sub,res);
System.out.println("输入收件人地址");
ez.sendTo(cin.next());
System.out.println("发送成功!");
cin.close();
}catch (Exception e) {
e.printStackTrace();
System.out.println("GG!");
}
}
}
以上,期待下次更新