一、首先在lib导入 commons-email-1.2.jar
发送邮件:
1、一个附件 发送给一个人
HtmlEmail email = new HtmlEmail();
email.setAuthenticator(new DefaultAuthenticator("自己的邮箱", "密码"));
email.setHostName(”smtp.qq.com“);
//设置收件人
email.addTo(”mailto(收件人的邮箱)", "收件人昵称(可以为空)");
//设置发件人
email.setFrom("自己的邮箱", "发件人昵称(可以为空)");
//主题
email.setSubject(subject);
// 设置邮件正文和字符编码
StringBuffer bodyBf = new StringBuffer();
bodyBf.append(content);
email.addPart(bodyBf.toString(), "text/html;charset=utf-8");
//附件
if (!attachment.equals("")) {
EmailAttachment attach = new EmailAttachment();
attach.setPath(attachment);
attach.setDisposition(EmailAttachment.ATTACHMENT);
email.attach(attach);
}
email.send();
2、多个附件 多个收件人
HtmlEmail email = new HtmlEmail();
email.setAuthenticator(new DefaultAuthenticator(“自己的邮箱","密码"));
email.setHostName("smtp.qq.com");
// email.addTo(mailto);
//设置发件人
email.setFrom("自己的邮箱", "发件人昵称(可以为空)");
//主题
email.setSubject(subject);
//设置多个收件人
List<InternetAddress> inter=new ArrayList<InternetAddress>();
inter.add(new InternetAddress(“****@qq.com”);
inter.add(new InternetAddress(“******@qq.com”);
email.setTo(inter);
// 设置邮件正文和字符编码
StringBuffer bodyBf = new StringBuffer();
bodyBf.append(content);
email.addPart(bodyBf.toString(), "text/html;charset=utf-8");
//添加多个附件
List<String> sumpath=null;
if (attachment!=null) {
//通过附件上传方法upFile() 得到附件的上传后的路经(类型List<String>)
sumpath=upFile();
List<EmailAttachment> attachlist= new ArrayList<EmailAttachment>();
for(int i=0;i<attachment.size();i++){
EmailAttachment attach = new EmailAttachment();
attach.setPath(sumpath.get(i));
//中文附近名 可以显示
attach.setName(MimeUtility.encodeText(attachmentFileName.get(i)));
attach.setDisposition(EmailAttachment.ATTACHMENT);
attachlist.add(attach);
}
for(int j=0;j<sumpath.size();j++){
email.attach(attachlist.get(j));
}
}
email.send();
补充上传方法upFile()
private List<File> attachment;
private InputStream fileStream;
private List<String> attachmentFileName; //上传的文件名 (1.系统自动注入 2.变量命名有规则: 前台对象名+"FileName")
private List<String> attachmentContentType; //文件类型 (1.系统自动注入 2.变量命名有规则: 前台对象名+"ContentType")
String savepath="\\Email";
/**
* 上传多个文件
* @return
*/
private List<String> upFile(){
String path = ServletActionContext.getServletContext().getRealPath(savepath) + "/" ;
List<String> sumpath=new ArrayList<String>();
File file = new File(path);
if(!file.exists())
{
file.mkdirs();;
System.out.println("目录需要创建!!");
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
if (attachment != null)
{
for (int i = 0; i < attachment.size(); i++)
{
try{
bis = new BufferedInputStream(new FileInputStream(attachment.get(i)));
bos = new BufferedOutputStream(new FileOutputStream(file.getPath()+"/"+ attachmentFileName.get(i)));
byte[] buf = new byte[(int)attachment.get(i).length()];
int len = 0;
while(((len=bis.read(buf))!=-1)){
bos.write(buf, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
}catch(Exception e){
bos = null;
bis = null;
}
}
sumpath.add(file.getPath()+"\\"+ attachmentFileName.get(i));
}
}
return sumpath;
}
* 上传一个文件
* @return
*/
private String upFile(){
String path = ServletActionContext.getServletContext().getRealPath(savepath) + "/" + attachmentFileName;
File file = new File(path);
if(!file.exists())
{
file.mkdirs();;
System.out.println("目录需要创建!!");
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(attachment));
bos = new BufferedOutputStream(new FileOutputStream(file.getPath()));
byte[] buf = new byte[(int)attachment.length()];
int len = 0;
while(((len=bis.read(buf))!=-1)){
bos.write(buf, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
}catch(Exception e){
bos = null;
bis = null;
}
}
return path;
}