java mail

package com.javaMail;

import java.net.InetAddress;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMail {
 private String to = ""; // 收件人
 private String from = ""; // 發件人
 private String host = ""; // SMTP主機
 private String userMailName = ""; // 發件人郵箱用戶名
 private String userMailPassword = ""; // 發件人郵箱密碼
 private String fileName = ""; // 附件文件名
 private String subject = ""; // 郵件主題
 private String content = ""; // 郵件正文
 private Vector<String> file = new Vector<String>(); // 附件文件集合

 public String getTo() {
  return to;
 }

 public void setTo(String to) {
  this.to = to;
 }

 public String getFrom() {
  return from;
 }

 public void setFrom(String from) {
  this.from = from;
 }

 public String getHost() {
  return host;
 }

 public void setHost(String host) {
  this.host = host;
 }

 public String getUserMailName() {
  return userMailName;
 }

 public void setUserMailName(String userMailName) {
  this.userMailName = userMailName;
 }

 public String getUserMailPassword() {
  return userMailPassword;
 }

 public void setUserMailPassword(String userMailPassword) {
  this.userMailPassword = userMailPassword;
 }

 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }

 public String getSubject() {
  return subject;
 }

 public void setSubject(String subject) {
  this.subject = subject;
 }

 public String getContent() {
  return content;
 }

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

 public Vector<String> getFile() {
  return file;
 }

 public void setFile(Vector<String> file) {
  this.file = file;
 }

 /**
  * 往附件組合中添加附件
  */
 public void addfile(String fname) {
  file.addElement(fname);
 }

 /**
  * 默認構造器
  */
 public JavaMail() {
 }

 /**
  * 構造器,提供直接的參數傳入
  */
 public JavaMail(String to, String from, String smtpServer, String userName,
   String password, String subject, String content) {
  this.to = to;
  this.from = from;
  this.host = smtpServer;
  this.userMailName = userName;
  this.userMailPassword = password;
  this.subject = subject;
  this.content = content;
 }

 /**
  * 發送郵件
  *
  * @return 發送成功時返回 true
  * @throws Exception
  * @throws Exception
  */
 public void sendMail() throws Exception {
  Properties properties = new Properties();
  try {
   if (host != null)
    properties.put("mail.smtp.host", host);
   else if (properties.getProperty("mail.smtp.host") == null)
    properties.put("mail.smtp.host", InetAddress.getLocalHost()
      .getHostName());
   properties.put("mail.smtp.port", 25);
   properties.put("mail.smtp.auth", true);
  } catch (Exception ex) {
   throw new Exception(ex.getMessage());
  }
  MyAuthenticator myauth = new MyAuthenticator(this.userMailName,
    this.userMailPassword);
  // 創建JavaMail Session
  Session session = Session.getDefaultInstance(properties, myauth);
  // 根據session創建一個郵件消息
  MimeMessage msg = new MimeMessage(session);
  msg.isExpunged();
  Address address = null;
  try {
   if (this.from != null) {
    address = new InternetAddress(this.from);
    // 創建郵件發送者地址
    Address[] fromAddresses = { address };
    // 設置郵件消息的發送者
    msg.addFrom(fromAddresses);
   } else
    throw new Exception("未指定發件人地址");
   if (this.to != null) {
    // 創建郵件的接收者地址,並設置到郵件消息中
    Address toAdresses = new InternetAddress(this.to);
    // 設置郵件消息的接受者
    msg.addRecipient(Message.RecipientType.TO, toAdresses);
   } else
    throw new Exception("未指定收件人地址");
   if (content != null) {
    msg.setContent(content, "text/html ;charset=utf-8");
   } else
    msg.setText("");

   // 主題
   msg.setSubject(subject);
   // 設置郵件消息發送的時間
   msg.setSentDate(new Date());
   // 設置郵件消息的主要內容
   javax.mail.Transport.send(msg);
  } catch (Exception e) {
   throw new Exception(e);
  }

 }

 /**
  * 校驗發信人權限類
  */
 class MyAuthenticator extends javax.mail.Authenticator {
  private String strUser = "";
  private String strPwd = "";

  public MyAuthenticator() {
  }

  public MyAuthenticator(String user, String password) {
   this.strUser = user;
   this.strPwd = password;
  }

  protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication(strUser, strPwd);
  }
 }

 /**
  * 主方法,用於測試
  */
 public static void main(String[] args) {

 

 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章