Java發(fā)郵件:Java Mail與Apache Mail
一、郵件簡(jiǎn)介
一封郵件由很多信息構(gòu)成,主要的信息如下,其他的暫時(shí)不考慮,例如抄送等:
1、收件人:收件人的郵箱地址,例如xxx@xx.com
2、收件人姓名:大部分的郵件顯示時(shí)都會(huì)顯示,例如loadfate 779554589@qq.com
3、發(fā)件人:發(fā)件人的郵箱地址
4、發(fā)件人姓名:
5、主題:郵件的標(biāo)題
6、內(nèi)容及附件:郵件的主要內(nèi)容
二、使用Java發(fā)郵件的通用步驟
一般的項(xiàng)目中沒(méi)有單獨(dú)的郵件服務(wù)器,一般情況下都是使用別人的服務(wù)器。
1、設(shè)置smtp服務(wù)器:不同的郵件服務(wù)器有不同的地址,例如:smtp.qq.com表示騰訊的smtp服務(wù)器。
2、授權(quán):使用該服務(wù)器的帳號(hào)和密碼登錄該服務(wù)器。
3、創(chuàng)建郵件:創(chuàng)建一份包含所有信息的郵件,比如發(fā)件人、收件人、內(nèi)容等。
4、設(shè)置郵件的屬性:為郵件的屬性添加數(shù)據(jù)。
5、發(fā)送郵件:因?yàn)榉庋b不同,發(fā)送的方式不一致。
三、Java Mail與Apache Mail
Apache Mail是對(duì)Java Mail的封裝,使用起來(lái)更加的簡(jiǎn)便,邏輯層次感更好。
使用Java Mail只需要導(dǎo)入一個(gè)jar包:mail.jar。
使用Apache Mail的時(shí)候需要導(dǎo)入兩個(gè)jar包:mail.jar、commons-email-1.3.1.jar。
四、使用Java Mail發(fā)送郵件
1 public static void main(String[] args) throws Exception { 2 final String user = "779554589"; 3 final String password = ""; 4 5 String fromAddress = "779554589@qq.com"; 6 String toAddress = "loadfate@163.com"; 7 String subject = "郵件測(cè)試主題"; 8 String content = "這是一個(gè)測(cè)試郵件<b>哈哈</b>"; 9 10 //配置參數(shù) 11 Properties props = new Properties(); 12 props.setProperty("mail.smtp.auth", "true"); 13 props.setProperty("mail.transport.protocol", "smtp"); 14 props.setProperty("mail.host", "smtp.qq.com"); 15 // 方法一:使用transport對(duì)象發(fā)送郵件 16 { 17 //通過(guò)參數(shù)生成會(huì)話 18 Session session = Session.getInstance(props); 19 //啟用調(diào)試模式 20 session.setDebug(true); 21 //創(chuàng)建一封郵件,并設(shè)置信息 22 Message message = new MimeMessage(session); 23 message.setFrom(new InternetAddress(fromAddress)); 24 message.setSubject(subject); 25 message.setText(content); 26 //創(chuàng)建傳輸 27 Transport transport = session.getTransport(); 28 //連接smtp服務(wù)器 29 transport.connect(user, password); 30 //發(fā)送 31 transport.sendMessage(message, new InternetAddress[] { new InternetAddress(toAddress) }); 32 transport.close(); 33 } 34 35 36 // 方法二:使用Transport類靜態(tài)方法發(fā)送郵件 37 { 38 //生成Session時(shí)以獲取授權(quán)連接 39 Session session = Session.getInstance(props, new Authenticator() { 40 @Override 41 protected PasswordAuthentication getPasswordAuthentication() { 42 return new PasswordAuthentication(user, password); 43 } 44 }); 45 session.setDebug(true); 46 //創(chuàng)建一封郵件,并設(shè)置信息 47 Message message = new MimeMessage(session); 48 message.setSubject(subject); 49 message.setFrom(new InternetAddress(fromAddress)); 50 message.setRecipient(RecipientType.TO, new InternetAddress(toAddress)); 51 message.setContent(content, "text/html;charset=utf-8"); 52 53 //直接發(fā)送,message通過(guò)已經(jīng)授權(quán)的Session生成 54 Transport.send(message); 55 } 56 } |
五、使用Apache Mail發(fā)送郵件
1 public class ApacheMailTest { 2 // smtp服務(wù)器 3 private String hostName = "smtp.qq.com"; 4 // 帳號(hào)與密碼 5 private String userName = "779554589"; 6 private String password = "這是個(gè)秘密"; 7 // 發(fā)件人 8 private String fromAddress = "779554589@qq.com"; 9 // 發(fā)件人姓名 10 private String fromName = "loadfate"; 11 12 public static void main(String[] args) throws Exception { 13 // 收件人與收件人名字 14 String toAddress = "loadfate@163.com"; 15 String toName = "loadfate"; 16 ApacheMailTest test = new ApacheMailTest(); 17 // 所有的異常都為處理,方便瀏覽 18 19 test.sendSimpleEmail(toAddress, toName); 20 test.sendHtmlEmail(toAddress, toName); 21 test.sendMultiPartEmail(toAddress, toName); 22 System.out.println("發(fā)送完成"); 23 } 24 25 // 發(fā)送簡(jiǎn)單郵件,類似一條信息 26 public void sendSimpleEmail(String toAddress, String toName) throws Exception { 27 SimpleEmail email = new SimpleEmail(); 28 email.setHostName(hostName);// 設(shè)置smtp服務(wù)器 29 email.setAuthentication(userName, password);// 設(shè)置授權(quán)信息 30 email.setCharset("utf-8"); 31 email.setFrom(fromAddress, fromName, "utf-8");// 設(shè)置發(fā)件人信息 32 email.addTo(toAddress, toName, "utf-8");// 設(shè)置收件人信息 33 email.setSubject("測(cè)試主題");// 設(shè)置主題 34 email.setMsg("這是一個(gè)簡(jiǎn)單的測(cè)試!");// 設(shè)置郵件內(nèi)容 35 email.send();// 發(fā)送郵件 36 } 37 38 // 發(fā)送Html內(nèi)容的郵件 39 public void sendHtmlEmail(String toAddress, String toName) throws Exception { 40 HtmlEmail email = new HtmlEmail(); 41 email.setHostName(hostName); 42 email.setAuthentication(userName, password); 43 email.setCharset("utf-8"); 44 email.addTo(toAddress, toName, "utf-8"); 45 email.setFrom(fromAddress, fromName, "utf-8"); 46 email.setSubject("這是一個(gè)html郵件"); 47 // 設(shè)置html內(nèi)容,實(shí)際使用時(shí)可以從文本讀入寫好的html代碼 48 email.setHtmlMsg("<div style='width:100px;height:200px;'>a</div>"); 49 email.send(); 50 51 } 52 53 // 發(fā)送復(fù)雜的郵件,包含附件等 54 public void sendMultiPartEmail(String toAddress, String toName) throws Exception { 55 MultiPartEmail email = null; 56 email = new MultiPartEmail(); 57 email.setHostName(hostName); 58 email.setAuthentication(userName, password); 59 email.setCharset("utf-8"); 60 email.addTo(toAddress, toName, "utf-8"); 61 email.setFrom(fromAddress, fromName, "utf-8"); 62 email.setSubject("這是有附件的郵件"); 63 email.setMsg("<a href='#'>測(cè)試內(nèi)容</a>"); 64 65 // 為郵件添加附加內(nèi)容 66 EmailAttachment attachment = new EmailAttachment(); 67 attachment.setPath("D:\\郵件.txt");// 本地文件 68 // attachment.setURL(new URL("http://xxx/a.gif"));//遠(yuǎn)程文件 69 attachment.setDisposition(EmailAttachment.ATTACHMENT); 70 attachment.setDescription("描述信息"); 71 // 設(shè)置附件顯示名字,必須要編碼,不然中文會(huì)亂碼 72 attachment.setName(MimeUtility.encodeText("郵件.txt")); 73 // 將附件添加到郵件中 74 email.attach(attachment); 75 email.send(); 76 } 77 } |
六、附件
下載地址:http://pan.baidu.com/s/1qW8rcAw
如果失效請(qǐng)聯(lián)系LZ
文件說(shuō)明:
1、maildemo.zip :maildemo的源代碼
2、mail.jar :Java Mail的jar包
3、commons-email-1.3.1.jar :Apache Mail的jar包
posted on 2014-06-11 11:19 順其自然EVO 閱讀(1007) 評(píng)論(0) 編輯 收藏 所屬分類: 測(cè)試學(xué)習(xí)專欄