本文執行個體為大家分享了郵件收發功能的具體實現代碼,供大家參考,具體內容如下
準備工作, 環境搭建:
1. 本地搭建一個郵件伺服器
易郵伺服器,eyoumailserversetup.exe
2. 建立郵箱帳號
張三給李四發郵件。
步驟1:
建立網域名稱: 工具, 伺服器設定, 單網域名稱框中輸入 itcast.com
步驟2:
建立郵箱帳號: zhangsan@itcast.com
lisi@itcast.com
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("zhangsan@itcast.com")); //3.4 接收人 message.setRecipient(RecipientType.TO, new InternetAddress("lisi@itcast.com")); //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("zhangsan@itcast.com"); } 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("lisi@itcast.com")); 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("zhangsan@itcast.com"); } 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("lisi@itcast.com")); 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程式設計有所協助。