1//HtmlEmail! 2HtmlEmail email =new HtmlEmail(); 3 email.setHostName("mail.4ya.cn"); 3 email.setAuthentication("<username>","<password>") 5 email.addTo("martin@4ya.cn"martin"); 6 email.setFrom("martin.xus@gmail.com"martin"); 7 email.setSubject("主题:该邮件包括html格式内容"); 8// embed the image and get the content id 9// 注意这里:embed 将帮助我们创建标签如:cid:xxx url 10URL url =new URL("http://www.apache.org/images/asf_logo_wide.gif"); 11 String cid = email.embed(url, "Apache logo"); 12 13/** 14 set the html message 15 我们看到HtmlEmail extends Email的,它依然有setMsg(),但是这里发送的邮件包括了插入在邮件内容中的图片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代码 16 **/ 17 email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>"); 18 19// set the alternative message 20email.setTextMsg("Your email client does not support HTML messages"); 21 22//set mail 23email.send(); 24