433【畢設課設】基於51微控制器超聲波視力保護器智慧防近視坐姿系統
阿新 • • 發佈:2020-12-05
使用到的jar包:
- mail.jar(29th)
- activation.jar(2fmo)
Java實現純文字郵件傳送
1 package org.westos.email; 2 3 import com.sun.mail.util.MailSSLSocketFactory; 4 5 import javax.mail.*; 6 import javax.mail.internet.InternetAddress; 7 import javax.mail.internet.MimeMessage; 8 import java.security.GeneralSecurityException;9 import java.util.Properties; 10 11 public class SendEamil { 12 public static void main(String[] args) throws MessagingException, GeneralSecurityException { 13 //建立一個配置檔案並儲存 14 Properties properties = new Properties(); 15 16 properties.setProperty("mail.host","smtp.qq.com");17 18 properties.setProperty("mail.transport.protocol","smtp"); 19 20 properties.setProperty("mail.smtp.auth","true"); 21 22 23 //QQ存在一個特性設定SSL加密 24 MailSSLSocketFactory sf = new MailSSLSocketFactory(); 25 sf.setTrustAllHosts(true); 26 properties.put("mail.smtp.ssl.enable", "true");27 properties.put("mail.smtp.ssl.socketFactory", sf); 28 29 //建立一個session物件 30 Session session = Session.getDefaultInstance(properties, new Authenticator() { 31 @Override 32 protected PasswordAuthentication getPasswordAuthentication() { 33 return new PasswordAuthentication("[email protected]","16位授權碼"); 34 } 35 }); 36 37 //開啟debug模式 38 session.setDebug(true); 39 40 //獲取連線物件 41 Transport transport = session.getTransport(); 42 43 //連線伺服器 44 transport.connect("smtp.qq.com","[email protected]","16位授權碼"); 45 46 //建立郵件物件 47 MimeMessage mimeMessage = new MimeMessage(session); 48 49 //郵件傳送人 50 mimeMessage.setFrom(new InternetAddress("[email protected]")); 51 52 //郵件接收人 53 mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]")); 54 55 //郵件標題 56 mimeMessage.setSubject("Hello Mail"); 57 58 //郵件內容 59 mimeMessage.setContent("我的想法是把程式碼放進一個迴圈裡","text/html;charset=UTF-8"); 60 61 //傳送郵件 62 transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients()); 63 64 //關閉連線 65 transport.close(); 66 } 67 }
Java實現文字圖片附件複雜的郵件傳送
MIME(多用途網際網路郵件擴充套件型別)
MimeBodyPart類
javax.mail.internet.MimeBodyPart類 表示的是一個MIME訊息,它和MimeMessage類一樣都是從Part介面繼承過來。
MimeMultipart類
javax.mail.internet.MimeMultipart是抽象類 Multipart的實現子類,它用來組合多個MIME訊息。一個MimeMultipart物件可以包含多個代表MIME訊息的MimeBodyPart物件
1 package org.westos.email; 2 3 import com.sun.mail.util.MailSSLSocketFactory; 4 5 import javax.activation.DataHandler; 6 import javax.activation.FileDataSource; 7 import javax.mail.*; 8 import javax.mail.internet.*; 9 import java.security.GeneralSecurityException; 10 import java.util.Properties; 11 12 public class SendComplexEmail { 13 public static void main(String[] args) throws GeneralSecurityException, MessagingException { 14 Properties prop = new Properties(); 15 prop.setProperty("mail.host", "smtp.qq.com"); 設定QQ郵件伺服器 16 prop.setProperty("mail.transport.protocol", "smtp"); // 郵件傳送協議 17 prop.setProperty("mail.smtp.auth", "true"); // 需要驗證使用者名稱密碼 18 19 // QQ郵箱設定SSL加密 20 MailSSLSocketFactory sf = new MailSSLSocketFactory(); 21 sf.setTrustAllHosts(true); 22 prop.put("mail.smtp.ssl.enable", "true"); 23 prop.put("mail.smtp.ssl.socketFactory", sf); 24 25 //1、建立定義整個應用程式所需的環境資訊的 Session 物件 26 Session session = Session.getDefaultInstance(prop, new Authenticator() { 27 @Override 28 protected PasswordAuthentication getPasswordAuthentication() { 29 //傳入發件人的姓名和授權碼 30 return new PasswordAuthentication("[email protected]","16位授權碼"); 31 } 32 }); 33 34 //2、通過session獲取transport物件 35 Transport transport = session.getTransport(); 36 37 //3、通過transport物件郵箱使用者名稱和授權碼連線郵箱伺服器 38 transport.connect("smtp.qq.com","[email protected]","16位授權碼"); 39 40 //4、建立郵件,傳入session物件 41 MimeMessage mimeMessage = complexEmail(session); 42 43 //5、傳送郵件 44 transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients()); 45 46 //6、關閉連線 47 transport.close(); 48 49 50 } 51 52 public static MimeMessage complexEmail(Session session) throws MessagingException { 53 //訊息的固定資訊 54 MimeMessage mimeMessage = new MimeMessage(session); 55 56 //發件人 57 mimeMessage.setFrom(new InternetAddress("[email protected]")); 58 //收件人 59 mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]")); 60 //郵件標題 61 mimeMessage.setSubject("帶圖片和附件的郵件"); 62 63 //郵件內容 64 //準備圖片資料 65 MimeBodyPart image = new MimeBodyPart(); 66 DataHandler handler = new DataHandler(new FileDataSource("E:\\IdeaProjects\\WebEmail\\resources\\測試圖片.png")); 67 image.setDataHandler(handler); 68 image.setContentID("test.png"); //設定圖片id 69 70 //準備文字 71 MimeBodyPart text = new MimeBodyPart(); 72 text.setContent("這是一段文字<img src='cid:test.png'>","text/html;charset=utf-8"); 73 74 //附件 75 MimeBodyPart appendix = new MimeBodyPart(); 76 appendix.setDataHandler(new DataHandler(new FileDataSource("E:\\IdeaProjects\\WebEmail\\resources\\測試檔案.txt"))); 77 appendix.setFileName("test.txt"); 78 79 //拼裝郵件正文 80 MimeMultipart mimeMultipart = new MimeMultipart(); 81 mimeMultipart.addBodyPart(image); 82 mimeMultipart.addBodyPart(text); 83 mimeMultipart.setSubType("related");//文字和圖片內嵌成功 84 85 //將拼裝好的正文內容設定為主體 86 MimeBodyPart contentText = new MimeBodyPart(); 87 contentText.setContent(mimeMultipart); 88 89 //拼接附件 90 MimeMultipart allFile = new MimeMultipart(); 91 allFile.addBodyPart(appendix);//附件 92 allFile.addBodyPart(contentText);//正文 93 allFile.setSubType("mixed"); //正文和附件都存在郵件中,所有型別設定為mixed 94 95 96 //放到Message訊息中 97 mimeMessage.setContent(allFile); 98 mimeMessage.saveChanges();//儲存修改 99 100 return mimeMessage; 101 } 102 }
QQ郵箱授權碼:
QQ郵箱需獲取相應的許可權:
QQ郵箱–>郵箱設定–>賬戶–>POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務 開啟POP3/SMTP服務,然後獲取16位授權碼(注意不要將授權碼洩露,一個賬戶可以擁有多個授權碼)