- 浏览: 1811 次
- 性别:
- 来自: 上海
文章分类
最新评论
java 发送邮件
package test;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class FinalTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String filePath = "src//ArticleRedirects.xls";
Workbook book;
try {
book = Workbook.getWorkbook(new File(filePath));
//get a Sheet object.
for (int sheeti= 0;sheeti<book.getNumberOfSheets();sheeti++){
Sheet sheet = book.getSheet(sheeti);
WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
//set background color;
cellFormat1.setBackground(Colour.LIGHT_TURQUOISE);
//set the border;
cellFormat1.setBorder(Border.ALL, BorderLineStyle.THIN);
//set wrap automatically;
cellFormat1.setWrap(true);
//set the text align center;
cellFormat1.setAlignment(Alignment.CENTRE);
//set the text align center vertically;
cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
System.out.println(sheet.getName());
List<String> redirectLink301 = new ArrayList<String>();
List<String> baseLink301 = new ArrayList<String>();
List<String> notFoundLinks = new ArrayList<String>();
List<String> movedLink = new ArrayList<String>();
List<String> otherStatusLink = new ArrayList<String>();
for(int k =0;k<sheet.getRows();k++){
Cell cell = sheet.getCell(0, k);
String baseLink = cell.getContents();
if(baseLink.trim()!=""){
URL url = new URL(baseLink);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
connection.setInstanceFollowRedirects( false );
int code = connection.getResponseCode();
if(code==301){
URL url2 = new URL(baseLink);
HttpURLConnection connection2 = (HttpURLConnection)url2.openConnection();
connection2.setRequestMethod("GET");
connection2.connect();
InputStream is = connection2.getInputStream();
baseLink301.add(baseLink);
redirectLink301.add(connection2.getURL()+"");
System.out.println(code + ": redirect Link: "+baseLink);
is.close();
}else if(code==302){
movedLink.add(baseLink);
System.out.println(code+ ": Moved links: "+baseLink);
}else if(code==404){
notFoundLinks.add(baseLink);
System.out.println(code+ ": NotFound Link: "+baseLink);
}else{
otherStatusLink.add(baseLink);
System.out.println(code+ ": sucess or other Status Links: "+baseLink);
}
}
}
//create excel
WritableWorkbook wwb = null;
wwb = Workbook.createWorkbook(new File("src//testResults//resultsOf"+sheet.getName()+".xls"));
WritableSheet movedws = wwb.createSheet("302 Moved links", 0);
Label labelA0 = new Label(0, 0, "HERE ARE THE MOVED LINKS",cellFormat1);//set the format of the first line,first column
movedws.addCell(labelA0);
movedws.getSettings().setDefaultColumnWidth(60);
for (int k =0;k<movedLink.size();k++){
Label labelA = new Label(0, k+2, movedLink.get(k));
try {
movedws.addCell(labelA);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
WritableSheet notFoundws = wwb.createSheet("404 not Found links", 0);
Label labelL0 = new Label(0, 0, "HERE ARE THE NOTFOUND LINKS",cellFormat1);//set the format of the first line,first column
notFoundws.addCell(labelL0);
notFoundws.getSettings().setDefaultColumnWidth(60);
for (int l =0;l<notFoundLinks.size();l++){
Label labelL = new Label(0, l+2, notFoundLinks.get(l));
try {
notFoundws.addCell(labelL);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
WritableSheet otherStatusws = wwb.createSheet("otherStatusLinks", 0);
Label labeln0 = new Label(0, 0, "HERE ARE OTHER STATUS LINKS LIKE 200,5XX",cellFormat1);//set the format of the first line,first column
otherStatusws.getSettings().setDefaultColumnWidth(60);
otherStatusws.addCell(labeln0);
for (int n =0;n<otherStatusLink.size();n++){
Label labeln = new Label(0, n+2, otherStatusLink.get(n));
try {
otherStatusws.addCell(labeln);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
WritableSheet redirect301ws = wwb.createSheet("redirect301Links", 1);
Label labelB10 = new Label(0, 0, "BASE URL: BEFORE REDIRECT",cellFormat1);//set the format of the first line,first column
redirect301ws.getSettings().setDefaultColumnWidth(60);
redirect301ws.addCell(labelB10);
for (int p =0;p<baseLink301.size();p++){
Label labelB1 = new Label(0, p+2, baseLink301.get(p));
try {
redirect301ws.addCell(labelB1);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
Label labelB0 = new Label(1, 0, "REDIRECED : AFTER REDIRECT",cellFormat1); //set the format of the first line,second column
redirect301ws.addCell(labelB0);
for (int m =0;m<redirectLink301.size();m++){
Label labelB = new Label(1, m+2, redirectLink301.get(m));
try {
redirect301ws.addCell(labelB);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
WritableSheet summary = wwb.createSheet("summary", 0);
summary.getSettings().setDefaultColumnWidth(60);
Label summaryLab = new Label(0, 0, "HERE ARE SUMMARIES OF " +sheet.getName(),cellFormat1);//set the format of the first line,first column
summary.addCell(summaryLab);
Label label301redirects = new Label(0, 2, "301redirect links:" + baseLink301.size());
Label labelNotFoundLinks = new Label(0, 3, "404 not Found links:" + notFoundLinks.size());
Label labelmovedLinks = new Label(0, 4, "302 Moved links:" + notFoundLinks.size());
Label otherStatusLinks = new Label(0, 5, "otherStatus links:" + otherStatusLink.size());
summary.addCell(label301redirects);
summary.addCell(labelNotFoundLinks);
summary.addCell(labelmovedLinks);
summary.addCell(otherStatusLinks);
wwb.write();
wwb.close();
}
System.out.println("================DONE====================");
send();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void send() throws Exception{
MultiPartEmail email = new MultiPartEmail();
email.setTLS(true);
email.setHostName("smtp.163.com");
email.setAuthentication("zhoulihonggood", "16307932923148");
EmailAttachment attachment = new EmailAttachment();
CompressBook cbook = new CompressBook();
cbook.zip("E:\\workspace\\verifyMMLinks\\src\\testResults");
attachment.setPath("c:\\test.zip");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("result");
attachment.setName("test.zip");
email.addTo("szhou@defymedia.com");
email.addCc("lzhang@defymedia.com");
email.addCc("szhou@defymedia.com");
email.addTo("cwong@defymedia.com");
email.addCc("ywang@defymedia.com");
email.addCc("MAlvarino@defymedia.com");
email.setFrom("zhoulihonggood@163.com");
email.setSubject("TestResults");
email.setMsg("hi Calvin, I have attached the tested results");
email.attach(attachment);
email.send();
}
}
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class FinalTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String filePath = "src//ArticleRedirects.xls";
Workbook book;
try {
book = Workbook.getWorkbook(new File(filePath));
//get a Sheet object.
for (int sheeti= 0;sheeti<book.getNumberOfSheets();sheeti++){
Sheet sheet = book.getSheet(sheeti);
WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
//set background color;
cellFormat1.setBackground(Colour.LIGHT_TURQUOISE);
//set the border;
cellFormat1.setBorder(Border.ALL, BorderLineStyle.THIN);
//set wrap automatically;
cellFormat1.setWrap(true);
//set the text align center;
cellFormat1.setAlignment(Alignment.CENTRE);
//set the text align center vertically;
cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
System.out.println(sheet.getName());
List<String> redirectLink301 = new ArrayList<String>();
List<String> baseLink301 = new ArrayList<String>();
List<String> notFoundLinks = new ArrayList<String>();
List<String> movedLink = new ArrayList<String>();
List<String> otherStatusLink = new ArrayList<String>();
for(int k =0;k<sheet.getRows();k++){
Cell cell = sheet.getCell(0, k);
String baseLink = cell.getContents();
if(baseLink.trim()!=""){
URL url = new URL(baseLink);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
connection.setInstanceFollowRedirects( false );
int code = connection.getResponseCode();
if(code==301){
URL url2 = new URL(baseLink);
HttpURLConnection connection2 = (HttpURLConnection)url2.openConnection();
connection2.setRequestMethod("GET");
connection2.connect();
InputStream is = connection2.getInputStream();
baseLink301.add(baseLink);
redirectLink301.add(connection2.getURL()+"");
System.out.println(code + ": redirect Link: "+baseLink);
is.close();
}else if(code==302){
movedLink.add(baseLink);
System.out.println(code+ ": Moved links: "+baseLink);
}else if(code==404){
notFoundLinks.add(baseLink);
System.out.println(code+ ": NotFound Link: "+baseLink);
}else{
otherStatusLink.add(baseLink);
System.out.println(code+ ": sucess or other Status Links: "+baseLink);
}
}
}
//create excel
WritableWorkbook wwb = null;
wwb = Workbook.createWorkbook(new File("src//testResults//resultsOf"+sheet.getName()+".xls"));
WritableSheet movedws = wwb.createSheet("302 Moved links", 0);
Label labelA0 = new Label(0, 0, "HERE ARE THE MOVED LINKS",cellFormat1);//set the format of the first line,first column
movedws.addCell(labelA0);
movedws.getSettings().setDefaultColumnWidth(60);
for (int k =0;k<movedLink.size();k++){
Label labelA = new Label(0, k+2, movedLink.get(k));
try {
movedws.addCell(labelA);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
WritableSheet notFoundws = wwb.createSheet("404 not Found links", 0);
Label labelL0 = new Label(0, 0, "HERE ARE THE NOTFOUND LINKS",cellFormat1);//set the format of the first line,first column
notFoundws.addCell(labelL0);
notFoundws.getSettings().setDefaultColumnWidth(60);
for (int l =0;l<notFoundLinks.size();l++){
Label labelL = new Label(0, l+2, notFoundLinks.get(l));
try {
notFoundws.addCell(labelL);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
WritableSheet otherStatusws = wwb.createSheet("otherStatusLinks", 0);
Label labeln0 = new Label(0, 0, "HERE ARE OTHER STATUS LINKS LIKE 200,5XX",cellFormat1);//set the format of the first line,first column
otherStatusws.getSettings().setDefaultColumnWidth(60);
otherStatusws.addCell(labeln0);
for (int n =0;n<otherStatusLink.size();n++){
Label labeln = new Label(0, n+2, otherStatusLink.get(n));
try {
otherStatusws.addCell(labeln);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
WritableSheet redirect301ws = wwb.createSheet("redirect301Links", 1);
Label labelB10 = new Label(0, 0, "BASE URL: BEFORE REDIRECT",cellFormat1);//set the format of the first line,first column
redirect301ws.getSettings().setDefaultColumnWidth(60);
redirect301ws.addCell(labelB10);
for (int p =0;p<baseLink301.size();p++){
Label labelB1 = new Label(0, p+2, baseLink301.get(p));
try {
redirect301ws.addCell(labelB1);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
Label labelB0 = new Label(1, 0, "REDIRECED : AFTER REDIRECT",cellFormat1); //set the format of the first line,second column
redirect301ws.addCell(labelB0);
for (int m =0;m<redirectLink301.size();m++){
Label labelB = new Label(1, m+2, redirectLink301.get(m));
try {
redirect301ws.addCell(labelB);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
WritableSheet summary = wwb.createSheet("summary", 0);
summary.getSettings().setDefaultColumnWidth(60);
Label summaryLab = new Label(0, 0, "HERE ARE SUMMARIES OF " +sheet.getName(),cellFormat1);//set the format of the first line,first column
summary.addCell(summaryLab);
Label label301redirects = new Label(0, 2, "301redirect links:" + baseLink301.size());
Label labelNotFoundLinks = new Label(0, 3, "404 not Found links:" + notFoundLinks.size());
Label labelmovedLinks = new Label(0, 4, "302 Moved links:" + notFoundLinks.size());
Label otherStatusLinks = new Label(0, 5, "otherStatus links:" + otherStatusLink.size());
summary.addCell(label301redirects);
summary.addCell(labelNotFoundLinks);
summary.addCell(labelmovedLinks);
summary.addCell(otherStatusLinks);
wwb.write();
wwb.close();
}
System.out.println("================DONE====================");
send();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void send() throws Exception{
MultiPartEmail email = new MultiPartEmail();
email.setTLS(true);
email.setHostName("smtp.163.com");
email.setAuthentication("zhoulihonggood", "16307932923148");
EmailAttachment attachment = new EmailAttachment();
CompressBook cbook = new CompressBook();
cbook.zip("E:\\workspace\\verifyMMLinks\\src\\testResults");
attachment.setPath("c:\\test.zip");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("result");
attachment.setName("test.zip");
email.addTo("szhou@defymedia.com");
email.addCc("lzhang@defymedia.com");
email.addCc("szhou@defymedia.com");
email.addTo("cwong@defymedia.com");
email.addCc("ywang@defymedia.com");
email.addCc("MAlvarino@defymedia.com");
email.setFrom("zhoulihonggood@163.com");
email.setSubject("TestResults");
email.setMsg("hi Calvin, I have attached the tested results");
email.attach(attachment);
email.send();
}
}
相关推荐
Java发送电子邮件是一个常见的任务,尤其在自动化通知、报表发送或者用户验证等场景中。下面将详细解释如何使用Java来发送邮件,并提供相关的知识点。 首先,Java通过JavaMail API提供了发送邮件的功能。JavaMail ...
java发送邮件工具类,用于java程序实现邮件的发送功能
本文将详细讲解如何使用Java发送邮件,并着重介绍消息格式化的概念及其重要性。 首先,发送邮件的核心类是JavaMail API提供的`javax.mail`包中的`Session`, `Message`, `Transport`等类。为了发送邮件,我们需要...
描述中提到的“java发送邮件所需要的包”即是指JavaMail API,它包含了一系列的类和接口,例如`javax.mail.Session`,它是邮件会话对象,用于配置邮件服务器的连接参数;`javax.mail.Transport`,用于发送邮件;`...
Java发送邮件功能是Java开发中常见的一项任务,它允许开发者通过编程的方式实现电子邮件的发送,包括正文内容、附件以及抄送等高级功能。在Java中,我们主要利用JavaMail API来实现这一目标。 JavaMail API是一个...
java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个...
Java发送邮件是Java编程中的一项常见任务,尤其在企业级应用和自动化通知系统中非常普遍。本项目基于Maven构建,但如果你不使用Maven,你需要手动添加以下三个关键的JAR包:`mail.jar`,`commons-email-X.X.jar`,...
Java发送邮件是一个常见的任务,尤其在自动化通知、报表发送或者用户验证等场景中。下面将详细解释这个过程,包括SMTP(Simple Mail Transfer Protocol)的工作原理、JavaMail API的使用以及可能遇到的问题及其解决...
总的来说,Java发送邮件涉及到了网络通信、邮件协议(如SMTP)、JavaMail API的使用,以及可能的认证机制。理解这些知识点对于实现一个稳定的邮件发送功能至关重要。在实际操作时,确保正确配置邮件服务信息,并对...
本文将详细讲解如何使用Java发送邮件,包括所需的库、基本概念以及实际示例代码。 首先,JavaMail API是Java平台用于处理邮件服务的核心库。要发送邮件,你需要在项目中引入JavaMail的依赖。如果你使用的是Maven,...
Java发送邮件是软件开发中常见的需求,特别是在自动化通知、用户验证和客户服务等方面。在这个主题下,我们将探讨三种Java实现邮件发送的方式,以及如何通过提供的代码和参考资料进行实践。 首先,让我们了解一下...
Java发送邮件是一个常见的任务,尤其在自动化通知、报表发送或者用户验证等场景中。要实现这个功能,开发者通常需要引入一些特定的库,也就是我们所说的jar包。在"java发送邮件所需jar包.zip"这个压缩文件中,很可能...
本示例“java发送邮件Demo”旨在教你如何利用JavaMail API实现这个功能。JavaMail API是一个开源库,允许开发者通过SMTP(简单邮件传输协议)发送、接收和管理邮件。 首先,确保在项目中引入JavaMail API的相关依赖...
JavaMail SDK是Java发送邮件的核心工具,通过配置Session,创建并设置Message,以及使用Transport发送邮件,开发者可以轻松地在Java应用中实现邮件功能。同时,JavaMail还提供了丰富的功能,如HTML邮件、附件、抄送...
Java发送邮件是一个常见的任务,尤其在自动化通知、订阅服务或者数据传输中。在这个"java发送邮件完整程序"中,我们将会探讨如何使用Java语言来构建一个邮件发送功能,包括所需的库、步骤以及关键代码片段。 首先,...
本示例主要关注如何使用Java发送邮件,并结合`Mail.jar`库来实现这一功能。`Mail.jar`通常包含了JavaMail API,这是一个用于处理邮件的开源库。 首先,我们需要在项目中引入JavaMail API的相关依赖。如果是在Maven...
在Java编程语言中,发送邮件是一项常见的任务,尤其在开发Web应用、自动化通知或系统集成时。...Java发送邮件的功能十分强大,可以根据需求进行各种定制。通过学习和实践,你可以轻松地在Java应用中实现邮件服务功能。
Java发送邮件功能主要依赖于两个库,即javax.mail.jar和activation.jar。这两个JAR文件包含了JavaMail API,它是Java平台中用于发送和接收电子邮件的标准API。让我们深入了解一下这两个库及其在Java邮件处理中的作用...
使用java发送邮件的方法,直接可以拿来用,最近找了个例子,稍微整理了一下