1、java.util.Properties类
java.util.Properties是对properties这类配置文件的映射。支持key-value类型和xml类型两种。
key-value类型的配置文件大略长这样:
#测试环境配置:平台路径配置
jstrd_home=D:/TMS2006/webapp/tms2006/WEB-INF/
dbPort = localhost
databaseName = myd
dbUserName = root
“#”打头的是注释行,Properties会忽略注释。允许只有key没有value。
#测试环境配置:平台路径配置
jstrd_home=D:/TMS2006/webapp/tms2006/WEB-INF/
dbPort =
databaseName
这种情况下,value会被set成null。
properties类实现了Map接口,所以很明显,他是用map来存储key-value数据,所以也注定存入数据是无序的,这个点需要注意。只能通过key的方式来get对应value。
针对key-value这种配置文件,是用load方法就能直接映射成map.
public class LoadSample {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis =
new FileInputStream("sample.properties");
prop.load(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " +
prop.getProperty("foo"));
}
}
第六行的load方法直接生产一个内存map,第九行就能get到对应的value了,简单快捷。
这里的第七行list方法是一个输出方法,这边是输出到console,也可以输出到文件等,就能实现内存写入配置文件了。
比如这样:
//通过list 方法将Properties写入Properties文件
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.util.Properties;
public class Test {
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("id","dean");
p.setProperty("password","123456");
try{
PrintStream fW = new PrintStream(new File("e:\\test1.properties"));
p.list(fW );} catch (IOException e) {
e.printStackTrace();
}
}
}
这样就能把内存中的properties对象写入到文件中了。
详情请见:http://www.cnblogs.com/lingiu/p/3468464.html
JavaMail API使用上非常灵活,比如,服务器信息可以设置到Session中,也可以设置到Transport中,收件人可以设置到Message中,也可以设置到Transport中,如何使用,取决于我们应用程序中的实际情况。
2、javax.mail.Session
Session用于收集JavaMail运行过程中的环境信息,它可以创建一个单例的对象,也可以每次创建新的对象,Session没有构造器,只能通过如下方法创造实例:
(1)、static Session getDefaultInstance(Properties props)
Get the default Session object.
(2)、static Session getDefaultInstance(Properties props,Authenticator authenticator)
Get the default Session object.
(3)、static Session getInstance(Properties props)
Get a new Session object.
(4)、static Session getInstance(Properties props,Authenticator authenticator)
Get a new Session object.
getDefaultInstance得到的始终是该方法初次创建的缺省的对象,而getInstance得到的始终是新的对象。
何谓getDefaultInstance?
从处理流程中可以看出,首先是从缓存中查找是否有properties存在
如果存在,则加载默认的properties
如果不存在才加载用户自己定义的properties,
所以当一个应用为每个用户独立创建properties的时候,还是应该调用getInstance,
除非你希望有一个默认的properties让用户使用
问 题找到了,因为它会首先去内存和系统文件中去找properties,所以不管我在页面改几次数据,其实在后台中生成session时都和系统启动时的一 样,所以在MailUtil.sendTextMessage(mailSession,RptTaskMailConfig.Sender,
receiver,null,mail_subject,mail_content,”GB2312”,null);时session里的sender和传入的sender不一致,因此出错,修改Session.getDefaultInstance(props, authentic);为Session.getInstance(props, authentic);后,可以正确发送修改帐号,密码等资料后的邮件.OK
最后再补充一下,session.getdefaultinstance和getinstance的区别 :
如果想要同时使用两个帐号发送javamail,比如使用1@a.com发送1#邮件,使用2@a.com发送2#邮件,这时候,你就需要同时创建两个java.mail.Session对象。但是如果你仍然使用Session.getDefaultInstance创建session对象的话,你会发现,第二个username:2@a.com创建的session永远都和第一个一样,这是为什么呢?因为,getDefaultInstance是真正单例模式,而且,里面的username和password属性是final型的,无法更改。所以,你会发现两封email都是由1@a.com发出来的。所以这个时候你要使用javax.mail.Session.getInstance()方法来创建session对象。
3、javax.mail.internet.MimeMessage
Message是邮件的载体,用于封装邮件的所有信息,Message是一个抽象类,已知的实现类有MimeMessage。
发件人
abstract void setFrom()
// Set the “From” attribute in this Message.
abstract void setFrom(Address address)
// Set the “From” attribute in this Message.
现在大多数SMTP服务器要求发件人与连接账户必须一致,否则会抛出验证失败的异常,这样是防止用户伪装成其它人的账户恶意发送邮件。
收件人/抄送人/暗送人
void setRecipient(Message.RecipientType type,Address address)
// Set the recipient address.
abstract void setRecipients(Message.RecipientType type,Address[] addresses)
// Set the recipient addresses.
第一个方法设置单个人,第二个方法设置多个人。方法中第一个参数涉及到另一个类RecipientType,该类是Message的静态内部类,期内有三个常量值:
static Message.RecipientType BCC
// The “Bcc” (blind carbon copy) recipients.
static Message.RecipientType CC
// The “Cc” (carbon copy) recipients.
static Message.RecipientType TO
// The “To” (primary) recipients. TO - 收件人;CC - 抄送人;BCC - 暗送人。
回复人
void setReplyTo(Address[] addresses)
// Set the addresses to which replies should be directed. 设置收件人收到邮件后的回复地址。
标题
abstract void setSubject(String subject)
Set the subject of this message. 设置邮件标题/主题。
内容
void setContent(Multipart mp)
//This method sets the given Multipart object as this message’s content.
void setContent(Object obj,String type)
// A convenience method for setting this part’s content.
void setText(String text)
// A convenience method that sets the given String as this part’s content with a MIME type of “text/plain”.
设置邮件文本内容、HTML内容、附件内容。