直接上代码:
package com.javamail;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
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 SendMail
{
//收件人邮箱地址
private String to;
//发件人邮箱地址
private String from;
//SMTP服务器地址
private String smtpServer;
//登录SMTP服务器的用户名
private String username ;
//登录SMTP服务器的密码
private String password ;
//邮件主题
private String subject;
//邮件正文
private String content;
//记录所有附件文件的集合
List<String> attachments
= new ArrayList<String>();
//无参数的构造器
public SendMail()
{
}
public SendMail(String to , String from , String smtpServer
, String username , String password
, String subject , String content)
{
this.to = to;
this.from = from;
this.smtpServer = smtpServer;
this.username = username;
this.password = password;
this.subject = subject;
this.content = content;
}
//to属性的setter方法
public void setTo(String to)
{
this.to = to;
}
//from属性的setter方法
public void setFrom(String from)
{
this.from = from;
}
//smtpServer属性的setter方法
public void setSmtpServer(String smtpServer)
{
this.smtpServer = smtpServer;
}
//username属性的setter方法
public void setUsername(String username)
{
this.username = username;
}
//password属性的setter方法
public void setPassword(String password)
{
this.password = password;
}
//subject属性的setter方法
public void setSubject(String subject)
{
this.subject = subject;
}
//content属性的setter方法
public void setContent(String content)
{
this.content = content;
}
//把邮件主题转换为中文
public String transferChinese(String strText)
{
try
{
strText = MimeUtility.encodeText(
new String(strText.getBytes()
, "GB2312"), "GB2312", "B");
}
catch(Exception e)
{
e.printStackTrace();
}
return strText;
}
//增加附件,将附件文件名添加的List集合中
public void attachfile(String fname)
{
attachments.add(fname);
}
//发送邮件
public boolean send()
{
//创建邮件Session所需的Properties对象
Properties props = new Properties();
props.put("mail.smtp.host" , smtpServer);
props.put("mail.smtp.auth" , "true");
//创建Session对象
Session session = Session.getDefaultInstance(props
//以匿名内部类的形式创建登录服务器的认证对象
, new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,password);
}
});
try
{
//构造MimeMessage并设置相关属性值
MimeMessage msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress(from));
//设置收件人
InternetAddress[] addresses = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO , addresses);
//设置邮件主题
subject = transferChinese(subject);
msg.setSubject(subject);
//整个邮件的组成方式
MimeMultipart msgMultipart = new MimeMultipart("related");//邮件的组成方式
MimeBodyPart contents = new MimeBodyPart();//正文
String body = "<img src = \"cid:hango\">";//cid:hango中要注意hango
contents.setContent(body, "text/html;charset=gbk");
msgMultipart.addBodyPart(contents);
MimeBodyPart jpgBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(content);
jpgBodyPart.setDataHandler(new DataHandler(fds));
jpgBodyPart.setContentID("hango"); //和上面对应
msgMultipart.addBodyPart(jpgBodyPart);
msg.setContent(msgMultipart);
msg.setSentDate(new Date());
// 保存对邮件的修改并写入文件中
msg.saveChanges();
Transport.send(msg);
}
catch (MessagingException mex)
{
mex.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args)
{
// String path = System.getProperty("user.dir")+"\\";
String path = "E:\\apache-tomcat-6.0.29\\webapps\\javamail\\WEB-INF\\";
String[] conf = null;
//读取文件
try {
BufferedReader br = new BufferedReader(new FileReader(path+"config.txt"));
String data = br.readLine();//一次读入一行,直到读入null为文件结束
String config = "";
while(data!=null){
if(!"".equals(data) && !data.contains("#")){
data = data.substring(data.indexOf("=")+1, data.length());
config += data.trim() + ",";
}
data = br.readLine(); //接着读下一行
}
config = config.substring(0,config.length()-1);
conf = config.split(",");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SendMail sendMail = new SendMail();
sendMail.setSmtpServer(conf[0]);
//此处设置登录的用户名
sendMail.setUsername(conf[2]);
//此处设置登录的密码
sendMail.setPassword(conf[3]);
//设置发送人地址
sendMail.setFrom(conf[2]);
//设置标题
sendMail.setSubject(conf[4]);
//设置内容
sendMail.setContent(conf[5]);
//读取文件
try {
BufferedReader br = new BufferedReader(new FileReader(path+conf[6]));
String data = br.readLine();//一次读入一行,直到读入null为文件结束
while( data!=null){
//设置收件人的地址
sendMail.setTo(data);
if (sendMail.send())
{
System.out.println("---邮件发送成功---"+data);
}
data = br.readLine(); //接着读下一行
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
=========config.txt==============
################
#注释请用"#"开头
################
#SMTP服务器
server=smtp.exmail.qq.com
#SMTP端口号
port=25
#用户名
username=
#密码
password=
#邮件主题
title=222
#邮件内容
content=c:\\123.jpg
#收件人TXT
txtPath=to.txt
======to.txt====
1123@qq.com
2323023498@163.com