java郵件傳送

水手發表於2018-11-14

工作中有java郵件傳送 用了原生的javax.mail 分享一下

// MailUtils
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.ibatis.io.Resources;


public class MailUtils {
  public static String sendMail(String title, String content, String ToAdress,
      String Toname, String fromName) {
    String sendStatic = MailStatus.FAIL.getTitle();
    
    
    
    try {
      Properties propsMail = Resources.getResourceAsProperties("jdbc.properties");
      String host = propsMail.getProperty("mailHost");
      String port = propsMail.getProperty("mailPort");
      /*
       * Properties是一個屬性物件,用來建立Session物件
       */
      Properties props = new Properties();
      props.setProperty("mail.smtp.host", host);
      props.setProperty("mail.smtp.port", port);
      props.setProperty("mail.smtp.auth", "true");
      props.setProperty("mail.smtp.ssl.enable", "false");// "true"
      props.setProperty("mail.smtp.connectiontimeout", "5000");
      
      final String user = propsMail.getProperty("mailUserName");// 使用者名稱
      final String pwd = propsMail.getProperty("mailUserPass");// 密碼
      /*
       * Session類定義了一個基本的郵件對話。
       */
      Session session = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
          // 登入使用者名稱密碼
          return new PasswordAuthentication(user, pwd);
        }
      });
      session.setDebug(true);
      /*
       * Transport類用來傳送郵件。 傳入引數smtp,transport將自動按照smtp協議傳送郵件。
       */
      Transport transport = session.getTransport("smtp");
      transport.connect(host, user, pwd);

      /*
       * Message物件用來儲存實際傳送的電子郵件資訊
       */
      MimeMessage message = new MimeMessage(session);
      message.setSubject(title);
      // 訊息傳送者接收者設定(發件地址,暱稱),收件人看到的暱稱是這裡設定的
      message.setFrom(new InternetAddress(user, fromName));
      message.addRecipients(Message.RecipientType.TO,
          new InternetAddress[] { new InternetAddress(ToAdress, Toname), });
      message.saveChanges();

      // 設定郵件內容及編碼格式
      // 後一個引數可以不指定編碼,如"text/plain",但是將不能顯示中文字元
      message.setContent(content, "text/html;charset=utf-8");
      Transport.send(message);

      transport.close();
      sendStatic = MailStatus.SUCCESS.getTitle();
    } catch (Exception e) {
      sendStatic = MailStatus.FAIL.getTitle();
      System.exit(0);
    }
    return sendStatic;
  }

  public static void main(String[] args) {
    
    MailUtils ma = new MailUtils();
    ma.sendMail("標題", "你好你的郵件", "xxxx@xxxx", "傳送姓名", "系統提示");
    System.exit(0);
  }

}

複製程式碼

public enum MailStatus {
  SUCCESS("SUCCESS", "1"), FAIL("FAIL", "0");

  // 成員變數
  private String title;
  private String content;

  // 構造方法
  private MailStatus(String title, String content) {
    this.title = title;
    this.content = content;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }
}

複製程式碼

相關文章