apache common mail包相关类解析
来源:互联网 发布:大数据的就业前景 编辑:程序博客网 时间:2024/04/24 18:51
在common mail包中总共有8各类,主要是那个Email类,这个类里面包括了发送邮件的几乎全部方法。它的子类MultiPartEmail扩展了几个简单的方法,主要的是扩展一个邮件附件的方法,MultiPartEmail的子类HtmlEmail定义了几个重载的embed方法,这几个方法我觉得在平常工作当中用到的机会不是很多。其他的几个类都是在这三个类当中作为方法参数或者变量出现。
所以我们在实现邮件发送的功能上,我们足足可以只使用HtmlEmail类来操作。创建一个HtmlEmail如下:
private final HtmlEmail createHtmlEmail() { HtmlEmail email = new HtmlEmail(); email.setHostName(hostname); email.setSmtpPort(port); email.setCharset("GBK"); email.setAuthentication(sender, password); return email; }这些方法都不是HtmlEmail类中的,全部都是Email中的方法。而发送的代码可以像下面这样写:
if(mailEntry != null) { htmlEmail.setSubject(mailEntry.getTitle()); try { htmlEmail.addTo(mailEntry.getRecipient()); htmlEmail.setFrom(sender); htmlEmail.setHtmlMsg(mailEntry.getContent()); if (mailEntry.getAttachment() != null) { htmlEmail.attach(mailEntry.getAttachment()); } htmlEmail.send(); } catch (EmailException ex) { LOGGER.error(String.format("HtmlEmail send failure. Cause: %s", ex.getMessage())); } }mailEntry是自己写的一个pojo类。没什么好说的。上面代码中的一些方法中setHtmlMsg是HtmlEmail类自身的,这个方法很简单,只是简单的赋值操作。attach方法是MultiPartEmail,方法的具体实现可以自己看源码,其他的方法也都是Email的。
当需要发送群邮件时,也是很简单的,Email类都为我们封装好了,我们需要做的只是简单的拼装邮件的内容,发送者,接受者等等信息:
public void sendMultiplyMails(MailEntry mail) throws EmailException { if (mail.getRecipients() != null && mail.getRecipients().size() == 1) { mail.setRecipient(mail.getRecipients().get(0)); sendMail(mail); return; } HtmlEmail email = createHtmlEmail(); email.setSubject(mail.getTitle()); try { email.setTo(mail.getRecipients()); email.setFrom(sender); email.setHtmlMsg(mail.getContent()); email.send(); } catch (EmailException ex) { LOGGER.error("HtmlEmail send failure. Cause " + ex.getMessage()); } }另外一个问题是我们需不需要将邮件发送的代码放入一个线程当中,如果我们把相关方法放入一个线程的run方法内,那么执行这个方法我们需要new Thread(new XxxThread()).start或new XxxThread().start的方法来执行。我们可以使用线程来实现多任务,如果没有使用线程有可能会出现代码的混乱执行(就变得不是并发的了,关于并发的只是可以查看相关书籍)。当然我们使用了线程实现的话我们还需要对线程的停止进行相关的操作:
private static void stopTask(Thread task) { if(task != null && task.isAlive()) { try { task.join(); } catch(InterruptedException ex) { // 线程被中断 } catch(Exception ex) { task.interrupt(); } } }join()方法的意思是等待线程消亡,注意这里是无参数的,这就意味着永远等待下去if(l == 0L) for(; isAlive(); wait(0L));在等待过程中别的线程不能执行相关代码。这样就相当于实现了同步。
所以我们在发送邮件时最好还是将代码放入线程当中。
- apache common mail包相关类解析
- Apache Common-Mail发送邮件
- Common mail 包的使用
- apache common包的一些简便类
- Apache common-lang 包
- Apache-common包之common-cli笔记
- Apache-common 包之 common-cli 笔记
- Apache-common 包之 common-configuration 笔记
- Apache -Common-lang包使用
- apache common常用包说明
- Apache -Common-lang包使用
- Apache -Common-lang包使用
- Apache common包应用集合
- apache common包中的一些有用的帮助类 --BooleanUtils
- [Java]Apache Common Lang包的StringUtils类
- apache common包 CollectionUtils 使用 详解
- apache common Lang包StringUtils系列(一)
- apache common Lang包StringUtils系列(二)
- ARM-Linux s3c2440 之UART分析(五)
- ArcGIS Engine代码共享-颜色(Color)对象函数
- apache+mod_wsgi+django在windows下的部署
- ArcGIS Engine代码共享-要素(Feature)操作
- 递归性高或者逻辑简单、性能要求严格、维护人数多的字符串分析,尽量避免正则表达式
- apache common mail包相关类解析
- ArcGIS Engine代码共享-字段(Field)操作
- free指针所指向的内容
- USB设备数据还原
- tcl/tk快速入门
- ArcGIS Engine代码共享-图层(Layer)操作
- filenet 流程解锁时的问题 [Err=d56d000c] Work queue record not locked
- ArcGIS Engine代码共享-投影(SpatialReference)对象操作
- VC环境中调用TeeChart控件实现数据曲线分析(转载)