使用commons-email发送邮件
项目要发送邮件,本来是想找原来的发送邮件的代码拿过来用的,突然记起来commons项目增加了一个commons-email发送邮件,决定用现成的,看了一下,果然很方便,直接贴出网站上的发送HTML邮件的例子:
// Create the email messageHtmlEmail email = new HtmlEmail();email.setHostName(”mail.myserver.com”);email.addTo(”jdoe-AT-somewhere-DOT-org”, “John Doe”);email.setFrom(”me-AT-apache-DOT-org”, “Me”);email.setSubject(”Test email with inline image”); // embed the image and get the content idURL url = new URL(”http://www.apache.org/images/asf_logo_wide.gif”);String cid = email.embed(url, “Apache logo”); // set the html messageemail.setHtmlMsg(”<html>The apache logo - <img src=”cid:”+cid+””></html>”); // set the alternative messageemail.setTextMsg(”Your email client does not support HTML messages”); // send the emailemail.send();
不需要解释了。发送邮件的时候还可以嵌图片,呵呵,喜欢。
这个例子里面没有涉及的有两个常用的方面:
登录验证和邮件的编码格式,其实也很简单,验证调用一下
email.setAuthentication("me", "password");对于中文邮件,设置下编码就行了:email.Charset(”utf-8″);或者email.Charset(”gbk”);
作者: Cherami
原载: 使用commons-email发送邮件
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。
日志评价
相关日志
随机日志
添加到网摘
[del.icio.us] [新浪 VIVI] [365key] [YouNote] [博采中心] [Poco] [SOHU狐摘] [天极网摘] [和讯网摘]喜欢这个插件?

暂时没有评论
发表评论 | RSS订阅 | 反向链接