package com.joyveb.export.action;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class JavaMail {
private String SMTPServer = new String();// 邮件服务器
private String user = new String();// 用户名
private String password = new String();// 密码
private String addresser = new String();// 发送者
private String addressTO = null;// 接收者
private String addressCC = null;// 抄送者
private String addressBCC = null;// 密秘抄送者
private String subject = new String();// 标题
private String content = new String();// 内容
private String filename = null; // 文件名
//一个附件时
private java.io.File fileAttachment = null;
// 当有多个附件时
private Vector<File> files = new Vector<File>();// 附件文件集合
private Vector<String> filenames = new Vector<String>();// 附件文件名
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
}
// 往附件组合中添加附件
public void attachfile(File file, String filename) {
files.addElement(file);
filenames.addElement(filename);
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, String addressCC) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.addressCC = addressCC;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, java.io.File fileAttachment, String filename) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filename = filename;
this.fileAttachment = fileAttachment;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, String addressCC, java.io.File fileAttachment,
String filename) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filename = filename;
this.fileAttachment = fileAttachment;
this.addressCC = addressCC;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, Vector<File> files, Vector<String> filenames) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filenames = filenames;
this.files = files;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, String addressCC, Vector<File> files,
Vector<String> filenames) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filenames = filenames;
this.files = files;
this.addressCC = addressCC;
}
// 发送邮件
public void sendMail() {
try {
Properties props = new Properties();
Session sendMailSession;
Transport transport;
PopupAuthenticator popAuthenticator = new PopupAuthenticator();
PasswordAuthentication pop = popAuthenticator.performCheck(user,
password);
props.put("mail.smtp.host", SMTPServer);
props.put("mail.smtp.auth", "true");
sendMailSession = Session.getInstance(props, popAuthenticator);
sendMailSession.setDebug(false);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(addresser));
newMessage.setSubject(subject, "GBK");
newMessage.setSentDate(new java.util.Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(content, "GBK");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
if (fileAttachment != null) {
messageBodyPart = new MimeBodyPart();
messageBodyPart.setFileName(filename);
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
multipart.addBodyPart(messageBodyPart);
}
// 添加多个附件
if (files.size() != 0) {
Enumeration efile = files.elements();
Enumeration names = filenames.elements();
while (efile.hasMoreElements() && names.hasMoreElements()) {
MimeBodyPart mbpFile = new MimeBodyPart();
String filestr = efile.nextElement().toString();
String namestr = names.nextElement().toString();
System.out.println(filestr);
System.out.println(namestr);
FileDataSource fds = new FileDataSource(filestr);
mbpFile.setDataHandler(new DataHandler(fds));
mbpFile.setFileName(namestr);
// 向MimeMessage添加(Multipart代表附件)
multipart.addBodyPart(mbpFile);
}
files.removeAllElements();
filenames.removeAllElements();
}
newMessage.setContent(multipart);
if (addressTO != null) {
newMessage.setRecipients(RecipientType.TO,
InternetAddress.parse(addressTO));
}
if (addressCC != null) {
newMessage.setRecipients(RecipientType.CC,
InternetAddress.parse(addressCC));
}
if (addressBCC != null) {
newMessage.setRecipients(RecipientType.BCC,
InternetAddress.parse(addressBCC));
}
transport = sendMailSession.getTransport("smtp");
transport.connect(SMTPServer, user, password);
transport.send(newMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
public class PopupAuthenticator extends Authenticator {
String username = null;
String password = null;
public PopupAuthenticator() {
}
public PasswordAuthentication performCheck(String user, String pass) {
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
public static void main(String[] args) throws UnsupportedEncodingException {
String smtpServer = "smtp.qq.com";
String user = "1025725439@qq.com";
String password = "**********";
String addresser = "1025725439@qq.com";
String subject = "你好,jone.";
String addressTO = "qwert1990817@126.com";
String addressBCC = "";
String addressCC = "";
String content = "Hello Greatjone!";
File file = new File("E:\\person.xls");
File file1 = new File("E:\\human.xls.xls");
Vector<File>files=new Vector<File>();
files.add(file);
files.add(file1);
Vector<String> filenames = new Vector<String>();
String filename = new String("person.xls".getBytes("gbk"), "iso-8859-1");
String filename1 = new String("human.xls.xls".getBytes("gbk"), "iso-8859-1");
filenames.add(filename);
filenames.add(filename1);
JavaMail mail = new JavaMail(smtpServer, user, password, addresser,
subject, content, addressTO, addressBCC, addressCC, files,
filenames);
mail.sendMail();
System.out.println("发送成功");
}
}
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class JavaMail {
private String SMTPServer = new String();// 邮件服务器
private String user = new String();// 用户名
private String password = new String();// 密码
private String addresser = new String();// 发送者
private String addressTO = null;// 接收者
private String addressCC = null;// 抄送者
private String addressBCC = null;// 密秘抄送者
private String subject = new String();// 标题
private String content = new String();// 内容
private String filename = null; // 文件名
//一个附件时
private java.io.File fileAttachment = null;
// 当有多个附件时
private Vector<File> files = new Vector<File>();// 附件文件集合
private Vector<String> filenames = new Vector<String>();// 附件文件名
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
}
// 往附件组合中添加附件
public void attachfile(File file, String filename) {
files.addElement(file);
filenames.addElement(filename);
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, String addressCC) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.addressCC = addressCC;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, java.io.File fileAttachment, String filename) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filename = filename;
this.fileAttachment = fileAttachment;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, String addressCC, java.io.File fileAttachment,
String filename) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filename = filename;
this.fileAttachment = fileAttachment;
this.addressCC = addressCC;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, Vector<File> files, Vector<String> filenames) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filenames = filenames;
this.files = files;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, String addressCC, Vector<File> files,
Vector<String> filenames) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filenames = filenames;
this.files = files;
this.addressCC = addressCC;
}
// 发送邮件
public void sendMail() {
try {
Properties props = new Properties();
Session sendMailSession;
Transport transport;
PopupAuthenticator popAuthenticator = new PopupAuthenticator();
PasswordAuthentication pop = popAuthenticator.performCheck(user,
password);
props.put("mail.smtp.host", SMTPServer);
props.put("mail.smtp.auth", "true");
sendMailSession = Session.getInstance(props, popAuthenticator);
sendMailSession.setDebug(false);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(addresser));
newMessage.setSubject(subject, "GBK");
newMessage.setSentDate(new java.util.Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(content, "GBK");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
if (fileAttachment != null) {
messageBodyPart = new MimeBodyPart();
messageBodyPart.setFileName(filename);
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
multipart.addBodyPart(messageBodyPart);
}
// 添加多个附件
if (files.size() != 0) {
Enumeration efile = files.elements();
Enumeration names = filenames.elements();
while (efile.hasMoreElements() && names.hasMoreElements()) {
MimeBodyPart mbpFile = new MimeBodyPart();
String filestr = efile.nextElement().toString();
String namestr = names.nextElement().toString();
System.out.println(filestr);
System.out.println(namestr);
FileDataSource fds = new FileDataSource(filestr);
mbpFile.setDataHandler(new DataHandler(fds));
mbpFile.setFileName(namestr);
// 向MimeMessage添加(Multipart代表附件)
multipart.addBodyPart(mbpFile);
}
files.removeAllElements();
filenames.removeAllElements();
}
newMessage.setContent(multipart);
if (addressTO != null) {
newMessage.setRecipients(RecipientType.TO,
InternetAddress.parse(addressTO));
}
if (addressCC != null) {
newMessage.setRecipients(RecipientType.CC,
InternetAddress.parse(addressCC));
}
if (addressBCC != null) {
newMessage.setRecipients(RecipientType.BCC,
InternetAddress.parse(addressBCC));
}
transport = sendMailSession.getTransport("smtp");
transport.connect(SMTPServer, user, password);
transport.send(newMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
public class PopupAuthenticator extends Authenticator {
String username = null;
String password = null;
public PopupAuthenticator() {
}
public PasswordAuthentication performCheck(String user, String pass) {
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
public static void main(String[] args) throws UnsupportedEncodingException {
String smtpServer = "smtp.qq.com";
String user = "1025725439@qq.com";
String password = "**********";
String addresser = "1025725439@qq.com";
String subject = "你好,jone.";
String addressTO = "qwert1990817@126.com";
String addressBCC = "";
String addressCC = "";
String content = "Hello Greatjone!";
File file = new File("E:\\person.xls");
File file1 = new File("E:\\human.xls.xls");
Vector<File>files=new Vector<File>();
files.add(file);
files.add(file1);
Vector<String> filenames = new Vector<String>();
String filename = new String("person.xls".getBytes("gbk"), "iso-8859-1");
String filename1 = new String("human.xls.xls".getBytes("gbk"), "iso-8859-1");
filenames.add(filename);
filenames.add(filename1);
JavaMail mail = new JavaMail(smtpServer, user, password, addresser,
subject, content, addressTO, addressBCC, addressCC, files,
filenames);
mail.sendMail();
System.out.println("发送成功");
}
}