浏览 4044 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (5)
|
|
---|---|
作者 | 正文 |
发表时间:2009-02-16
最后修改:2009-02-17
package cn.iamsese.product.custom.tools.email; import java.text.MessageFormat; import java.util.Enumeration; import org.apache.commons.mail.Email; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.MultiPartEmail; import org.apache.commons.mail.SimpleEmail; /** * 简单的邮件发送客户端 -- 使用Apache Commons Email * cn.iamsese.product.custom.tools.email * Author: vb2005xu [JAVA菜鸟] */ public class SimpleEmailClient { private Email emailTranslate ; //email 传输器对象 , 可以为SimpleEmail 和 MultiPartEmail 作为实现对象 public SimpleEmailClient() { this.setEmailTranslate(new SimpleEmail()); } public SimpleEmailClient(boolean isMultiPartEmail) { if (isMultiPartEmail) this.setEmailTranslate(new MultiPartEmail()); else this.setEmailTranslate(new SimpleEmail()); } private String smtpHost, smtpUser, smtpPassword; public Email getEmailTranslate() { return emailTranslate; } public void setEmailTranslate(Email emailTranslate) { this.emailTranslate = emailTranslate; } public String getSmtpUser() { return smtpUser; } private void setSmtpUser(String smtpUser) { this.smtpUser = smtpUser; } public String getSmtpPassword() { return smtpPassword; } private void setSmtpPassword(String smtpPassword) { this.smtpPassword = smtpPassword; } public String getSmtpHost() { return smtpHost; } public void setSmtpHost(String smtpHost) { this.smtpHost = smtpHost; } public void setSmtpAuthentication(String smtpUser, String smtpPassword) { this.setSmtpUser(smtpUser); this.setSmtpPassword(smtpPassword); } @SuppressWarnings("finally") public boolean sendTextEmail(SimpleEmailObject emailObj) { boolean sendState = false; SimpleEmail simpleEmailTranslate = (SimpleEmail) this.getEmailTranslate(); simpleEmailTranslate.setHostName(this.getSmtpHost()); simpleEmailTranslate.setAuthentication(this.getSmtpUser(), this.getSmtpPassword()); simpleEmailTranslate.setCharset(emailObj.getCharset()); try { simpleEmailTranslate.addTo(emailObj.getTo(), emailObj.getReciverName()); simpleEmailTranslate.setFrom(emailObj.getFrom(), emailObj.getSenderName()); simpleEmailTranslate.setSubject(emailObj.getSubject()); simpleEmailTranslate.setMsg(emailObj.getMessage()); simpleEmailTranslate.send(); sendState = true; } catch (EmailException e) { // } finally { return sendState; } } @SuppressWarnings("finally") public boolean sendMultiPartEmail(MultiPartEmailObject emailObj) { boolean sendState = false; MultiPartEmail multiPartEmailTranslate = (MultiPartEmail) this.getEmailTranslate(); multiPartEmailTranslate.setHostName(this.getSmtpHost()); multiPartEmailTranslate.setAuthentication(this.getSmtpUser(), this.getSmtpPassword()); multiPartEmailTranslate.setCharset(emailObj.getCharset()); try { //添加附件 Enumeration<EmailAttachmentObject> attachments = emailObj.getAttachments().elements(); while (attachments.hasMoreElements()){ EmailAttachment attachment = attachments.nextElement().getEmailAttachment() ; if (attachment != null && attachment instanceof EmailAttachment) multiPartEmailTranslate.attach(attachment); } multiPartEmailTranslate.addTo(emailObj.getTo(), emailObj.getReciverName()); multiPartEmailTranslate.setFrom(emailObj.getFrom(), emailObj.getSenderName()); multiPartEmailTranslate.setSubject(emailObj.getSubject()); multiPartEmailTranslate.setMsg(emailObj.getMessage()); multiPartEmailTranslate.send(); sendState = true; } catch (EmailException e) { // } finally { return sendState; } } private final String tipInfoFormat = "{5} \nTime: {4,date,full} {4,time,full} \nTo: {0} \nFrom: {1} \nSubject: {2} \nMessage: {3}"; public String getErrorInfo(SimpleEmailObject emailObj) { // Assert.verify(s.equals("")); return MessageFormat.format(this.tipInfoFormat, emailObj.getTo(), emailObj.getFrom(), emailObj.getSubject(), emailObj .getMessage(), new java.util.Date(), "邮件发送失败<?>"); } public String getSuccessInfo(SimpleEmailObject emailObj) { return MessageFormat.format(this.tipInfoFormat, emailObj.getTo(), emailObj.getFrom(), emailObj.getSubject(), emailObj .getMessage(), new java.util.Date(), "邮件发送成功<!!> "); } public static void main(String[] args) { // SimpleEmailClient emailClient = new SimpleEmailClient(); SimpleEmailClient emailClient = new SimpleEmailClient(true); emailClient.setSmtpHost("smtp.sina.com"); emailClient.setSmtpAuthentication("vb2005xu", "123456"); //emailClient.testSendTextEmail(emailClient); emailClient.testSendMultiPartEmail(emailClient); } public String getTipInfoFormat() { return tipInfoFormat; } public void testSendTextEmail(SimpleEmailClient emailClient) { SimpleEmailObject emailObj = new SimpleEmailObject(); emailObj.setCharset("UTF-8"); emailObj.setTo("vb2005xu@sina.com"); emailObj.setReciverName("色色"); emailObj.setFrom("iamsese@hklm.com"); emailObj.setSenderName("会潜水的猪"); emailObj.setSubject("文本Email测试: "); emailObj.setMessage("纯文本信息" + this.getClass().getName()); if (emailClient.sendTextEmail(emailObj)) { System.out.println(emailClient.getSuccessInfo(emailObj)); System.out.println("--------------------------------"); } else { System.out.println(emailClient.getErrorInfo(emailObj)); System.out.println("--------------------------------"); } } public void testSendMultiPartEmail(SimpleEmailClient emailClient) { MultiPartEmailObject emailObj = new MultiPartEmailObject(); emailObj.setCharset("UTF-8"); emailObj.setTo("vb2005xu@sina.com"); emailObj.setReciverName("色色"); emailObj.setFrom("momo@hklm.com"); emailObj.setSenderName("默默"); emailObj.setSubject("默默相片附件测试: "); String[] attachmentIDs = new String[5]; attachmentIDs[0] = emailObj.addAttachment(null, new EmailAttachmentObject("默默","E:\\momo.jpg","默默的相片",false)); attachmentIDs[1] = emailObj.addAttachment(null, new EmailAttachmentObject("默默","E:\\momo.jpg","默默的相片",false)); attachmentIDs[2] = emailObj.addAttachment(null, new EmailAttachmentObject("默默","E:\\momo.jpg","默默的相片",true)); attachmentIDs[3] = emailObj.addAttachment(null, new EmailAttachmentObject("默默","E:\\momo.jpg","默默的相片",false)); attachmentIDs[4] = emailObj.addAttachment(null, new EmailAttachmentObject("默默","E:\\momo.jpg","默默的相片",false)); emailObj.setMessage("图片附件信息: " + emailObj.getAttachments().size()); if (emailClient.sendMultiPartEmail(emailObj)) { System.out.println(emailClient.getSuccessInfo(emailObj)); System.out.println("--------------------------------"); for (String attachmentID : attachmentIDs) System.out.println("附件ID: " + attachmentID); System.out.println("--------------------------------"); } else { System.out.println(emailClient.getErrorInfo(emailObj)); System.out.println("--------------------------------"); for (String attachmentID : attachmentIDs) System.out.println("附件ID: " + attachmentID); System.out.println("--------------------------------"); } } }
package cn.iamsese.product.custom.tools.email; /** * 简单的 Email 对象 ,一个 pojo类 * cn.iamsese.product.custom.tools.email * Author: vb2005xu [JAVA菜鸟] */ public class SimpleEmailObject { private String to, from, subject, message; private String senderName, reciverName; private String charset ; public String getTo() { return to; } public void setTo(String to) { this.to = to; } 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 getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getSenderName() { return senderName; } public void setSenderName(String senderName) { this.senderName = senderName; } public String getReciverName() { return reciverName; } public void setReciverName(String reciverName) { this.reciverName = reciverName; } public String getCharset() { return charset; } public void setCharset(String charset) { this.charset = charset; } }
package cn.iamsese.product.custom.tools.email; import java.util.Hashtable; //import org.apache.commons.mail.EmailAttachment; /** * 支持附件的 Email源对象 -- 支持 动态操作附件 * cn.iamsese.product.custom.tools.email * Author: vb2005xu [JAVA菜鸟] */ public class MultiPartEmailObject extends SimpleEmailObject { /** * 附件列表 -- org.apache.commons.mail.EmailAttachment 对象 */ private Hashtable<String, EmailAttachmentObject> attachments ; public MultiPartEmailObject(){ this.setAttachments(new Hashtable<String, EmailAttachmentObject>()); } /** * 添加一个指定名称的附件到列表中 -- 返回附件的id * 未指定id的将由系统来随机生成 * @param attachmentID * @param attachment * @return */ public String addAttachment(String attachmentID,EmailAttachmentObject attachment){ attachmentID = this.varityAttachmentID(attachmentID); if (attachmentID == null){ // attachmentID = String.valueOf(System.currentTimeMillis()) ; attachmentID = String.valueOf(System.currentTimeMillis()) + '@' + Integer.toHexString(attachment.hashCode()) ; } if (attachment != null && attachment instanceof EmailAttachmentObject){ this.getAttachments().put(attachmentID,attachment); return attachmentID ; } return null ; } /** * 验证 附件id的方法 -- 因为多个方法要求使用 * @param attachmentID * @return */ private String varityAttachmentID(String attachmentID){ if (attachmentID == null){ return null ; } attachmentID = attachmentID.trim(); if ("".equals(attachmentID)){ return null ; } if (! this.getAttachments().containsKey(attachmentID) ){ return null ; } return attachmentID ; } /** * 通过 附件id移除一个附件 -- 成功则返回被删除附件的id * @param attachmentID * @return */ public String removeAttachmentByID(String attachmentID){ attachmentID = this.varityAttachmentID(attachmentID); if (attachmentID != null){ this.getAttachments().remove(attachmentID); return attachmentID ; } return null ; } /** * 通过 附件id 查找 附件 * @param attachmentID * @return */ public EmailAttachmentObject getAttachmentByID(String attachmentID){ attachmentID = this.varityAttachmentID(attachmentID); if (attachmentID != null){ //这里不用强制转换是因为 使用了 哈希表的泛型定义 return this.getAttachments().get(attachmentID); } return null ; } public Hashtable<String, EmailAttachmentObject> getAttachments() { return attachments; } public void setAttachments(Hashtable<String, EmailAttachmentObject> attachments) { this.attachments = attachments; } }
package cn.iamsese.product.custom.tools.email; import org.apache.commons.mail.EmailAttachment; /** * Email 附件对象封装类 * cn.iamsese.product.custom.tools.email * Author: vb2005xu [JAVA菜鸟] */ public class EmailAttachmentObject { private String desc , name , path ; private boolean isInLine ; //缺省为false public EmailAttachmentObject(){} public EmailAttachmentObject(String name ,String path ,String desc , boolean isInLine){ this.setName(name);this.setPath(path);this.setDesc("");this.setInLine(isInLine); } /** * 初始化 , 防止 传入的参数 不达标 */ private void init(){ this.name = (name != null && ! "".equals(name.trim())) ? name : "" ; this.path = (path != null && ! "".equals(path.trim())) ? path : "" ; this.desc = (desc != null && ! "".equals(desc.trim())) ? path : "" ; } /** * 获取 邮件附件对象 * @return */ public EmailAttachment getEmailAttachment(){ this.init(); EmailAttachment attachment = null ; if (! "".equals(this.getPath()) ){ attachment = new EmailAttachment(); attachment.setPath(this.getPath()); attachment.setDescription(this.getDesc()); attachment.setDisposition(this.getDisposition()); attachment.setName(this.getName()); } return attachment ; } public void setInLine(boolean isInLine) { this.isInLine = isInLine; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDisposition() { return (isInLine) ? EmailAttachment.INLINE : EmailAttachment.ATTACHMENT ; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } } 出现了邮件附件名称乱码的问题,见图
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-02-16
封装之后用在自己搞的的用于小型Web系统开发的零配置的FleaJAVA框架中还可以.
在写的过程中又进一步学习了JAVA 面向对象编程的技术,不错! |
|
返回顶楼 | |
发表时间:2009-02-26
这个问题我也遇到了 但是不晓得怎么解决!!!
我测试了 英文、中文、日文内容都没问题,就是附件文件名出现乱码! |
|
返回顶楼 | |
发表时间:2009-03-05
这个。apache网站上都有说明。
另外,这个不是异步进行的。用于简单发送邮件还行。 还是 spring+activeMQ用着爽一些。 |
|
返回顶楼 | |
发表时间:2009-03-05
kaka2008 写道 这个。apache网站上都有说明。
另外,这个不是异步进行的。用于简单发送邮件还行。 还是 spring+activeMQ用着爽一些。 那你告诉我在哪里?? apache 网站大了去了,不知道就别这样说. 有没有去和那玩意比的意思,再说了,我做什么都套着框架用么?偶不知道你脑子是怎么想的 就为了添加一个简单的邮件功能,却需要加入那么多额外的东西进来. 有病么? 何况我一开始就说了,只是自己学习和简单开发. 你那么牛X,干嘛不使用JMS,还用邮件干嘛? |
|
返回顶楼 | |
发表时间:2009-03-11
你写个东西还不允许比尔评论了?!
只喜欢听好听的,不喜欢听难听点? 如果你时间多,那么你就自己慢慢的在那里封装。 别人已经封装好了的,你再去自己封装。当练习可以啊 “不发明重复的轮子。。。” 支持封装,但更支持用现成的! |
|
返回顶楼 | |
发表时间:2009-03-11
请注意我一开始就说了:
封装之后用在自己搞的的用于小型Web系统开发的零配置的FleaJAVA框架中还可以. 在写的过程中又进一步学习了JAVA 面向对象编程的技术,不错! 我用在自己的东西里面,请问爱你什么事了??? 我学习不行么?? 我不用 spring+activeMQ 就不行么? 我想自己写不行么??? 我要简单的东西还不行么?? 我真不知你们这些人都是怎么想的? 为了一个小功能,都是加上N多的额外的代码,做着做着就给自己添加N大的 负担了... 比如 activeMQ ,你要用它得熟悉它吧,简单的东西为什么就不自己来呢? 我乐意,我自己封装,我乐意.. 我的fleajava这个小玩意 我给自己订的目标是 小于100K的, 数据层打算自己实现个活动记录的db样板 使用JSP+JSTL做视图样板的 web框架,,爱你什么事了,我就郁闷.. 我不要那种 动不动就是好多M的东西,那些类我都不知道干嘛用的. 我不需要... OK,这个话题打住,, 你们的好意我心领了, 但是我要做自己的东西 一开始我就说过,我做了一些网站,我发现在开发的过程中,我总是在 那些框架的代码编写中花费了最多的时间,而不是在逻辑处理的过程中 所以我要抽出一套东西来做自己接的站 ... 不知道你们里不理解吧 我不是做你们所说的企业级应用,我是做网站.. 做轻量级的,不,如果 是你们理解的轻量级,那我的这个就是想实现 微量级的东西 ... 算了 ... 真的很无语 |
|
返回顶楼 | |