1. 程式人生 > >JavaMail實踐--實現郵件傳送

JavaMail實踐--實現郵件傳送

一、介紹

  1、主要功能:

    實現普通郵件的傳送

    實現帶有HTML標籤的內容傳送

    實現帶有附件的郵件傳送

  2、API

  3、QQ郵箱的認證

    這裡使用的郵件主機是:smtp.qq.com

    需要獲得QQ郵箱的授權碼:需要在登入QQ郵箱後臺在"設定"=》賬號中開啟POP3/SMTP服務 

二、原始碼分享

import java.util.Properties;
import java.util.Scanner;

import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication;
import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.*; public class EMailDemo { public static void main(String[] args) { // EMailDemo.sendEmail(); // EMailDemo.sendHtml(EMailDemo.getSession()); EMailDemo.sendWithFile(getSession()); }
private static String to = null; private static String from = null; private static Scanner sc = new Scanner(System.in); private static Session getSession() { System.out.println("請輸入收件人郵箱(任意):"); String to = sc.nextLine();// 收件人 System.out.println("請輸入發件人郵箱(QQ):"); String from = sc.nextLine();// 發件人 System.out.println("請輸入發件人郵箱的授權碼:"); String pop3 = sc.nextLine();// 發件人 String host = "smtp.qq.com";// 指定傳送郵件的QQ主機 Properties properties = System.getProperties(); // 獲取系統屬性 // 設定郵件伺服器 properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true"); // 獲取預設session物件 Session session = Session.getDefaultInstance(properties, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, pop3); // 發件人郵件使用者名稱、授權碼 } }); return session; } public static void sendEmail(Session session) { try { // 建立預設的 MimeMessage 物件 MimeMessage message = new MimeMessage(session); // Set From: 頭部頭欄位 message.setFrom(new InternetAddress(from)); // Set To: 頭部頭欄位 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 頭部頭欄位 System.out.println("請設定郵件標題:"); message.setSubject(sc.nextLine()); // 設定訊息體 System.out.println("請設定郵件內容:"); message.setText(sc.nextLine()); // 傳送訊息 Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } } public static void sendHtml(Session session) { try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); System.out.println("請設定郵件標題:"); message.setSubject(sc.nextLine()); // 傳送 HTML 訊息, 可以插入html標籤 System.out.println("請設定郵件內容(可以插入html標籤):"); message.setContent(sc.nextLine(), "text/html"); Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } } public static void sendWithFile(Session session) { try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); System.out.println("請設定郵件標題:"); message.setSubject(sc.nextLine()); // 建立訊息部分 BodyPart messageBodyPart = new MimeBodyPart(); // 訊息 System.out.println("請設定郵件內容:"); message.setText(sc.nextLine()); // 建立多重訊息 Multipart multipart = new MimeMultipart(); // 設定文字訊息部分 multipart.addBodyPart(messageBodyPart); // 附件部分 messageBodyPart = new MimeBodyPart(); System.out.println("請輸入檔案完整路徑:"); String filename = sc.nextLine(); DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // 傳送完整訊息 message.setContent(multipart); Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } } }