1. 程式人生 > >【JavaMail開發總結】配置檔案形式--傳送郵件程式

【JavaMail開發總結】配置檔案形式--傳送郵件程式

        在上一篇中簡單的實現了一個傳送郵件功能的程式,今天用配置檔案的方式來實現,大致思路一致,示例程式碼如下:

package com.javamail.test.demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailUtil {
 /**
  * 傳送郵件
  * @param to 
  *   收信人
  * @param subject
  *   郵件主題
  * @param content
  *   郵件內容
  * @throws IOException
  * @throws AddressException
  * @throws MessagingException
  */
 public static void send_email(String to, String subject, String content)
               throws IOException, AddressException, MessagingException {
     Properties properties = new Properties();
     InputStream resourceAsStream = null;
     try {
         // 獲取配置檔案
         resourceAsStream = EmailUtil.class.getClassLoader().getResourceAsStream("email.properties");
         properties.load(resourceAsStream);
     } finally {
          if (resourceAsStream != null) {
              resourceAsStream.close();
          }
     }
     // System.err.println("properties:"+properties);
     properties.put("mail.smtp.host", properties.get("mail.smtp.host"));
     properties.put("mail.smtp.port", properties.get("mail.smtp.port"));
     properties.put("mail.smtp.auth", "true");
     Authenticator authenticator = new Email_Authenticator(properties.get(
             "username").toString(), properties.get("password").toString());
     javax.mail.Session sendMailSession = javax.mail.Session
              .getInstance(properties, authenticator);
     MimeMessage mailMessage = new MimeMessage(sendMailSession);
     //設定發信人
     mailMessage.setFrom(new InternetAddress(properties.get("username")
              .toString(),"***"));
     // 設定收信人,Message.RecipientType.TO 收信人,Message.RecipientType.CC抄送人
     mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
               to,properties.get("username").toString()));
     //主題
     mailMessage.setSubject(subject, "UTF-8");
     //設定郵件傳送日期
     mailMessage.setSentDate(new Date());
     // MiniMultipart類是一個容器類
     Multipart mainPart = new MimeMultipart();
     // 建立一個郵件體物件
     BodyPart html = new MimeBodyPart();
     html.setContent(content.trim(), "text/html; charset=utf-8");
     mainPart.addBodyPart(html);
     mailMessage.setContent(mainPart);
     Transport.send(mailMessage);
  }
}
//通過建構函式傳入身份驗證資訊
class Email_Authenticator extends Authenticator {
    String userName = null;
    String password = null;
    public Email_Authenticator() {
    }
    public Email_Authenticator(String username, String password) {
        this.userName = username;
        this.password = password;
    }
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
}



 配置檔案如下(一般放在src目錄下):

#email Configuration
#need account and password
#author cash
#host
mail.smtp.host=smtp.qq.com
#port
mail.smtp.port=25
#your email account
username=***@qq.com
#your email password
password=***


"mail.smtp.host=smtp.qq.com"指的是用qq.com的smtp伺服器,除此之外還有163.com等等

"mail.smtp.port=25"指的是SMTP伺服器埠是25