接收郵件服務器:
發送郵件服務器:
1、準備工作登錄郵箱開啟PO3/SMTP服務1.1、SMTP
簡單郵件傳輸協議定義了遞送郵件的機制。在下文中,我們將使用基于Java-Mail的程序與公司或者ISP的SMTP服務器進行通訊。這個SMTP服務器將郵件轉發到接收者的SMTP服務器,直至最后被接收者通過POP或者IMAP協議獲取。這并不需要SMTP服務器使用支持授權的郵件轉發,但是卻的確要注意SMTP服務器的正確設置(SMTP服務器的設置與 API無關)。
1.2、POP
POP是一種郵局協議,目前為第3個版本,即眾所周知的POP3。POP定義了一種用戶如何獲得郵件的機制。它規定了每個用戶使用一個單獨的郵箱。大多數人在使用POP時所熟悉的功能并非都被支持,例如查看郵箱中的新郵件數量。而這個功能是微軟的內建的,那么就說明微軟之類的郵件客戶端軟件是通過查詢最近收到的郵件來計算新郵件的數量來實現前面所說的功能。因此在我們使用 API時需要注意.net服務器上不能發送郵件,當需要獲得如前面所講的新郵件數量之類的信息時,我們不得不自己進行計算。
1.3、打開qq郵箱>點擊設置>賬戶,默認是關閉的 ,開啟PO3/SMTP服務。
手機短信驗證成功后,生成16位SMTP命令授權碼,見下圖:
2、Java原生發送qq郵箱實現步驟2.1、創建一個項目
2.2、新建一個lib文件夾.net服務器上不能發送郵件,放郵件發送所需要的架包
2.3、編寫核心代碼 我這里封裝了 也可以不封裝
2.4、封裝郵箱,代碼如下
/**
* @author makeJava
*
* @create 2021-03-21日 18:43
* @describes qq郵箱工具類
*/
public class Email {
public void qqemai(String QQmail,String head,String body) throws AddressException, MessagingException {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 連接協議
properties.put("mail.smtp.host", "smtp.qq.com");// 主機名
properties.put("mail.smtp.port", 465);// 端口號
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");// 設置是否使用ssl安全連接 ---一般都使用
properties.put("mail.debug", "true");// 設置是否顯示debug信息 true 會在控制臺顯示相關信息
// 得到回話對象

Session session = Session.getInstance(properties);
// 獲取郵件對象
Message message = new MimeMessage(session);
// 設置發件人郵箱地址
message.setFrom(new InternetAddress("****@qq.com"));
// 設置收件人郵箱地址
message.setRecipients(Message.RecipientType.TO,
new InternetAddress[] { new InternetAddress(QQmail) });
//new InternetAddress();設置同時發送多個好友
// 設置郵件標題
message.setSubject(head);
// 設置郵件內容
message.setText(body);
// 得到郵差對象
Transport transport = session.getTransport();
// 連接自己的郵箱賬戶
transport.connect("****@qq.com", "*****授權碼");// 密碼為QQ郵箱開通的stmp服務后得到的客戶端授權碼
// 發送郵件
int i=0;
transport.sendMessage(message, message.getAllRecipients());
System.out.println("成功!");
transport.close();
}
}
2.5、編寫main方法
/**
* @author makeJava
*
* @create 2021-03-21日 18:50
* @describes 測試qq郵件
*/
public class qqmain {
public static void main(String[] args) throws MessagingException {

Email qq=new Email();
Scanner input=new Scanner(System.in);
System.out.println("請輸入QQ號");
String QQmail=input.next()+"@qq.com";
System.out.println("請輸入要發送的標題");
String head=input.next();
System.out.println("請輸入要發送的文本");
String body=input.next();
qq.qqemai(QQmail, head, body);
}
}
3、運行測試:
4、結果如下:
測試成功
5、實現QQ郵箱的發送
打開qq郵箱>點擊設置>賬戶,默認是關閉的 ,開啟PO3/SMTP服務。手機短信驗證成功后,生成16位SMTP命令授權碼,見下圖:
==記住這串授權碼 會用到==
6、實現步驟6.1、創建一個項目
6.2、勾選web依賴
6.3、導入QQ郵件所需依賴
org.springframework.boot
spring-boot-starter-mail

com.troy.keeper
keeper-core-boot
com.troy.keeper
keeper-starter-excel
com.troy.keeper
keeper-starter-swagger
com.troy.keeper
sd-user-api
${project.parent.version}
com.troy.keeper
sd-system-api
${project.parent.version}
6.4、配置.
#配置郵件消息
spring.mail.host=smtp.qq.com
#發送郵件者信箱
spring.mail.username=xxxxxxxxx@qq.com
#PO3/SMTP服務時郵箱的授權碼
spring.mail.password=xxxxxxxxxxxxxxxx
spring.mail.default-encoding=UTF-8
spring.mail.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
6.5、編寫
/**
* @author makeJava
*
* @create 2021-03-21日 19:50
* @describes 測試qq郵件前端層
*/
@Controller
@RequestBody
@Slf4j
@Api(tags="qq郵件前端層")
public class EmailController {
@Autowired
JavaMailSender mailSender;//注入QQ發送郵件的bean
//定義發送的內容 我這里發送一張圖片 需要html標簽
/**
* 查詢所有數據
*
* @param goodTestDTO 查詢實體
* @return 所有數據
*/
public static String body="";
@RequestMapping("/qqemail")
@ApiOperation(value = "定義發送的內容")
public Object qqemail(@RequestParam String qq,String title) {
try {
MimeMessage mimeMessage = this.mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setFrom("******@qq.com");//設置發件qq郵箱
qq+="@qq.com"; //補全地址
message.setTo(qq); //設置收件人
message.setSubject(title); //設置標題
message.setText(body,true); //第二個參數true表示使用HTML語言來編寫郵件

// FileSystemResource file = new FileSystemResource(
// File file = new File("圖片路徑");
// helper.addAttachment("圖片.jpg", file);//添加帶附件的郵件
// helper.addInline("picture",file);//添加帶靜態資源的郵件
this.mailSender.send(mimeMessage);
return "發送成功";
} catch (Exception ex) {
ex.printStackTrace();
return "發送成功";
}
}
}
6.6、編寫前端頁面
QQ郵件發送
7、啟動運行 瀏覽器輸入 :8080/.html
輸入qq號 和標題 點擊發送
已收到發送來的圖片
測試成功!