JavaMail,顧名思義,提供給開發者處理電子郵件相關的編程接口。它是Sun發布的用來處理email的API。它可以方便地執行一些常用的郵件傳輸。我們可以基於JavaMail開發出類似於Microsoft Outlook的應用程序。
JavaMail包中用於處理電子郵件的核心類是:Session,Message,Address,Authenticator,Store,Transport, Folder等。Session定義了一個基本的郵件會話,它需要從Properties中讀取類似於郵件服務器,用戶名和密碼等信息。不過JDK中並沒有包含,使用JavaMail發送郵件需要使用Sun發布的mail.jar和activtion.jar兩個包。
郵件協議:
SMTP協議:Simple Mail Transfer Protocol,即簡單郵件傳輸協議,用於發送電子郵件
POP3協議:Post Office Protocol 3,即郵局協議的第三個版本,用於接收郵件
IMAP協議:Internet Message Access Protocol,即互聯網消息訪問協議,是POP3的替代協議
JavaMail發郵件的流程:
1.搭建James郵件服務器
2.安裝OutLook[郵件客戶端]
3.配置outLook郵件客戶端
4.搭建James郵件服務器
1.搭建james郵件服務器
james是apache的一個開源項目,純java實現。
首先我們需要下載apache-james-2.3.2.zip(批:http://pan.baidu.com/s/1pJoyg7h)
其次運行bin目錄下的run.bat即可啟動服務器
然后運行cmd命令[Telnet localhost 4555]
最后通過apps\james\SAR-INF\config.xml配置服務器(修改節點)
2.安裝OutLook[郵件客戶端],3.配置outLook郵件客戶端
輸入產品秘鑰(可選,測試的話,不必輸入)
產品秘鑰:PQDV9-GPDV4-CRM4D-PHDTH-4M2MT
創建用戶賬號,根據Telnet localhost 4555,進行連接,
用戶名和密碼默認為root
進入help,進行添加賬戶(adduser)
按要求安裝,下一步即可,注意下面這個:

hosts文件:

安裝完成,進行測試:

4.搭建James郵件服務器

最后使用JavaMail發送郵件:
需求:賬戶收到由javaMail所發送的任務,以及附件信息。
核心代碼:
創建一個類EmailAuthenticator並繼承自Authenticator,並植入用戶名和密碼

創建Mail類(設置郵件信息):
public class Mail {
private String mailServer,from,to,mailSubject,mailContent;
private String username,password;
public Mail(){
//設置郵件信息
//進行認證登錄的用戶名
username="hq@mail.com";
//認證密碼
password="hq";
//認證的郵箱對應的郵件服務器
mailServer="192.168.17.176";
//發件人信息
from="wj";
//收件人信息
to="wj@mail.com";
//郵件標題
mailSubject="我們都是好孩子333";
//郵件內容
mailContent="這是一封測試郵件!如有雷同,純屬不可能";
}
//設置郵件服務器
@SuppressWarnings("static-access")
public void send(){
Properties prop=System.getProperties();
//指定郵件server
prop.put("mail.smtp.host", mailServer);
//是否開啟認證
prop.put("mail.smtp.auth", "true");
//smtp協議的
prop.put("mail.smtp.port", "25");
//產生Session服務
EmailAuthenticator mailauth=new EmailAuthenticator(username, password);
Session mailSession=Session.getInstance(prop,(Authenticator)mailauth);
try {
//封裝Message對象
Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from)); //發件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//收件人
message.setSubject(mailSubject);
//設置內容(設置字符集處理亂碼問題)
message.setContent(mailContent,"text/html;charset=gbk");
message.setSentDate(new Date());
//創建Transport實例,發送郵件
Transport tran=mailSession.getTransport("smtp");
tran.send(message,message.getAllRecipients());
tran.close();
} catch (Exception e) {
e.printStackTrace();
}
}
測試類:
public class MyTest {
public static void main(String[] args) {
Mail mail=new Mail();
mail.send();
System.out.println("success!");
}
}


七、發送帶附件的Mail
public class MailWithAttachment {
private JavaMailSender mailSender; //必須使用 JavaMailSender
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void send() throws MessagingException,IOException{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setFrom("hq@mail.com");
helper.setTo("wj@mail.com");
helper.setSubject("生活生活");
helper.setText("生活不止眼前的苟且!!!");
//添加附件1
ClassPathResource file1 = new ClassPathResource(
"/cn/bdqn/attachfiles/test.doc");
helper.addAttachment(file1.getFilename(), file1.getFile());
//添加附件2:附件的文件名為中文時,需要對文件名進行編碼轉換,解決亂碼問題
ClassPathResource file2 = new ClassPathResource(
"/cn/bdqn/attachfiles/附件測試文件.doc");
helper.addAttachment(MimeUtility.encodeWord(file2.getFilename()),file2.getFile());
mailSender.send(mimeMessage);
}
}
測試類:
public class MailTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/*測試帶附件的郵件*/
try{
MailWithAttachment mailWithAttach = (MailWithAttachment)context.getBean("mailWithAttachment");
mailWithAttach.send();
}catch(Exception e){
System.out.print(e.toString());
}
}
}
applicationContext.xml:


外在壓力增加時,就應增強內在的動力。
--- 告誡自己
