JavaMail快速发送
邮件相关协议
smtp:简单的邮件传输协议(发送)
pop3:邮件协议第三版(接受)
一般写一封邮件的步骤(比较麻烦,不做介绍)
1、导包:mail.jar和activition.jar
2、得到session
3、使用MimeMessage对象填写邮件内容
4、使用Transport类发送邮件
二、使用itcast-tools工具包快速发送邮件
//解析src下的配置文件email_template.properties
Properties props=new Properties();
props.load(this.getClass().getClassLoader()
.getResourceAsStream("email_template.properties"));
String host=props.getProperty("host"); //得到主机
String uname=props.getProperty("uname"); //得到用户名
String pwd=props.getProperty("pwd"); //得到密码
String from =props.getProperty("from"); //得到发件人
String subject=props.getProperty("subject"); //得到邮件主题
String content=props.getProperty("content"); //得到邮件内容
String to=user.getEmail(); //得到收件人,注册人填写的地址
//由于邮件内容中要传入参数,使用占位符{0}
content=MessageFormat.format(content, user.getCode()); //替换content中站位符{0}
//使用工具包itcast-tools中的MailUtils类去创建session对象
Session session=MailUtils.createSession(host, uname, pwd);
//同样是使用Mail类中的构造器,去写一封邮件
Mail mail=new Mail(from, to, subject, content);
//直接发送邮件
MailUtils.send(session, mail);