Common mail 包的使用

jopen 12年前發布 | 34K 次閱讀 Java開發 Common Mail

   在javax包中雖然定義了一些些關于發mail的類,但是用起來不是很方便,下面是我對common.mail包的一些使用筆記.

package bo;

import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL;

import javax.mail.internet.MimeUtility;

import org.apache.commons.io.IOUtils; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.commons.mail.MultiPartEmail; import org.apache.commons.mail.SimpleEmail;

public class Email { //發送普通文件的mail public void mySend() { try { SimpleEmail email = new SimpleEmail(); // 構造一個mail對象 //email.setHostName("hostname");// 設置主機名 email.addTo("mail", "my");// 設置發對象 email.setFrom("yahu@.cn.com", "my");// 設置發送人 email.setSubject("郵件測試");// 設置主題 email.setCharset("GBK");// 設置發送使用的字符集 String content = "測試內容是我自己的";// 內容

  email.setContent(content, "text/plain;charset=GBK");// 設置內容

  email.send();// 發送
 } catch (EmailException e) {
  e.printStackTrace();
 }

}

public static void main(String[] args) {
 Email email = new Email();
 email.mySend();
}

//發送帶附件的mail public void myAttachment() { MultiPartEmail email = new MultiPartEmail();// 構造一個mail對象 email.setHostName("hostname");// 設置服務器名 try { EmailAttachment attachment = new EmailAttachment();// 構造一個發送附件 attachment.setPath("C:\2.jpg");// 設置附件路徑 attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("中文");// 描述 attachment.setName(MimeUtility.encodeText("美女.png"));

  email.addTo("yahu@.cn.com", "name");// 發送對象
  email.setFrom("yahu@.cn.com", "name");// 發送人
  email.setSubject("測試");// 標題
  email.setCharset("GBK");// 使用的字符集

  String content = "美女";// 內容
  email.setMsg(content);// 設置內容
  email.attach(attachment);// 發送附件
  email.send();// 發送

 } catch (UnsupportedEncodingException e) {

  e.printStackTrace();
 } catch (EmailException e) {
  e.printStackTrace();
 }

}

付注:附件可以發送多個,只需要構靠多個EmailAttachment即可

//發送html格式的mail public void htmlEmail() { HtmlEmail email = new HtmlEmail();// 構造一個html mail對象

 email.setHostName("hope.cn");// 設置服務器名
 try {
  email.addTo("yahu@.cn.com", "my");// 設置發送對象
  email.setFrom("yahu@.cn.com", "my");// 設置發送人
  email.setSubject("測試");// 設置主題
  String mag = "紅色";//這里可以寫你的html文檔,因為本頁面我是用xml文件做數據存儲的,不能寫標簽,這里我就不寫了
  email.setHtmlMsg(mag);// 設置內容
  email.setCharset("GBK");// 設置字符集
  email.send();// 發送
 } catch (EmailException e) {

  e.printStackTrace();
 }

}

public void hEmail() {

 EmailAttachment attachment = new EmailAttachment();

 try {
  attachment.setURL(new URL(
    "url"));// 設置附件的URL
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  attachment.setDescription("附件.pdf");// 設置附件描述
  try {
   attachment.setName(MimeUtility.encodeText("附件.pdf"));
  } catch (UnsupportedEncodingException e1) {
   e1.printStackTrace();
  }
  HtmlEmail hmail = new HtmlEmail();// 構造mail對象
  hmail.setHostName("hope.cn");// 設置服務器
  hmail.addTo("yahu@.cn.com", "my");// 設置發送對象
  hmail.setFrom("yahu@.cn.com", "my");// 設置發送人
  hmail.setSubject("測試");// 設置標題

  URL url = new URL(
    "url");// 構造一個UTL

  InputStream in = (InputStream) url.getContent();// 從URL中獲得輸出流
  String msg = IOUtils.toString(in);// 獲得輸出流的內容

  hmail.setHtmlMsg(msg);// 設置html內容
  hmail.setCharset("GBK");// 設置字符集
  hmail.attach(attachment);// 設置附件
  hmail.send();// 發送

 } catch (MalformedURLException e) {
  e.printStackTrace();
 } catch (EmailException e) {
  e.printStackTrace();
 } catch (IOException e) {
  // TODO 自動生成 catch 塊
  e.printStackTrace();
 }

}

}</pre>
 
   本代碼中使用了commons.io軟件包.</span></strong>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!