JavaMail:java使用QQ郵箱傳送郵件簡單版。
java使用QQ郵箱傳送郵件簡單版。
1. 新增需要的依賴
<!-- JavaMail -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<!-- JavaMail -->
2. QQ郵箱配置
登入到qq郵箱,進入到設定,賬戶管理。找到以下配置
3. 主要程式碼
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;
import com.personal.front.base.util.Email_Util;
public class EmailSendUtil {
public static void main(String args[]) {
try {
// send_email("","");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void send_email(String email, String msg) throws IOException, AddressException, MessagingException {
String to = email; //傳送到哪個郵箱
String subject = "有新留言!"; //郵件標題
String content = msg; //郵件內容
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_Util("xxxxx@qq.com(填你配置的qq郵箱)", "xx(對應授權碼)");//
javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
MimeMessage mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress("xxxx@qq.com"));//傳送郵件的地址/就填你的qq郵箱
// 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();
// 建立一個包含HTML內容的MimeBodyPart
BodyPart html = new MimeBodyPart();
html.setContent(content.trim(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
}
}
4. 引用到的工具類
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Util extends Authenticator {
String userName = null;
String password = null;
public Email_Util() {
}
public Email_Util(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName, password);
}
}
結束。
相關文章
- golang傳送郵件(qq郵箱)Golang
- spring boot配置QQ郵箱傳送郵件Spring Boot
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- Springboot+Javamail實現郵件傳送Spring BootJavaAI
- win10郵件怎麼用qq郵箱 win10郵件如何使用qq郵箱Win10
- 谷歌郵箱,配置傳送郵件密碼谷歌密碼
- 如何使用 ABAP 程式碼傳送郵件到指定郵箱試讀版
- java郵件傳送Java
- Java實現網易企業163郵箱傳送郵件Java
- 電子郵件協議及GO傳送QQ郵件協議Go
- 如何傳送電子郵件到別人郵箱?電子郵件傳送的方法
- qq郵箱怎麼發檔案給別的郵箱 qq郵箱如何將文件傳送給別人
- Spring Boot實現傳送QQ郵件Spring Boot
- php 傳送郵件(以QQ為例)PHP
- Linux Centos7傳送QQ郵件LinuxCentOS
- 使用 smtplib 傳送郵件
- 使用phpmailer傳送郵件PHPAI
- 郵件和簡訊傳送
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- JavaMailSender怎麼傳送163和qq郵件JavaAI
- 傳送郵件
- 郵件傳送
- win10自帶郵件怎麼新增qq郵箱_win10郵箱如何匯入qq郵箱Win10
- win10使用自帶郵箱傳送郵件時直接進傳送箱裡無法完成傳送怎麼解決Win10
- 一次性解決python smtp 傳送outlook郵件,163郵件,qq郵件等等.Python
- 使用node的emailjs傳送郵箱AIJS
- 如何使用Excel傳送郵件?Excel
- 使用阿里雲傳送郵件阿里
- 【Stmplib】Python正確模擬傳送QQ郵件Python
- 郵箱傳送模組
- SpringBoot傳送郵件Spring Boot
- phpcms傳送郵件PHP
- 郵件的傳送
- Laravel傳送郵件Laravel
- Laravel 傳送郵件Laravel
- thinkjs 傳送郵件JS
- PHP傳送郵件PHP
- nodejs 傳送郵件NodeJS