本文實例為大家分享了郵件收發功能的具體實現代碼,供大家參考,具體內容如下
準備工作, 環境搭建:
1. 本地搭建一個郵件服務器
易郵服務器,eyoumailserversetup.exe
2. 新建郵箱賬號
張三給李四發郵件。
步驟1:
新建域名: 工具, 服務器設置, 單域名框中輸入itcast.com
步驟2:
新建郵箱賬號: [email protected]
[email protected]
3. 安裝foxmail
配置郵件發送服務器(smtp): localhost 25
郵件接收服務器(pop3): localhost 110
再新建賬號,就可以接收郵件了!
注意
如果是web項目,因為javaee自帶的有郵件功能,可能存在問題!
我們要用自己的mail.jar文件功能! 需要刪除javaee中mail包!
使用:
JavaMail開發,先引入jar文件:
activation.jar 【如果使用jdk1.6或以上版本,可以不用這個jar文件】
mail.jar 【郵件發送核心包】
/** * 1. 發送一封普通郵件* @author Jie.Yuan * */public class App_SendMail { @Test public void testSend() throws Exception { //0. 郵件參數Properties prop = new Properties(); prop.put("mail.transport.protocol", "smtp"); // 指定協議prop.put("mail.smtp.host", "localhost"); // 主機stmp.qq.com prop.put("mail.smtp.port", 25); // 端口prop.put("mail.smtp.auth", "true"); // 用戶密碼認證prop.put("mail.debug", "true"); // 調試模式//1. 創建一個郵件的會話Session session = Session.getDefaultInstance(prop); //2. 創建郵件體對象(整封郵件對象) MimeMessage message = new MimeMessage(session); //3. 設置郵件體參數: //3.1 標題message.setSubject("我的第一封郵件"); //3.2 郵件發送時間message.setSentDate(new Date()); //3.3 發件人message.setSender(new InternetAddress("[email protected]")); //3.4 接收人message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]")); //3.5內容message.setText("你好,已經發送成功! 正文...."); // 簡單純文本郵件message.saveChanges(); // 保存郵件(可選) //4. 發送Transport trans = session.getTransport(); trans.connect("zhangsan", "888"); // 發送郵件trans.sendMessage(message, message.getAllRecipients()); trans.close(); }}帶圖片
/** * 帶圖片資源的郵件* @author Jie.Yuan * */public class App_2SendWithImg { // 初始化參數private static Properties prop; // 發件人private static InternetAddress sendMan = null; static { prop = new Properties(); prop.put("mail.transport.protocol", "smtp"); // 指定協議prop.put("mail.smtp.host", "localhost"); // 主機stmp.qq.com prop.put("mail.smtp.port", 25); // 端口prop.put("mail.smtp.auth", "true"); // 用戶密碼認證prop.put("mail.debug", "true"); // 調試模式try { sendMan = new InternetAddress("[email protected]"); } catch (AddressException e) { throw new RuntimeException(e); } } @Test public void testSend() throws Exception { // 1. 創建郵件會話Session session = Session.getDefaultInstance(prop); // 2. 創建郵件對象MimeMessage message = new MimeMessage(session); // 3. 設置參數:標題、發件人、收件人、發送時間、內容message.setSubject("帶圖片郵件"); message.setSender(sendMan); message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]")); message.setSentDate(new Date()); /***************設置郵件內容: 多功能用戶郵件(related)*******************/ // 4.1 構建一個多功能郵件塊MimeMultipart related = new MimeMultipart("related"); // 4.2 構建多功能郵件塊內容= 左側文本+ 右側圖片資源MimeBodyPart content = new MimeBodyPart(); MimeBodyPart resource = new MimeBodyPart(); // 設置具體內容: a.資源(圖片) String filePath = App_2SendWithImg.class.getResource("8.jpg").getPath(); DataSource ds = new FileDataSource(new File(filePath)); DataHandler handler = new DataHandler(ds); resource.setDataHandler(handler); resource.setContentID("8.jpg"); // 設置資源名稱,給外鍵引用// 設置具體內容: b.文本content.setContent("<img src='cid:8.jpg'/> 好哈哈! ", "text/html;charset=UTF-8"); related.addBodyPart(content); related.addBodyPart(resource); /*******4.3 把構建的複雜郵件快,添加到郵件中********/ message.setContent(related); // 5. 發送Transport trans = session.getTransport(); trans.connect("zhangsan", "888"); trans.sendMessage(message, message.getAllRecipients()); trans.close(); }}圖片+附件
/** * 3. 帶圖片資源以及附件的郵件* @author Jie.Yuan * */public class App_3ImgAndAtta { // 初始化參數private static Properties prop; // 發件人private static InternetAddress sendMan = null; static { prop = new Properties(); prop.put("mail.transport.protocol", "smtp"); // 指定協議prop.put("mail.smtp.host", "localhost"); // 主機stmp.qq.com prop.put("mail.smtp.port", 25); // 端口prop.put("mail.smtp.auth", "true"); // 用戶密碼認證prop.put("mail.debug", "true"); // 調試模式try { sendMan = new InternetAddress("[email protected]"); } catch (AddressException e) { throw new RuntimeException(e); } } @Test public void testSend() throws Exception { // 1. 創建郵件會話Session session = Session.getDefaultInstance(prop); // 2. 創建郵件對象MimeMessage message = new MimeMessage(session); // 3. 設置參數:標題、發件人、收件人、發送時間、內容message.setSubject("帶圖片郵件"); message.setSender(sendMan); message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]")); message.setSentDate(new Date()); /* * 帶附件(圖片)郵件開發*/ // 構建一個總的郵件塊MimeMultipart mixed = new MimeMultipart("mixed"); // ---> 總郵件快,設置到郵件對像中message.setContent(mixed); // 左側: (文本+圖片資源) MimeBodyPart left = new MimeBodyPart(); // 右側: 附件MimeBodyPart right = new MimeBodyPart(); // 設置到總郵件塊mixed.addBodyPart(left); mixed.addBodyPart(right); /******附件********/ String attr_path = this.getClass().getResource("a.docx").getPath(); DataSource attr_ds = new FileDataSource(new File(attr_path)); DataHandler attr_handler = new DataHandler(attr_ds); right.setDataHandler(attr_handler); right.setFileName("a.docx"); /***************設置郵件內容: 多功能用戶郵件(related)*******************/ // 4.1 構建一個多功能郵件塊MimeMultipart related = new MimeMultipart("related"); // ----> 設置到總郵件快的左側中left.setContent(related); // 4.2 構建多功能郵件塊內容= 左側文本+ 右側圖片資源MimeBodyPart content = new MimeBodyPart(); MimeBodyPart resource = new MimeBodyPart(); // 設置具體內容: a.資源(圖片) String filePath = App_3ImgAndAtta.class.getResource("8.jpg").getPath(); DataSource ds = new FileDataSource(new File(filePath)); DataHandler handler = new DataHandler(ds); resource.setDataHandler(handler); resource.setContentID("8.jpg"); // 設置資源名稱,給外鍵引用// 設置具體內容: b.文本content.setContent("<img src='cid:8.jpg'/> 好哈哈! ", "text/html;charset=UTF-8"); related.addBodyPart(content); related.addBodyPart(resource); // 5. 發送Transport trans = session.getTransport(); trans.connect("zhangsan", "888"); trans.sendMessage(message, message.getAllRecipients()); trans.close(); }}以上就是本文的全部內容,希望對大家學習java程序設計有所幫助。