對于
JavaMail
,最基礎的功能就是郵件的發送和接收,在這里,我先講一講郵件的發送。
在寫具體的程序前,先講一些概念。
1.
郵件的發送:如果你的郵件地址是
a@host.com
,而你要用這個郵箱發送一封郵件到
to@tohost.com
,這個發送過程是怎樣的呢,你以為是先連接到
tohost.com
這服務器上,然后把郵件發送出去嗎?其實不然。最初,你需要連接到服務器
host.com
上,當然這個連接可能需要認證,然后是發送郵件到服務器
host.com
上,關閉連接。在
host.com
上,你所發送的郵件進入發送隊列中,輪到你要發送的郵件時,
host.com
主機再聯系
tohost.com
,將郵件傳輸到服務器
tohost.com
上。
2.
一些垃圾郵件的發送:在垃圾郵件中,可能大部分的發件人的地址都是假的,這是怎么回事呢?實際上在發送這些垃圾郵件的時候,這里的
host.com
有些特別,可能
host.com
不需要對用戶進行認證,也可能發送垃圾郵件的人本來就控制著服務器
host.com
,然后控制著
host.com
向其他服務器,如
tohost.com
,發送郵件,而發送郵件的內容可以被控制,發件人的地址就可以隨便填寫。
發送郵件主要包括
3
個部分:創建連接,創建郵件體,發送郵件
JavaMail
中,是使用會話
(Session)
來管理連接的。創建一個連接,就需要創建一個會話。在會話中,有兩個重要的因素,一是會話的屬性,二是會話的認證。在我們使用
Hotmail
等郵件工具的時候,就要設置
”SMTP
服務器身份驗證
”
,也就是這里的會話的認證。
?
?
首先,創建一個連接屬性。

Properties?props?
=
?
new
?Properties();

props.put(
"
mail.smtp.host
"
,
"
smtp.126.com
"
);??
//
設置smtp的服務器地址是smtp.126.com
props.put(
"
mail.smtp.auth
"
,
"
true
"
);??????????
//
設置smtp服務器要身份驗證。
?
?
在創建一個身份驗證。身份驗證稍微復雜一點,要創建一個
Authenticator
的子類,并重載
getPasswordAuthentication
()方法
,
代碼如下:
class
?PopupAuthenticator?
extends
?Authenticator?
{

 ????
public
?PasswordAuthentication?getPasswordAuthentication()?
{

????????String?username?
=
?
"
cqhcp
"
;?
//
126郵箱登錄帳號
????????String?pwd?
=
?
"
12345
"
;?
//
登錄密碼
????????
return
?
new
?PasswordAuthentication(username,?pwd);

????}
}
創建身份驗證的實例:

PopupAuthenticator?auth?
=
?
new
?PopupAuthenticator();

?
創建會話
:
關于會話的創建,有兩種方法,具體請參看后續的文章
,
這里只簡單使用一種。
Session session = Session.getInstance(props, auth);
?
?
定義郵件地址
:
//
發送人地址
Address addressFrom = new InternetAddress("cqhcp@126.com", "George Bush");
//
收件人地址
Address addressTo = new InternetAddress("webmaster@javazy.com", "George Bush");
//
抄送地址
Address addressCopy = new InternetAddress("haocongping@gmail.com", "George Bush");
?
?
創建郵件體
:
message.setContent("Hello", "text/plain");//
或者使用
message.setText("Hello");
更詳細的信息請參看后續文章
.
message.setSubject("Title");
message.setFrom(addressFrom);
message.addRecipient(Message.RecipientType.TO,addressTo);
message.addRecipient(Message.RecipientType.CC,addressCopy);
message.saveChanges();
?
?
發送郵件的過程
:
Transport transport = session.getTransport("smtp");//
創建連接
transport.connect("smtp.126.com", "cqhcp", "12345");//
連接服務器
transport.send(message);//
發送信息
transport.close();//
關閉連接
?
?
整體程序的代碼如下
:
class PopupAuthenticator extends Authenticator {
??? public PasswordAuthentication getPasswordAuthentication() {
??????? String username = "cqhcp"; //163
郵箱登錄帳號
??????? String pwd = "12345"; //
登錄密碼
??????? return new PasswordAuthentication(username, pwd);
??? }
}
?
?
Properties props = new Properties();
props.put("mail.smtp.host","smtp.126.com");
props.put("mail.smtp.auth","true");
PopupAuthenticator auth = new PopupAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
???????????
Address addressFrom = new InternetAddress("cqhcp@126.com", "George Bush");
Address addressTo = new InternetAddress("webmaster@javazy.com", "George Bush");
Address addressCopy = new InternetAddress("haocongping@gmail.com", "George Bush");
?
message.setContent("Hello", "text/plain");//
或者使用
message.setText("Hello");
message.setSubject("Title");
message.setFrom(addressFrom);
message.addRecipient(Message.RecipientType.TO,addressTo);
message.addRecipient(Message.RecipientType.CC,addressCopy);
message.saveChanges();
?
Transport transport = session.getTransport("smtp");
transport.connect("smtp.126.com", "cqhcp", "12345");
transport.send(message);
transport.close();
|