Java實現(xiàn)簡單的郵件發(fā)送功能
本文實例為大家分享了Java實現(xiàn)簡單的郵件發(fā)送的具體代碼,供大家參考,具體內(nèi)容如下
要注意只有開啟了POP3/SMTP才能收發(fā)郵件,首先要開啟此功能
** qq郵箱的開啟方式**
設置——》賬戶——》找到下圖的的信息,并開啟

網(wǎng)易163郵箱的開啟方式
設置——》POP3/SMTP/IMAP

當開啟了之后就可以收發(fā)郵件了
Java代碼
package cn.itcast.travel.util;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
?* 發(fā)郵件工具類
?*/
public final class MailUtils {
? ? private static final String USER = ""; // 發(fā)件人稱號,同郵箱地址
? ? private static final String PASSWORD = ""; // 如果是qq郵箱可以使戶端授權碼,或者登錄密碼
? ? /**
? ? ?*
? ? ?* @param to 收件人郵箱
? ? ?* @param text 郵件正文
? ? ?* @param title 標題
? ? ?*/
? ? /* 發(fā)送驗證信息的郵件 */
? ? public static boolean sendMail(String to, String text, String title){
? ? ? ? try {
? ? ? ? ? ? final Properties props = new Properties();
? ? ? ? ? ? props.put("mail.smtp.auth", "true");
? ? ? ? ? ? props.put("mail.smtp.host", "smtp.qq.com");
? ? ? ? ? ? // 發(fā)件人的賬號
? ? ? ? ? ? props.put("mail.user", USER);
? ? ? ? ? ? //發(fā)件人的密碼
? ? ? ? ? ? props.put("mail.password", PASSWORD);
? ? ? ? ? ? // 構建授權信息,用于進行SMTP進行身份驗證
? ? ? ? ? ? Authenticator authenticator = new Authenticator() {
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? protected PasswordAuthentication getPasswordAuthentication() {
? ? ? ? ? ? ? ? ? ? // 用戶名、密碼
? ? ? ? ? ? ? ? ? ? String userName = props.getProperty("mail.user");
? ? ? ? ? ? ? ? ? ? String password = props.getProperty("mail.password");
? ? ? ? ? ? ? ? ? ? return new PasswordAuthentication(userName, password);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? ? ? // 使用環(huán)境屬性和授權信息,創(chuàng)建郵件會話
? ? ? ? ? ? Session mailSession = Session.getInstance(props, authenticator);
? ? ? ? ? ? // 創(chuàng)建郵件消息
? ? ? ? ? ? MimeMessage message = new MimeMessage(mailSession);
? ? ? ? ? ? // 設置發(fā)件人
? ? ? ? ? ? String username = props.getProperty("mail.user");
? ? ? ? ? ? InternetAddress form = new InternetAddress(username);
? ? ? ? ? ? message.setFrom(form);
? ? ? ? ? ? // 設置收件人
? ? ? ? ? ? InternetAddress toAddress = new InternetAddress(to);
? ? ? ? ? ? message.setRecipient(Message.RecipientType.TO, toAddress);
? ? ? ? ? ? // 設置郵件標題
? ? ? ? ? ? message.setSubject(title);
? ? ? ? ? ? // 設置郵件的內(nèi)容體
? ? ? ? ? ? message.setContent(text, "text/html;charset=UTF-8");
? ? ? ? ? ? // 發(fā)送郵件
? ? ? ? ? ? Transport.send(message);
? ? ? ? ? ? return true;
? ? ? ? }catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return false;
? ? }
? ? public static void main(String[] args) throws Exception { // 做測試用
? ? ? ? MailUtils.sendMail("","測試郵件,無需回復!","測試郵件");
? ? ? ? System.out.println("發(fā)送成功");
? ? }
}這里填寫發(fā)件人的郵箱和授權碼,如果是qq郵箱則使用qq密碼即可


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
springboot日志文件名稱叫l(wèi)ogback-spring.xml的原因解析
這篇文章主要介紹了springboot日志文件名稱為什么叫l(wèi)ogback-spring.xml,本文給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
使用maven方式創(chuàng)建springboot項目的方式
使用Spring Initializr創(chuàng)建spring boot項目,因為外網(wǎng)問題導致很難成功,所以只能使用maven方式,這里介紹下使用maven方式創(chuàng)建springboot項目的方法,感興趣的朋友一起看看吧2022-09-09
Spring Boot Actuator執(zhí)行器運行原理詳解
這篇文章主要介紹了Spring Boot Actuator執(zhí)行器運行原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03

