Apache Common Email的几个例子
2016-09-11 22:40
302 查看
Apache Common Email的几个例子
最近用到了发邮件,于是就看到了JavaMail和Apache Common Email,先开始试的是JavaMail,后来发现一个不知道认证失败的错误,于是干脆就直接使用Common Email,觉得还是挺简单容易使用的。一般只用第一种,记录下来。以后有需要了再来看最简单的Text mail
Email email = new SimpleEmail(); email.setHostName("smtp.googlemail.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator("username", "password")); email.setSSLOnConnect(true); email.setFrom("user@gmail.com"); email.setSubject("TestMail"); email.setMsg("This is a test mail ... :-)"); email.addTo("foo@bar.com"); email.send();
Sending emails with attachments
import org.apache.commons.mail.*; ... // Create the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setPath("mypictures/john.jpg"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Picture of John"); attachment.setName("John"); // Create the email message MultiPartEmail email = new MultiPartEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("The picture"); email.setMsg("Here is the picture you wanted"); // add the attachment email.attach(attachment); // send the email email.send();
Attachment 也可以指向一个合法的url,发送时就会把这个url内容给download下来进行发送.
Sending HTML formatted email
import org.apache.commons.mail.HtmlEmail; ... // Create the email message HtmlEmail email = new HtmlEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.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();
Sending HTML formatted email with embedded images
import org.apache.commons.mail.HtmlEmail; ... // load your HTML email template String htmlEmailTemplate = ".... <img src=\"http://www.apache.org/images/feather.gif\"> ...."; // define you base URL to resolve relative resource locations URL url = new URL("http://www.apache.org"); // create the email message ImageHtmlEmail email = new ImageHtmlEmail(); email.setDataSourceResolver(new DataSourceUrlResolver(url)); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("Test email with inline image"); // set the html message email.setHtmlMsg(htmlEmailTemplate); // set the alternative message email.setTextMsg("Your email client does not support HTML messages"); // send the email email.send();
PS:
在本地使用common mail的时候没有任何问题,但是放到了服务器不知道为什么就一直报connection reset,通过telnet手动发stmp的命令已经成功发送了邮件,无奈只能换回了JavaMail,没有出错。可能是某一项没有设置导致的。
相关文章推荐
- apache common email组件小结
- apache mod_rewrite 模块使用的几个例子
- apache common email组件小结
- apache common email组件
- apache common email组件小结
- apache common email组件小结
- apache common email组件
- Apache Common Email Subject不能有空行
- 使用apache common email 组件发送邮件
- apache mod_rewrite 模块使用的几个例子
- apache common-email使用
- apache common email组件
- 关于使用Apache 的common-email-1.2.jar 包写发送Email
- jquery 验证Email邮箱格式的几个例子
- js判断手机访问或者PC的几个例子(常用于手机跳转)
- jQuery 复合选择器的几个例子
- android之Uri的常用几个例子
- 经常用到的提取手机、电话、Email联系方式的几个java正则表达式
- Apache对象池插件common-pool学习小结
- php curl的几个例子