對于JavaMail,最基礎(chǔ)的功能就是郵件的發(fā)送和接收,在這里,我先講一講郵件的發(fā)送。
在寫具體的程序前,先講一些概念。1.郵件的發(fā)送:如果你的郵件地址是a@host.com,而你要用這個郵箱發(fā)送一封郵件到to@tohost.com,這個發(fā)送過程是怎樣的呢,你以為是先連接到tohost.com這服務(wù)器上,然后把郵件發(fā)送出去嗎?其實(shí)不然。最初,你需要連接到服務(wù)器host.com上,當(dāng)然這個連接可能需要認(rèn)證,然后是發(fā)送郵件到服務(wù)器host.com上,關(guān)閉連接。在host.com上,你所發(fā)送的郵件進(jìn)入發(fā)送隊(duì)列中,輪到你要發(fā)送的郵件時,host.com主機(jī)再聯(lián)系tohost.com,將郵件傳輸?shù)椒?wù)器tohost.com上。2.一些垃圾郵件的發(fā)送:在垃圾郵件中,可能大部分的發(fā)件人的地址都是假的,這是怎么回事呢?實(shí)際上在發(fā)送這些垃圾郵件的時候,這里的host.com有些特別,可能host.com不需要對用戶進(jìn)行認(rèn)證,也可能發(fā)送垃圾郵件的人本來就控制著服務(wù)器host.com,然后控制著host.com向其他服務(wù)器,如tohost.com,發(fā)送郵件,而發(fā)送郵件的內(nèi)容可以被控制,發(fā)件人的地址就可以隨便填寫。
發(fā)送郵件主要包括3個部分:創(chuàng)建連接,創(chuàng)建郵件體,發(fā)送郵件
JavaMail中,是使用會話(Session)來管理連接的。創(chuàng)建一個連接,就需要創(chuàng)建一個會話。在會話中,有兩個重要的因素,一是會話的屬性,二是會話的認(rèn)證。在我們使用Hotmail等郵件工具的時候,就要設(shè)置”SMTP服務(wù)器身份驗(yàn)證”,也就是這里的會話的認(rèn)證。
首先,創(chuàng)建一個連接屬性。
Properties props = new Properties();
props.put("mail.smtp.host","smtp.126.com"); //設(shè)置smtp的服務(wù)器地址是smtp.126.com
props.put("mail.smtp.auth","true"); //設(shè)置smtp服務(wù)器要身份驗(yàn)證。
在創(chuàng)建一個身份驗(yàn)證。身份驗(yàn)證稍微復(fù)雜一點(diǎn),要創(chuàng)建一個Authenticator的子類,并重載getPasswordAuthentication()方法,代碼如下:
class PopupAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = "cqhcp"; //126郵箱登錄帳號
String pwd = "12345"; //登錄密碼
return new PasswordAuthentication(username, pwd);
}
}
創(chuàng)建身份驗(yàn)證的實(shí)例:
PopupAuthenticator auth = new PopupAuthenticator();
創(chuàng)建會話: 關(guān)于會話的創(chuàng)建,有兩種方法,具體請參看后續(xù)的文章,這里只簡單使用一種。
Session session = Session.getInstance(props, auth);
定義郵件地址:
//發(fā)送人地址
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");
創(chuàng)建郵件體:
message.setContent("Hello", "text/plain");//或者使用message.setText("Hello");更詳細(xì)的信息請參看后續(xù)文章.
message.setSubject("Title");
message.setFrom(addressFrom);
message.addRecipient(Message.RecipientType.TO,addressTo);
message.addRecipient(Message.RecipientType.CC,addressCopy);
message.saveChanges();
發(fā)送郵件的過程:
Transport transport = session.getTransport("smtp");//創(chuàng)建連接
transport.connect("smtp.126.com", "cqhcp", "12345");//連接服務(wù)器
transport.send(message);//發(fā)送信息
transport.close();//關(guān)閉連接
整體程序的代碼如下:
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();