网站注册功能完成后回向用户邮箱发送一封邮件。邮件也是html页面。因为页面比较复杂,我通过io,再对读取的字符串进行处理然后发送。
首先,写一个读取页面的工具类。
public static String readHTML() throws IOException {
String spath ="C:/Users/jindongzp/Desktop/accountActive/accountActive.html";
InputStreamReader isReader = null;
BufferedReader bufReader = null;
StringBuffer buf = new StringBuffer();
try {
File file = new File(spath);
isReader = new InputStreamReader(new FileInputStream(file), "utf-8");
bufReader = new BufferedReader(isReader, 1);
String data;
while((data = bufReader.readLine())!= null) {
buf.append(data);
}
} catch (Exception e) {
//TODO 处理异常
} finally {
//TODO 关闭流
isReader.close();
bufReader.close();
}
// System.out.print(buf.toString());
return buf.toString();
}
然后,使用javamail发邮件
public void test222() {
String from="XXX@kiwinano.com";
String to="若干年后@126.com";
//下方的代码是设置发送e-mail服务器的
String smtpServer="smtpout.secureserver.net";
String subject="Hello here is your reset comformation e-mail from Kiwinano.ca";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","80");//远程服务器无法连到邮件服务器的25端口,改用80端口;
MailAuthenticator autherticator=null;
autherticator = new MailAuthenticator("info@kiwinano.com","若干年后");
Session session = Session.getDefaultInstance(props,autherticator);
MimeMessage msg = new MimeMessage(session);
try{
msg.setFrom(new InternetAddress(from));
msg.setRecipient(MimeMessage.RecipientType.TO , new InternetAddress(to));
msg.setSubject(subject);
msg.setSentDate(new Date());
String readHTML = TestTest2.readHTML();
readHTML = readHTML.replace("/$","若干年后@126.com;");
readHTML = readHTML.replace("/*","若干年后@126.com;");
msg.setContent(readHTML, "text/html; charset=utf-8");
System.out.print(readHTML);
Transport.send(msg);
System.out.println("成功发送邮件......");
}catch(Exception se){
se.printStackTrace();
}
}
在这里我把字符串里面的“/$”和“/*”进行了替换处理。
String readHTML = TestTest2.readHTML();
readHTML = readHTML.replace("/$","若干年后@126.com;");
readHTML = readHTML.replace("/*","若干年后@126.com;");