Apache Commons Email组件介绍使用
来源:互联网 发布:网络机顶盒排行榜 编辑:程序博客网 时间:2024/05/21 22:41
Apache Commons Email组件介绍使用
介绍
发送邮件功能这几乎是很多应用程序都会使用到的,所以Apache Commons提供了Email组件便捷的发送邮件方式。只需要简单的几个步骤就能够达到发送邮件的功能。
几种常见的发送邮件格式
SimpleEmail(普通文本)
// 定义发送简单邮件对象 SimpleEmail simpleEmail = new SimpleEmail(); // 设置SMTP服务器,比如:smtp.163.com // Set the hostname of the outgoing mail server. simpleEmail.setHostName("smtp.163.com"); // 设置登入认证服务器的用户名和密码 // Sets the userName and password if authentication is needed. If this // method is not used, no authentication will be performed. simpleEmail.setAuthentication("username", "password"); // 默认是25端口 simpleEmail.setSmtpPort(25); // SSL enabled SMTP server,即如果是支持SSL服务器的端口是465 // simpleEmail.setSmtpPort(465);//gmail邮箱服务器就是支持SSL的。 // 设置发送人邮箱和名字 simpleEmail.setFrom("xxx@163.com", "xuyi", "utf-8"); // 设置收件人可以是多个 simpleEmail.addTo("xxx@qq.com", "sky-xuyi"); // simpleEmail.addTo(String ...emails); // 设置发送主题 simpleEmail.setSubject("use apache email send mial"); // 设置发送主体内容 simpleEmail.setMsg("apache commons emial is a very good commonent for send email"); // 确定发送邮件动作 simpleEmail.send(); System.out.println("send mail success");
MultiPartEmail(携带附件的邮件)
// 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 attachment // 创建附件(本地不存在的附件,在网络上下载) EmailAttachment attachment = new EmailAttachment(); attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif")); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Apache logo"); attachment.setName("Apache logo"); // Create the email message // 设置邮件信息 MultiPartEmail email = new MultiPartEmail(); // 设置邮件服务器 email.setHostName("mail.163.com"); //设置认证用户名和密码 simpleEmail.setAuthentication("xxx@163.com", "xxx"); // 设置收件人邮箱地址 email.addTo("xxx@qq", "xuyi"); // 设置发件人邮箱地址 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();
HtmlEmail(内嵌html代码的Email)
// 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();
ImageHtmlEmail(内嵌图片的html代码Email)
// 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();
总结
使用apache commons Email组件来发送邮件非常的简单便捷,并且可以很轻松的实现几种不同类型邮件发送。
参考
1、http://commons.apache.org/proper/commons-email/userguide.html
0 0
- Apache Commons Email组件介绍使用
- 使用Apache Commons-email组件发送邮件
- 使用Apache Commons-email组件发送邮件
- Apache Commons Email使用
- Apache Commons IO组件介绍使用
- Apache Commons Codec组件介绍使用
- Apache Commons BeanUtils组件介绍使用
- Apache Commons lang组件介绍使用
- Apache Commons Collections组件介绍使用
- Apache Commons IO组件介绍使用
- Apache Commons Email 简单使用
- Apache Commons Email 简单使用
- Apache Commons Email使用心得
- 利用Apache commons-email组件发送邮件
- apache commons学习系列之Email组件
- Apache Commons Email的使用(一)
- 使用Apache commons轻松发送Email
- Apache commons组件的使用
- 【Git】'ssh-keygen' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
- 关于解析图片
- centos 安装 mysql
- LeetCode - 169. Majority Element
- 派生类中构造函数与虚构函数的研究
- Apache Commons Email组件介绍使用
- 百练 10 迷宫问题
- leetcode No49. Group Anagrams
- Belarusian State University & Kazakhstan Contest D Contest
- STM32学习笔记之中断向量表,MDK程序启动分析
- 以下代码共调用多少次拷贝构造函数:
- pair与排序
- Java学习笔记1——集合
- 【一天一道LeetCode】#223. Rectangle Area