程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> [JAVA100例]047、使用JavaMail發送郵件

[JAVA100例]047、使用JavaMail發送郵件

編輯:關於JAVA

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
* <p>Title: 使用javamail發送郵件</p>
* <p>Description: 演示如何使用javamail包發送電子郵件。這個實例可發送多附件</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: Mail.java</p>
* @version 1.0
*/
public class Mail {
String to = "";//收件人
String from = "";//發件人
String host = "";//smtp主機
String username = "" ;
String password = "" ;
String filename = "";//附件文件名
String subject = "";//郵件主題
String content = "";//郵件正文
Vector file = new Vector();//附件文件集合
/**
*<br>方法說明:默認構造器
*<br>輸入參數:
*<br>返回類型:
*/
public Mail(){
}
/**
*<br>方法說明:構造器,提供直接的參數傳入
*<br>輸入參數:
*<br>返回類型:
*/
public Mail(String to,String from,String smtpServer,String username,String password,String subject,String content){
 this.to = to;
 this.from = from;
 this.host = smtpServer;
 this.username = username;
 this.password = password;
 this.subject = subject;
 this.content = content;
}
/**
*<br>方法說明:設置郵件服務器地址
*<br>輸入參數:String host 郵件服務器地址名稱
*<br>返回類型:
*/
public void setHost(String host){
 this.host = host;
}
/**
*<br>方法說明:設置登錄服務器校驗密碼
*<br>輸入參數:
*<br>返回類型:
*/
public void setPassWord(String pwd){
 this.password = pwd;
}
/**
*<br>方法說明:設置登錄服務器校驗用戶
*<br>輸入參數:
*<br>返回類型:
*/
public void setUserName(String usn){
 this.username = usn;
}
/**
*<br>方法說明:設置郵件發送目的郵箱
*<br>輸入參數:
*<br>返回類型:
*/
public void setTo(String to){
 this.to = to;
}
/**
*<br>方法說明:設置郵件發送源郵箱
*<br>輸入參數:
*<br>返回類型:
*/
public void setFrom(String from){
 this.from = from;
}
/**
*<br>方法說明:設置郵件主題
*<br>輸入參數:
*<br>返回類型:
*/
public void setSubject(String subject){
 this.subject = subject;
}
/**
*<br>方法說明:設置郵件內容
*<br>輸入參數:
*<br>返回類型:
*/
public void setContent(String content){
 this.content = content;
}
/**
*<br>方法說明:把主題轉換為中文
*<br>輸入參數:String strText
*<br>返回類型:
*/
public String transferChinese(String strText){
 try{
  strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");
 }catch(Exception e){
  e.printStackTrace();
 }
 return strText;
}
/**
*<br>方法說明:往附件組合中添加附件
*<br>輸入參數:
*<br>返回類型:
*/
public void attachfile(String fname){
 file.addElement(fname);
}
/**
*<br>方法說明:發送郵件
*<br>輸入參數:
*<br>返回類型:boolean 成功為true,反之為false
*/
public boolean sendMail(){
//構造mail session
 Properties props = System.getProperties();
 props.put("mail.smtp.host",host);
 props.put("mail.smtp.auth","true");
 Session session=Session.getDefaultInstance(props, new Authenticator(){
  public PasswordAuthentication getPasswordAuthentication(){
  return new PasswordAuthentication(username,password);
  }
 });

 try {
  //構造MimeMessage 並設定基本的值
  MimeMessage msg = new MimeMessage(session);
  msg.setFrom(new InternetAddress(from));
  InternetAddress[] address={new InternetAddress(to)};
  msg.setRecipients(Message.RecipientType.TO,address);
  subject = transferChinese(subject);
  msg.setSubject(subject);

  //構造Multipart
  Multipart mp = new MimeMultipart();

  //向Multipart添加正文
  MimeBodyPart mbpContent = new MimeBodyPart();
  mbpContent.setText(content);
  //向MimeMessage添加(Multipart代表正文)
  mp.addBodyPart(mbpContent);

  //向Multipart添加附件
  Enumeration efile=file.elements();
  while(efile.hasMoreElements()){

   MimeBodyPart mbpFile = new MimeBodyPart();
   filename=efile.nextElement().toString();
   FileDataSource fds = new FileDataSource(filename);
   mbpFile.setDataHandler(new DataHandler(fds));
   mbpFile.setFileName(fds.getName());
   //向MimeMessage添加(Multipart代表附件)
   mp.addBodyPart(mbpFile);
}

  file.removeAllElements();
  //向Multipart添加MimeMessage
  msg.setContent(mp);
  msg.setSentDate(new Date());
  //發送郵件
  Transport.send(msg);

 } catch (MessagingException mex) {
  mex.printStackTrace();
  Exception ex = null;
  if ((ex=mex.getNextException())!=null){
   ex.printStackTrace();
  }
  return false;
 }
 return true;
}
/**
*<br>方法說明:主方法,用於測試
*<br>輸入參數:
*<br>返回類型:
*/
public static void main(String[] args){
 Mail sendmail = new Mail();
 sendmail.setHost("smtp.sohu.com");
 sendmail.setUserName("du_jiang");
 sendmail.setPassWord("31415926");
 sendmail.setTo("[email protected]");
 sendmail.setFrom("[email protected]");
 sendmail.setSubject("你好,這是測試!");
 sendmail.setContent("你好這是一個帶多附件的測試!");
 //Mail sendmail = new Mail("[email protected]","[email protected]","smtp.sohu.com","du_jiang","31415926","你好","胃,你好嗎?");
 sendmail.attachfile("c:\\test.txt");
 sendmail.attachfile("DND.jar");
 sendmail.sendMail();
}
}//end

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved