简单易用的 commons-email-1.1.jar

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>今天在Apache网站上下载commons-lang jar文件时突然看到commons-email-1.1.jar这个项目jar文件,看了看user guide确实比JDK提供的好多了,简单的几行代码就实现了发邮件的功能,以前实现过一个纯JavaMail带附件发邮件功能,代码复杂不说,现在回过头来都懒得看(主要是WEB项目注释少)。 贴上TEST代码看看,简单、清晰。只要稍加修改(邮件服务器地址、名称、密码)就可以了 简单邮件的发送: package com.bulktree.mail; import java.util.Date; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.SimpleEmail; publicclass SimpleMailTest { publicstaticvoid main(String[] args) throws EmailException { SimpleEmail email = new SimpleEmail(); //设置发送主机的服务器地址 email.setHostName("smtp.163.com"); //设置收件人邮箱 email.addTo("[email protected]","bulktree"); //发件人邮箱 email.setFrom("[email protected]", "bulktree"); //如果要求身份验证,设置用户名、密码,分别为发件人在邮件服务器上注册的用户名和密码 email.setAuthentication("bulktree", "123456"); //设置邮件的主题 email.setSubject("Hello, This is My First Email Application"); //邮件正文消息 email.setMsg("I am bulktree This is JavaMail Application"); email.send(); System.out.println("The SimpleEmail send sucessful!!!"); } } 带附件邮件发送: package com.bulktree.mail; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.MultiPartEmail; publicclass AttachmentMailTest { publicstaticvoid main(String[] args) throws EmailException, MalformedURLException { // 创建一个Email附件 EmailAttachment emailattachment = new EmailAttachment(); emailattachment.setPath("/biao_05.jpg"); // emailattachment.setURL(new URL("http://www.blogjava.net/bulktree/picture/bulktree.jpg")); emailattachment.setDisposition(EmailAttachment.ATTACHMENT); emailattachment.setDescription("This is Smile picture"); emailattachment.setName("bulktree"); // 创建一个email MultiPartEmail multipartemail = new MultiPartEmail(); multipartemail.setHostName("smtp.163.com"); multipartemail.addTo("[email protected]", "bulktree"); multipartemail.setFrom("[email protected]", "bulktree"); multipartemail.setAuthentication("bulktree", "123456"); multipartemail.setSubject("This is a attachment Email"); multipartemail.setMsg("this a attachment Eamil Test"); //添加附件 multipartemail.attach(emailattachment); //发送邮件 multipartemail.send(); System.out.println("The attachmentEmail send sucessful!!!"); } } Feedback # re: 用commons-email-1.1.jar实现发邮件功能: 回复 更多评论 2008-04-07 11:03 by Unmi 其实也 JDK 本身没有提供发邮件的功能,JavaMail 是 J2ee 应具有的。 用 commons-email 发邮件也是一样的,要注意发送中文时的乱码问题。 试着在英文系统环境下发带中文的标题和内容,commons-email 提供了相应的方法来设置字符集的。 文章来源:http://www.blogjava.net/bulktree/archive/2008/04/07/191170.html

你可能感兴趣的:(apache,jdk,.net,Web)