利用commons-email发送邮件的简单例子

1.这个是Email类

import java.util.ArrayList; import java.util.List; public class Email { public enum KIND { HTML, TEXT } private String host; private List<String> to; private String from; private String subject; private String htmlContent; private String textContent; private KIND type; public Email() { to = new ArrayList<String>(); } public void setKind(KIND kind) { this.type = kind; } public KIND getKind() { return type; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String[] getTo() { return to.toArray(new String[to.size()]); } public void addTo(String receiver) { to.add(receiver); } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getHtmlContent() { return htmlContent; } public void setHtmlContent(String htmlContent) { this.htmlContent = htmlContent; } public String getTextContent() { return textContent; } public void setTextContent(String textContent) { this.textContent = textContent; } }

 

2.以下是发送类

客户调用sendEmail方法向Email队列中添加邮件,而发送类有一个线程负责每2秒中检查邮件队列,如果不为空,取出邮件并发送

import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class MailSender { private final static Log logger = LogFactory.getLog(MailSender.class); private final static MailSender unique = new MailSender(); public static MailSender getInstance() { return unique; } private int totalSent = 0; private int succSent = 0; private int failSent = 0; private Queue<Email> queue; private MailSender() { queue = new LinkedList<Email>(); realSend(); } public int getTotalSent() { return totalSent; } public int getSuccSent() { return succSent; } public int getFailSent() { return failSent; } public void sendEmail(Email email) { synchronized(queue) { queue.add(email); } if (logger.isDebugEnabled()) { logger.debug("Enqueue one email succ"); } } public void sendEmail(Email[] emails) { synchronized (queue) { for (int i = 0; i < emails.length; i++) { queue.add(emails[i]); } } if (logger.isDebugEnabled()) { logger.debug("Enqueue emails[" + emails.length + "] succ"); } } private void realSend() { Runnable run = new Runnable() { public void run() { while (true) { List<Email> emails = new ArrayList<Email>(); synchronized (queue) { while (queue.peek() != null) { emails.add(queue.poll()); } } if (logger.isDebugEnabled()) { logger.debug("Get Email Count=" + emails.size()); } for (Email email : emails) { try { if (email.getKind() == Email.KIND.HTML) { sendHtmlEmail(email); } else { sendTextEmail(email); } if (logger.isDebugEnabled()) { logger.debug("Send Email Succ"); } succSent++; } catch (EmailException e) { logger.error("Send Email Error", e); failSent++; } totalSent++; } try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { logger.error("Sleep Error", e); } if (logger.isDebugEnabled()) { logger.debug("TotalSent=" + totalSent + ";SuccSent=" + succSent + ";FailSent=" + failSent); } } } }; Thread thread = new Thread(run); thread.setDaemon(true); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } private void sendHtmlEmail(Email email) throws EmailException { HtmlEmail sender = new HtmlEmail(); sender.setHostName(email.getHost()); String[] to = email.getTo(); for (int i = 0; i < to.length; i++) { sender.addTo(to[i]); } sender.setFrom(email.getFrom()); sender.setSubject(email.getSubject()); sender.setHtmlMsg(email.getHtmlContent()); sender.setTextMsg(email.getTextContent()); sender.send(); } private void sendTextEmail(Email email) throws EmailException { HtmlEmail sender = new HtmlEmail(); sender.setHostName(email.getHost()); String[] to = email.getTo(); for (int i = 0; i < to.length; i++) { sender.addTo(to[i]); } sender.setFrom(email.getFrom()); sender.setSubject(email.getSubject()); sender.setTextMsg(email.getTextContent()); sender.send(); } }

 

3.测试类

import java.util.ArrayList; import java.util.List; import org.junit.Test; public class TestMailSender { private static final int SEND_COUNT = 10; @Test public void testEmailSender() { String html = "<html><body><p><font color='blue'>This is a test email.</font></p></body></html>"; String msg = "This is a test email."; String subject = "Test Email"; String host = "yoursmtphost"; String from = "senderaddress@yourdomain"; String to = "toaddress@todomain"; final List<Email> emails = new ArrayList<Email>(); for (int i = 0; i < SEND_COUNT; i++) { Email email = new Email(); email.setHost(host); email.setFrom(from); email.addTo(to); email.setSubject(subject); if (i % 2 == 0) { email.setKind(Email.KIND.HTML); email.setHtmlContent(html); email.setTextContent(msg); } else { email.setKind(Email.KIND.TEXT); email.setTextContent(msg); } emails.add(email); } final MailSender sender = MailSender.getInstance(); Thread thread1 = new Thread() { public void run() { for (int i = 0; i < SEND_COUNT / 2; i++) { sender.sendEmail(emails.get(i)); } } }; Thread thread2 = new Thread() { public void run() { for (int i = SEND_COUNT / 2; i < SEND_COUNT; i++) { sender.sendEmail(emails.get(i)); } } }; thread1.start(); thread2.start(); try { Thread.sleep(20 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } int totalSent = sender.getTotalSent(); int succSent = sender.getSuccSent(); int failSent = sender.getFailSent(); assertEquals("TotalSent should be " + SEND_COUNT, SEND_COUNT, totalSent); assertEquals("SuccSent should be 0", 0, succSent); assertEquals("FailSent should be " + SEND_COUNT, SEND_COUNT, failSent); } }

你可能感兴趣的:(thread,html,String,Class,email,import)