第一步:实现网页截屏
1. 下载phantomjs,用于实现网页的截屏。
我这里提供百度云的下载地址,包括linux32位以及64位,macosx和windows版本。
链接:https://pan.baidu.com/s/1rkgT8NgbRDN5_lNJlnIhMg 密码:3afb
2.解压文件编写java测试类
package com.jq;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class RobotExp6 {
public static void main(String[] args) throws IOException {
String s=getPictureByURL("http://www.iconfont.cn/plus","1");
String s2=getPictureByURL("https://www.baidu.com/","2");
List<String> list = new ArrayList<String>();
list.add(s);
list.add(s2);
for(int i = 0 ; i < list.size() ; i++) {
System.out.println(list.get(i));
}
}
public static String getPictureByURL(String url,String image){
String imageUrl="/Users/用户名/Desktop/"+image+".png";//生成图片的路径位置
String BLANK = " ";
Process process = null;
try {
process = Runtime.getRuntime().exec(
"/Users/用户名/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs" + BLANK //你的phantomjs路径
+ "/Users/用户名/Desktop/screenshot.js" + BLANK //就是上文中那段javascript脚本的存放路径,在这个js里面可以修改抓取网页的大小
+ url.trim() + BLANK //你的目标url地址
+ imageUrl);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String tmp = "";
try {
while ((tmp = reader.readLine()) != null) {
if (reader != null) {
reader.close();
}
if (process != null) {
process.destroy();
process = null;
}
System.out.println("图片生成成功...");
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imageUrl;
}
}
第二步:实现邮件的发送
下载activation.jar mail.jar
链接:https://pan.baidu.com/s/1WcnyPbH7fyhOlP-SdJwssg 密码:yo2i
package com.util;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
//阿里企业邮箱进行发送邮件
public class SendEmail6
{
public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
{
Properties prop=new Properties();
prop.put("mail.host","smtp.~.com" );//设置邮件服务器主机名
prop.put("mail.transport.protocol", "smtp");//发送邮件协议名称
prop.put("mail.smtp.auth", "true");//发送服务器需要身份验证
Session session=Session.getInstance(prop);
session.setDebug(true);//开启debug调试
//连接邮件服务器
Transport ts=session.getTransport();
ts.connect("****@~.com", "密码"); //登录的账户名和密码
Message msg=createSimpleMail(session);//创建邮件对象
ts.sendMessage(msg, msg.getAllRecipients());
}
}
public static MimeMessage createSimpleMail(Session session) throws AddressException,MessagingException, UnsupportedEncodingException{
MimeMessage mm=new MimeMessage(session);
mm.setFrom(new InternetAddress("邮箱")); //设置发件人
mm.setRecipient(Message.RecipientType.TO, new InternetAddress("邮箱地址")); //设置收件人
mm.setRecipient(Message.RecipientType.CC, new InternetAddress("抄送人邮箱地址")); //设置抄送人
mm.setSubject("主题!");
// 设置正文
BodyPart bp = new MimeBodyPart();
String mailBody = "请查收";
Multipart mp = new MimeMultipart(); ; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
bp.setContent(mailBody, "text/html;charset=utf-8");
mp.addBodyPart(bp);
//设置图片生成的路径
List<String> list = new ArrayList<String>();
list.add("/Users/~/Desktop/1.png");
list.add("/Users/·/Desktop/2.png");
for(int i = 0 ; i < list.size() ; i++) {
System.out.println(list.get(i));
}
// 设置附件
if (list != null && list.size() > 0) {
for(int i = 0 ; i < list.size() ; i++) {
bp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(list.get(i));
bp.setDataHandler(new DataHandler(fds));
bp.setFileName(MimeUtility.encodeText(fds.getName(), "UTF-8", "B"));
mp.addBodyPart(bp);
}
}
mm.setContent(mp);
mm.saveChanges();
return mm;
}
}