java mail使用qq郵箱發(fā)郵件的配置方法
程序入口:
Test_Email_N.java
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Test_Email_N {
public static void main(String args[]){
try {
send_email();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void send_email() throws IOException, AddressException, MessagingException{
String to = "1219999@qq.com";
String subject = "subject";
String content = "content";
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.qq.com");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true");
Authenticator authenticator = new Email_Authenticator("1219999@qq.com", "password");
javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
MimeMessage mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress("1219999@qq.com"));
// Message.RecipientType.TO屬性表示接收者的類型為TO
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
mailMessage.setSubject(subject, "UTF-8");
mailMessage.setSentDate(new Date());
// MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象
Multipart mainPart = new MimeMultipart();
// 創(chuàng)建一個包含HTML內(nèi)容的MimeBodyPart
BodyPart html = new MimeBodyPart();
html.setContent(content.trim(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
}
}
其中依賴的jar包為javax.mail,我這里是maven管理的,直接用maven去下載jar包,也可以到https://java.net/projects/javamail/pages/Home直接下載jar包.
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
Email_Authenticator.java,這里繼承了Authenticator 類,用來封裝name,和password的:
package com.infomorrow.webtest.JuxinliTest.restdetect;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Authenticator extends Authenticator {
String userName = null;
String password = null;
public Email_Authenticator() {
}
public Email_Authenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
配置就這么多,把郵箱密碼改成自己的就可以了,否則會報錯。程序到這就可以運行了!
下面介紹的是配置properties文件來管理賬號密碼:
新建一個email.propertis文件。
email.propertis:
mail.smtp.host=smtp.qq.com
mail.smtp.port=25
username=1219999@qq.com
password=password
Test_Email.java 代碼改為如下:
package com.infomorrow.webtest.JuxinliTest.restdetect;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Test_Email {
public static void main(String args[]){
try {
send_email();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void send_email() throws IOException, AddressException, MessagingException{
String to = "1215186706@qq.com";
String subject = "subject";//郵件主題
String content = "content";//郵件內(nèi)容
Properties properties = new Properties();
InputStream resourceAsStream = null;
try {
resourceAsStream = Object.class.getResourceAsStream("/email.properties");
properties.load(resourceAsStream);
} finally{
if (resourceAsStream!=null) {
resourceAsStream.close();
}
}
System.err.println("properties:"+properties);
properties.put("mail.smtp.host", properties.get("mail.smtp.host"));
properties.put("mail.smtp.port", properties.get("mail.smtp.port"));
properties.put("mail.smtp.auth", "true");
Authenticator authenticator = new Email_Authenticator(properties.get("username").toString(), properties.get("password").toString());
javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
MimeMessage mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress(properties.get("username").toString()));
// Message.RecipientType.TO屬性表示接收者的類型為TO
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
mailMessage.setSubject(subject, "UTF-8");
mailMessage.setSentDate(new Date());
// MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象
Multipart mainPart = new MimeMultipart();
// 創(chuàng)建一個包含HTML內(nèi)容的MimeBodyPart
BodyPart html = new MimeBodyPart();
html.setContent(content.trim(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
}
}
ok,到此為止。
相關文章
SpringMVC HttpMessageConverter消息轉換器
??HttpMessageConverter???,報文信息轉換器,將請求報文轉換為Java對象,或?qū)ava對象轉換為響應報文。???HttpMessageConverter???提供了兩個注解和兩個類型:??@RequestBody,@ResponseBody???,??RequestEntity,ResponseEntity??2023-04-04
MyBatis標簽之Select?resultType和resultMap詳解
這篇文章主要介紹了MyBatis標簽之Select?resultType和resultMap,在MyBatis中有一個ResultMap標簽,它是為了映射select標簽查詢出來的結果集,下面使用一個簡單的例子,來介紹 resultMap 的使用方法,需要的朋友可以參考下2022-09-09
SpringBoot2 整合 ClickHouse數(shù)據(jù)庫案例解析
這篇文章主要介紹了SpringBoot2 整合 ClickHouse數(shù)據(jù)庫案例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10
jmeter壓力測試工具簡介_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了jmeter壓力測試工具相關介紹資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
解決運行jar包出錯:ClassNotFoundException問題
這篇文章主要介紹了解決運行jar包出錯:ClassNotFoundException問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Spring security實現(xiàn)對賬戶進行加密
這篇文章主要介紹了Spring security實現(xiàn)對賬戶進行加密,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
selenium-java實現(xiàn)自動登錄跳轉頁面方式
利用Selenium和Java語言可以編寫一個腳本自動刷新網(wǎng)頁,首先,需要確保Google瀏覽器和Chrome-Driver驅(qū)動的版本一致,通過指定網(wǎng)站下載對應版本的瀏覽器和驅(qū)動,在Maven項目中添加依賴,編寫腳本實現(xiàn)網(wǎng)頁的自動刷新,此方法適用于需要頻繁刷新網(wǎng)頁的場景,簡化了操作,提高了效率2024-11-11

