javamail读取并发送完整的html页面
生活随笔
收集整理的這篇文章主要介紹了
javamail读取并发送完整的html页面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
網站注冊功能完成后回向用戶郵箱發送一封郵件。郵件也是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;");?
轉載于:https://www.cnblogs.com/rgnh/p/4735680.html
總結
以上是生活随笔為你收集整理的javamail读取并发送完整的html页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript继承机制
- 下一篇: 构建LAMP平台(一)(软件版本:htt