javamail WEB版

在做這個實驗之前,我們應該準備一些包,你可以到這裏下載點擊打開鏈接http://download.csdn.net/detail/wohenai791515672/5673893


一.郵件服務器按功能可以劃分爲兩種類型,SMTP與POP3/IMAP。SMTP用來替用戶發送郵件和接收外面發送給本地用戶的郵件;POP3/IMAP用來幫用戶讀取郵件。在瞭解郵件服務器後,我們應該知道用戶要與郵件服務器交互,應該先登錄此服務器。但是郵件服務器與郵件服務器之間是不需要登錄驗證的。

二.對於發送郵件基本原來實現,從Session對象中獲得實現了Transport對象,使用Session對象創建Transport對象,並調用Message對象的方法封裝郵件數據。連接指定的SMTP服務器,調用Transport對象中的郵件發送方法發送Message對象中封裝郵件數據。(實現時要注意引入mail包)



//創建Session對象
Session session=Session.getDefaultInstance(new Properties());
//創建MimeMessage
MimeMessage  msg=new MimeMessage(Session);
//可以設置郵件發送人,主題,日期
msg.setForm();
msg.setSentDate();
//設置郵件的內容
msg.setContent();
msg.saveMessage();
//獲取Transport對象
Transport   transport=session.getTransport();
Transport.connect(server,user,pass);
transport.sendMessage(message);transport.close();

Authenticator類的應用

public class myAuthenticator extends Authenticator{

	String username=null;
	String password=null;
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	public myAuthenticator(String userName,String passWord)
	{
		
		this.username=userName;
		this.password=passWord;
		
	}
	
	Protected PasswordAuthentication  getPasswordAuthenticator()
	{
		
		Return new PasswordAuthentication(username,password);
		
		}

可以爲郵件發送程序配置代理

(1)提高訪問速度(2)代理服務器可以起到防火牆的作用(3)通過代理服務器可以

訪問一些不能直接訪問的網站。(4)具有一定的隱身作用。

三.對於郵件接收的實現,從session中獲得實現了某種郵件發送協議的store對象

以某個郵箱帳戶的身份連接上POP3IMAP服務器,調用StoregetFolder的方法

,獲取代表該帳戶的郵箱中的某個郵件夾的Folder對象,調用Folder對象中的getMessage()或getMessage方法,獲取郵件夾中每一封郵件,每一封郵件以一個Message對象返回。


roperties prop=new Properties();
       prop.setProperty("mail.store.protocol",protocol);
       prop.setProperty("mail.pop3.host",pop3Server);
       Session mailSession=Session.getDefaultInstance(prop,null);
       mailSession.setDebug(false);
       Store store=mailSession.getStore(protocol);
       store.connect(pop3Server,username,password);//pop3服務器的登錄認證
       Folder folder =store.getFolder("inbox");
       folder.open(Folder.READ_WRITE);
       Message[] messages=folder.getMessages();
       for(int i=0;i<messages.length;i++)
       {
         String subject=messages[i].getSubject();
         Address[] froms=messages[i].getFrom();
         Address from=froms[0];
         System.out.println("第"+(i+1)+"封郵件的主題:"+subject+"\t發件人地址爲:"+from);
       }

四.在郵件發送過程中要發送圖片與附件。參考http://developer.51cto.com/art/200907/133797.htm

爲郵件添加附件和內嵌圖片,這需要在郵件撰寫頁面中選擇郵件附件的多個文件和作爲內嵌圖片的多個圖片文件,這些文件必須上傳到web服務器後,web應用程序才能將他們的內容插入到整份MIME郵件 消息中,這涉及文件編程上傳處理。對於文件上傳處理,我們要注意的是路徑一定要寫正確。我這時參考了網上的代碼,但是它的有問題,因爲savefile路徑不對,導致文件不能上傳。

//一定要加“/”,new File(saveDir+"/"+fileName)
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+"/"+fileName)));//獲得文件輸出流 

因爲我們新建文件夾時,可能給的路徑是 String savePath = "d:/savefiles/";

但是通過 saveDir = new File(savePath);路徑變成savePath = "d:/savefiles

所以我們一定要加“/”。

,用js打開文件對話框

<html> 
<head> 
<script type="text/javascript"> 
function openFileSelect()
{
 var obj = document.getElementById('test');
 obj.click();
}
</script> 
</head> 
<body> 
<img src="liuyaf.JPG" οnclick='openFileSelect();'/>
<input type='file' name='tttt' value='' id='test' style='display:none'>
</body> 
</html>








發佈了60 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章