项目要发送邮件,本来是想找原来的发送邮件的代码拿过来用的,突然记起来commons项目增加了一个commons-email发送邮件,决定用现成的,看了一下,果然很方便,直接贴出网站上的发送HTML邮件的例子:

// Create the email message
HtmlEmail 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 id
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = email.embed(url, "Apache logo");

// set the html message
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");

// send the email
email.send();

不需要解释了。发送邮件的时候还可以嵌图片,呵呵,喜欢。
这个例子里面没有涉及的有两个常用的方面:
登录验证和邮件的编码格式,其实也很简单,验证调用一下

email.setAuthentication("me", "password");
对于中文邮件,设置下编码就行了:
email.Charset("utf-8");或者
email.Charset("utf8");

(Visited 231 times, 1 visits today)