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