文章标题+路径问题
来源:互联网 发布:linux系统运维面试题 编辑:程序博客网 时间:2024/05/18 03:40
前几天被要求加一个发送邮件的小功能,在网上搜了之后发现了commons email,commons email是Apache提供的email API,以Java Mail为基础,进一步简化了操作。这是官网,上面写的很详细,也有使用指导(http://commons.apache.org/proper/commons-email/index.html)。
接下来是要记录一下自己的使用过程,方便以后查阅。(虽然和别人记录的东西都大同小异,没办法,自己现在还没有啥创新的地方,先就酱)
上面需要配置的信息很多,可以将配置信息存在properties文件中。
mail.properties
attachmentPath = "**/**/*.jpg";attachmentDescription = "description";attachmentName = "name";userName = "username";password = "password";emailFromName = "from name";emailFromAdd = "emailAdd";emailSubject = "subject";emailMsg = "email message";emailTo = "the address to send to";
import org.apache.commons.mail.*;public class Test{ public static void main(String args[]) { Test test = new Test(); test.sendEmail(); } public void sendEmail() { //read message from properties file, just like get value by "key" Properties prop = new Properties(); //get properties file's content InputStream mailInfo = this.getClass().getResourceAsStream("/mail.properties"); prop.load(mailInfo); String attachmentPath = prop.getProperty("attachmentPath"); String attachmentName = prop.getProperty("attachmentName"); String userName = prop.getProperty("userName"); String password = prop.getProperty("password"); String emailFromName = prop.getProperty("emailFromName"); String emailFromAdd = prop.getProperty("emailFromAdd"); String attachmentDescription = prop.getProperty("attachmentDescription"); String emailMsg = prop.getProperty("emailMsg"); String emailSubject = prop.getProperty("emailSubject"); //create attachment EmailAttachment attachment = new EmailAttachment(); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription(attachmentDescription); attachment.setPath(attachmentPath); attachment.setName(attachmentName); //create email Email email = new SimpleEmail(); email.setHostName("smtp.googlemail.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator(userName,password)); email.setSSLOnConnect(true); email.setFrom(emailFromAdd,emailFromName); email.setSubject(emailSubject); email.setMsg(emailMsg); email.addTo(emailTo); email.attach(attachment); email.send(); }}
- 使用的是SimpleEmail类,通过邮件发送基本的文字内容和附件
- smtp是Simple Mail Transfer Protocal,属于TCP/IP协议簇,smtp.googlemail.com是Gmail的SMTP服务器地址,端口一般是465或者587。要想启用gmail的服务器要先在google邮箱账户中手动启用POP,并且需要授权才能启用服务器发送邮件
- 支持cc、replyto,在官网中有详细的API文档可以查询使用方法
- setSSLOnConnect (SSL:Secure Sockets Layer /TSL:Transport Layer Security),设置安全连接
关于项目中读取资源文件,看到写的比较详细的博客,贴在此处。
(http://blog.csdn.net/zhanghaipeng1989/article/details/19332489)
path 不以’/’开头时默认是从此类所在的包下取资源,以’/’开头则是从ClassPath(Src根目录)根下获取。
Class.getResourceAsStream() 会指定要加载的资源路径与当前类所在包的路径一致。例如你写了一个MyTest类在包com.test.mycode 下,MyTest.class.getResourceAsStream(“name”) 会在com.test.mycode包下查找相应的资源。如果这个name是以 ‘/’ 开头的,那么就会从classpath的根路径下开始查找。
ClassLoader.getResourceAsStream() 无论要查找的资源前面是否带’/’ 都会从classpath的根路径下查找。所以: MyTest.getClassLoader().getResourceAsStream(“/name”) 和
MyTest.getClassLoader().getResourceAsStream(“name”) 的效果是一样的。
Class.getResourceAsStream(String path) :
path 不以’/’开头时默认是从此类所在的包下取资源,以’/’开头则是从ClassPath(Src根目录)根下获取。
其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。Class.getClassLoader.getResourceAsStream(String path) :
默认则是从ClassPath根下获取,path不能以’/’开头,最终是由ClassLoader获取资源。
(http://ouyangfei0426.iteye.com/blog/1020232)
src(源文件夹)
┃
┣━11.properties
┃
┗━myspider(myspider包)
┃
┣━22.properties
┗━Test.java
package myspider;import java.io.UnsupportedEncodingException;/** * * @author mark */public class Test { public static void main(String[] args) throws UnsupportedEncodingException{ Test t=new Test(); //文件名前不加“/”,则表示从当前类所在的包下查找该资源。如下则表示的是从包myspider下查找22.properties文件资源。 System.out.println("1:"+t.getClass().getResourceAsStream("22.properties"));//输出java.io.BufferedInputStream@61de33 //文件名前加了“/”,则表示从类路径下也就是从classes文件夹下查找资源,如下表示从classes文件夹下查找22.properties文件资源。 System.out.println("2:"+t.getClass().getResourceAsStream("/22.properties"));//输出null //文件名前加了“/”,则表示从类路径下也就是从classes文件夹下查找资源,如下表示从classes文件夹下查找11.properties文件资源。 System.out.println("3:"+t.getClass().getResourceAsStream("/11.properties"));//输出java.io.BufferedInputStream@14318bb System.out.println(); //当前包路径4:file:/E:/myobject/myspider/build/classes/myspider/ System.out.println("4:"+t.getClass().getResource("")); //输出当前类路径5:file:/E:/myobject/myspider/build/classes/ System.out.println("5:"+t.getClass().getResource("/")); /* * 如果类路径下的当前包有22.properties文件,则输出6:file:/E:/myobject/myspider/build/classes/myspider/22.properties * 否者输出源文件下的22.properties文件的路径,则输出:6:file:/E:/myobject/myspider/src/myspider/22.properties */ System.out.println("6:"+t.getClass().getResource("22.properties")); /* * 如果类路径下有11.properties文件,则输出7:file:/E:/myobject/myspider/build/classes/11.properties * 否者输出源文件下的11.properties文件的路径,则输出:6:7:file:/E:/myobject/myspider/src/11.properties */ System.out.println("7:"+t.getClass().getResource("/11.properties")); }}
- 文章标题+路径问题
- dedecms 获取文章标题路径
- 文章标题 棋盘问题
- 文章标题VirtualBox Centos 网络连接问题
- 文章标题关于mysql安装出错问题
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 保存退出vim编辑
- 从今天开始写博客2016-09-07
- 导航吸顶效果
- Shiro 学习笔记(1)—— Hello World
- 最大公约数问题
- 文章标题+路径问题
- Spring框架参考手册_5.0.0_中文版_Part II_3.3
- spark源码阅读笔记Dataset(一)初识Dataset
- java在pdf模板的指定位置插入图片
- Shiro 学习笔记(2)—— JdbcRealm
- 泡了个学妹,好开心
- java在word(*.docx)指定地点插入图片
- 启动hadoop集群DataNode不显示,DataNode显示NodeManager不显示
- 215. Kth Largest Element in an Array 难度:medium 类别:分治