2014/7/18

[Java]使用JavaMail寄信,以html的畫面呈現郵件

需要匯入mail.jar
JavaMail API:
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.7-oth-JPR
JavaMail API doc:
https://javamail.java.net/nonav/docs/api/
簡略說明一下這程式在做什麼
讀取一個html檔,並把html的code寫進郵件內文並寄出,收件者看到的畫面會是一個html的畫面。


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;


public class Emailtest {

 public static void main(String[] args) throws IOException {
  String htmlCode = "";
        String myreadline;
        FileInputStream fis = new FileInputStream("D:/test.html");
        InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
        BufferedReader br= new BufferedReader(isr) ;
        while (br.ready()) {  
         myreadline = br.readLine();  
         htmlCode=htmlCode+myreadline;  
        } 
        br.close();   
        isr.close();
        fis.close();
  
  String mailserver = "mail伺服器";
  String fromName="寄件者名子";
  String from = "寄件者email";
  String to = "收件者email";
  String subject = "主旨";
  
  //1為需驗證 0為不需驗證
  String emailauth = "1";
  String emailAcct = "帳號";
  String emailPwd = "密碼";


  sendEmail(mailserver, from, fromName,to, subject, htmlCode, emailAcct, emailPwd,
    emailauth);
 }

 public static void sendEmail(String mailserver, String from,String fromName, String to,
   String subject, String htmlCode, final String emailAcct,
   final String emailPwd, String emailauth) throws IOException {

  InternetAddress[] address = null;
  Session mailSession=null;
  try {

   Properties props = new Properties(); 
   props.put("mail.host", mailserver);
   if("1".equals(emailauth)){
    props.put("mail.smtp.auth", "true");
   }
   props.put("mail.transport.protocol", "smtp");
   if ("1".equals(emailauth)) {
    mailSession = Session.getInstance(props,
      new javax.mail.Authenticator() {
       protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(emailAcct,emailPwd);
       }
      });
   }
   
   
   if("0".equals(emailauth)){
    mailSession = Session.getDefaultInstance(props,null); 
   }

   //讀取html code
   MimeBodyPart textPart = new MimeBodyPart();
   StringBuffer html = new StringBuffer();
   html.append(htmlCode);
   textPart.setContent(html.toString(), "text/html; charset=UTF-8"); 
   
   Multipart email = new MimeMultipart();
   email.addBodyPart(textPart);
   
   // 產生整封 email 的主體 message
   Message message = new MimeMessage(mailSession);
   //設定郵件內文
   message.setContent(email);
   // 設定主旨
   message.setSubject(subject);
   //設定日期
   message.setSentDate(new Date());
   //設定寄件者
   message.setFrom(new InternetAddress(from,fromName));
   //設定收件者
   address = InternetAddress.parse(to,false);
   message.setRecipients(Message.RecipientType.TO, address);
   //寄出郵件
   Transport.send(message);
  } catch (AddressException e) {
   e.printStackTrace();
  } catch (NoSuchProviderException e) {
   e.printStackTrace();
  } catch (MessagingException e) {
   e.printStackTrace();
  }
 }
}

沒有留言:

張貼留言