1. Emails with only plain text
Code examples are as follows:
package com.lyh.sendemail; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; //發(fā)送郵件 public class MessageDemo1 { public static void main(String[] args) throws Exception{ Properties props = new Properties();//key value:配置參數。真正發(fā)送郵件時再配置 props.setProperty("mail.transport.protocol", "smtp");//指定郵件發(fā)送的協議,參數是規(guī)范規(guī)定的 props.setProperty("mail.host", "smtp.163.com");//指定發(fā)件服務器的地址,參數是規(guī)范規(guī)定的 // props.setProperty("mail.debug", "true");//郵件發(fā)送的調試模式,參數是規(guī)范規(guī)定的 props.setProperty("mail.smtp.auth", "true");//請求服務器進行身份認證。參數與具體的JavaMail實現有關 Session session = Session.getInstance(props);//發(fā)送郵件時使用的環(huán)境配置 session.setDebug(true); MimeMessage message = new MimeMessage(session); //設置郵件的頭 message.setFrom(new InternetAddress("xxx@163.com")); message.setRecipients(Message.RecipientType.TO, "xxx@qq.com"); message.setSubject("This is second message"); //設置正文 message.setContent("<h1>hello</h1>", "text/html"); // message.setText("<h1>hello</h1>");//純文本 message.saveChanges(); //發(fā)送郵件 Transport ts = session.getTransport(); ts.connect("xxx@163.com", "123456"); // 密碼為授權碼不是郵箱的登錄密碼 ts.sendMessage(message, message.getAllRecipients());//對象,用實例方法} } }
2. Emails with pictures
a. Complex email encapsulation model
Code examples
package com.lyh.sendemail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; //發(fā)送郵件 public class MessageDemo2 { public static void main(String[] args) throws Exception{ Properties props = new Properties();//key value:配置參數。真正發(fā)送郵件時再配置 props.setProperty("mail.transport.protocol", "smtp");//指定郵件發(fā)送的協議,參數是規(guī)范規(guī)定的 props.setProperty("mail.host", "smtp.163.com");//指定發(fā)件服務器的地址,參數是規(guī)范規(guī)定的 // props.setProperty("mail.debug", "true");//郵件發(fā)送的調試模式,參數是規(guī)范規(guī)定的 props.setProperty("mail.smtp.auth", "true");//請求服務器進行身份認證。參數與具體的JavaMail實現有關 Session session = Session.getInstance(props);//發(fā)送郵件時使用的環(huán)境配置 session.setDebug(true); MimeMessage message = new MimeMessage(session); //設置郵件的頭 message.setFrom(new InternetAddress("xxx@163.com")); message.setRecipients(Message.RecipientType.TO, "xxx@qq.com"); message.setSubject("This is second message"); //設置正文 //搞出文本部分 MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent("aaa<img src='cid:mm'/ alt="JavaMail email development" >aaa", "text/html"); //搞圖片部分 MimeBodyPart imagePart = new MimeBodyPart(); imagePart.setContentID("mm"); //把磁盤上的文件加到part中使用到了JAF框架 DataHandler dh = new DataHandler(new FileDataSource("src/0.jpg")); imagePart.setDataHandler(dh); MimeMultipart mp = new MimeMultipart(); mp.addBodyPart(textPart); mp.addBodyPart(imagePart); mp.setSubType("related");//有關系的 message.setContent(mp); message.saveChanges(); //發(fā)送郵件 Transport ts = session.getTransport(); ts.connect("xxx@163.com", "123456"); //密碼為授權碼不是郵箱的登錄密碼 ts.sendMessage(message, message.getAllRecipients());//對象,用實例方法 } }
3. With text, Emails with pictures and attachments
Code examples:
package com.lyh.sendemail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; //發(fā)送郵件 public class MessageDemo3 { public static void main(String[] args) throws Exception{ Properties props = new Properties();//key value:配置參數。真正發(fā)送郵件時再配置 props.setProperty("mail.transport.protocol", "smtp");//指定郵件發(fā)送的協議,參數是規(guī)范規(guī)定的 props.setProperty("mail.host", "smtp.163.com");//指定發(fā)件服務器的地址,參數是規(guī)范規(guī)定的 // props.setProperty("mail.debug", "true");//郵件發(fā)送的調試模式,參數是規(guī)范規(guī)定的 props.setProperty("mail.smtp.auth", "true");//請求服務器進行身份認證。參數與具體的JavaMail實現有關 Session session = Session.getInstance(props);//發(fā)送郵件時使用的環(huán)境配置 // session.setDebug(true); MimeMessage message = new MimeMessage(session); //設置郵件的頭 message.setFrom(new InternetAddress("xxx@163.com")); message.setRecipients(Message.RecipientType.TO, "xxxqq.com"); message.setSubject("這是一封復雜的郵件"); //設置正文 //搞出文本部分 MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent("美女<img src='cid:mm'/ alt="JavaMail email development" >aaa", "text/html;charset=UTF-8"); //搞圖片部分 MimeBodyPart imagePart = new MimeBodyPart(); imagePart.setContentID("mm"); //把磁盤上的文件加到part中使用到了JAF框架 DataHandler dh = new DataHandler(new FileDataSource("src/0.jpg")); imagePart.setDataHandler(dh); MimeMultipart mp = new MimeMultipart(); mp.addBodyPart(textPart); mp.addBodyPart(imagePart); mp.setSubType("related");//有關系的 MimeBodyPart textImagePart = new MimeBodyPart(); //將 MimeMultipart 添加到 MimeBodyPart實現附件的發(fā)送 textImagePart.setContent(mp); //創(chuàng)建附件部分 MimeBodyPart attachmentPart = new MimeBodyPart(); dh = new DataHandler(new FileDataSource("src/賬戶.rar")); String filename = dh.getName(); attachmentPart.setDataHandler(dh); //手工設置文件名 防止亂碼使用 javaMail里的 MimeUtility進行編碼 attachmentPart.setFileName(MimeUtility.encodeText(filename)); //最終的 MimeMultipart MimeMultipart finalMp = new MimeMultipart(); finalMp.addBodyPart(attachmentPart); finalMp.addBodyPart(textImagePart); finalMp.setSubType("mixed"); message.setContent(finalMp); message.saveChanges(); //發(fā)送郵件 Transport ts = session.getTransport(); ts.connect("xxx@163.com", "123456"); //密碼為授權碼不是郵箱的登錄密碼 ts.sendMessage(message, message.getAllRecipients());//對象,用實例方法 } }
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Grass Wonder Build Guide | Uma Musume Pretty Derby
1 months ago
By Jack chen
Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them
4 weeks ago
By DDD
Uma Musume Pretty Derby Banner Schedule (July 2025)
1 months ago
By Jack chen
RimWorld Odyssey Temperature Guide for Ships and Gravtech
3 weeks ago
By Jack chen
Windows Security is blank or not showing options
1 months ago
By 下次還敢

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)