package test;
import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
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 org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
public class MailSenderImpl {
private static final org.apache.log4j.Logger LOG = Logger.getLogger(MailSenderImpl.class);
private Session session;
private Transport transport;
public void init() throws Exception{
this.session = Session.getDefaultInstance(new Properties(), null);
this.transport = session.getTransport("smtp");
this.transport.connect("219.145.110.51", "sendmail@mail.163.com", "123456");
}
public boolean send(String subject, String content, String target){
try{
System.setProperty("mail.smtp.auth","true");
MimeMessage message =new MimeMessage(session);
/** 发送人地址 */
Address fromAddress = new InternetAddress("sendmail@mail.163.com");
message.setFrom(fromAddress);
message.setReplyTo(new Address[]{fromAddress});
/** 收件人地址 */
Address toAddress = new InternetAddress(target);
message.addRecipient(Message.RecipientType.TO, toAddress);
/** 邮件主题 */
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
message.setSubject("=?GB2312?B?"+enc.encode(subject.getBytes())+"?=");
MimeBodyPart messageBodyPart =new MimeBodyPart();
/** 邮件文本内容 */
// messageBodyPart.setText("Hi");//邮件正文内容
/** 要发送附件的位置 */
String fileAttachment = "D:/workDoc/iof_8888.xml";
/** 邮件引入其它文件内容 */
DataSource fds = new javax.mail.util.ByteArrayDataSource(content.getBytes(),"text/html");
messageBodyPart.setDataHandler(new DataHandler(fds));
/** 邮件附件 */
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source =new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
/** 以下为不带附件的写法 */
// MimeMessage message = new MimeMessage(session);
//
// Address fromAddress = new InternetAddress("sendmail@mail.yimei.com");
// message.setFrom(fromAddress);
// message.setReplyTo(new Address[]{fromAddress});
//
// Address toAddress = new InternetAddress(target);//gj@yimei.com
// message.addRecipient(Message.RecipientType.TO, toAddress);
//
// sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
// message.setSubject("=?GB2312?B?"+enc.encode(subject.getBytes())+"?=");
//
// DataSource fds = new javax.mail.util.ByteArrayDataSource(content.getBytes(),"text/html");
// message.setDataHandler(new DataHandler(fds));
//
// transport.sendMessage(message, message.getAllRecipients());
return true;
}catch(Exception e){
LOG.error(e.getMessage(),e);
}
return false;
}
public void close(){
try{
transport.close();
}catch(Exception e){
}
}
public static void main(String[] args) throws Exception{
long start = System.currentTimeMillis();
String[] targets = new String[]{"zj@yimei.com","zj@yimei.com","zj@yimei.com","zj@yimei.com","zmfkplj@163.com","zmfkplj@126.com"};
MailSenderImpl sender = new MailSenderImpl();
sender.init();
for(String target:targets){
sender.send("测试邮件", FileUtils.readFileToString(new File("D:/workDoc/index.html")), target);
}
sender.close();
long end = System.currentTimeMillis();
System.out.println("elapse time: "+(end-start));
}
}