【commons】邮件发送工具——commons-email
2017-10-13 10:34
459 查看
一、概述
直接通过官网的overview进行了解,一句话概括如下:Commons Email aims to provide a API for sending email. It is built on top of the Java Mail API, which it aims to simplify.
Commons Email 旨在提供发送邮件的API,它是简历在Java Mail之上的,目的是简化它。
二、入门
1.获取commons-email采用maven构建的坐标如下:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.4</version> </dependency>
如需下载Jar包,请登陆官网下载:http://commons.apache.org/proper/commons-email/download_email.cgi
2.入门程序
发送简单文本邮件
public static void main(String[] args) throws Exception{ Email email = new SimpleEmail(); email.setHostName("smtp.qq.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator("775992759@qq.com", "你的授权码")); email.setSSLOnConnect(true); email.setFrom("775992759@qq.com"); email.setSubject("TestMail"); email.setMsg("This is a test mail ... :-)"); email.addTo("2126802032@qq.com"); email.send(); System.out.println("发送成功!"); }
关于授权码的设置请参见QQ邮箱(其它邮箱的SMTP的开启设置请使用搜索引擎)的帮助:http://service.mail.qq.com/cgi-bin/help?id=28
结果:
发送带附件的邮件:
public static void main(String[] args) throws Exception{ // Create the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setPath("D:\\test\\1.jpg"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Picture of John"); attachment.setName("John.jpg"); // Create the email message MultiPartEmail email = new MultiPartEmail(); email.setHostName("smtp.qq.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator("775992759@qq.com", "你的授权码")); email.setSSLOnConnect(true); email.addTo("2126802032@qq.com"); email.setFrom("775992759@qq.com"); email.setSubject("The picture"); email.setMsg("Here is the picture you wanted"); // add the attachment email.attach(attachment); // send the email email.send(); System.out.println("发送成功!"); }
结果:
更多,请参见官网入门介绍:http://commons.apache.org/proper/commons-email/userguide.html
相关文章推荐
- 用java开发Email工具之发送邮件 (1)作者:冯睿
- 利用Commons-email组件发送邮件
- ci框架(codeigniter)Email发送邮件、收件人、附件、Email调试工具
- 通过apache 的 commons-email 包发送邮件
- java实现发送邮件可多个附件内嵌图片-commons-email使用
- shell下发送邮件工具sendEmail的用法详解
- commons-email 轻松发送无乱码邮件
- 使用commons-email-1.3.2.jar包发送邮件
- Android中利用commons-email.jar和 mail.jar 发送和接受邮件
- commons-email 发送邮件
- commons-email实现发送邮件及遇到的问题
- linux sendEmail工具发送邮件
- 使用commons-email-1.3.2.jar包发送邮件
- 采用apache的commons-email包发送邮件死锁
- java利用commons-email发送邮件并进行封装
- 利用apache的commons-email发送邮件Demo
- Commons: 封装Apache Commons Email简化邮件发送
- JavaMail、Commons Email发送邮件 出现结果后到的文件是Base64的问题的解决办法
- 用java开发Email工具之发送邮件
- 使用Commons Email发送邮件