当前位置:网站首页>Email (including attachments, Netease, QQ)
Email (including attachments, Netease, QQ)
2022-07-19 05:08:00 【mabo_ [email protected]】
design sketch

Code
package com.mabo.email;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/** * @Author mabo * @Description smtp service * Send E-mail * The email address of the sender is : NetEase mail box 、QQ mailbox 、139 mailbox * Unlimited receiving mailbox * Recommended QQ Of smtp The server , Faster * It is not recommended to use 139 mailbox * 139 mailbox , Unable to send mail without attachments */
public class EmailTest{
//QQsmtp The server
private static final String QQHost="smtp.qq.com";
private static final String QQPort="587";
// NetEase smtp The server
private static final String WangYiHost="smtp.163.com";
private static final String WangYiPort="25";
//139 mailbox , Cannot send without attachments
private static final String E139Host="smtp.139.com";
private static final String E139Port="25";
public static void main(String[] args) throws Exception {
// The sender's email is qq mailbox
String username="[email protected]";
// To configure QQsmtp The token after the server succeeds
String password="xxxsfqvondjee";
// Email address 1
String receive1="[email protected]";
// Email address 2
String receive2="[email protected]";
// // The sender is Netease email
// String username = "[email protected]";
// // Configure Netease smtp The token after the server succeeds
// String password = "xxxHWSRPNFHI";
// // Email address 1
// String receive1="[email protected]";
// // The sender is 139 mailbox
// String username = "[email protected]";
// // Configure Netease smtp The token after the server succeeds
// String password = "xxx63ce95564ce00";
// // Email address 1
// String receive1="[email protected]";
// Attachments sent
List<String> list=new ArrayList<>();
list.add("F:/test.doc");
list.add("F:/testw copy .doc");
// Mail recipient collection
List<String> listReceiver=new ArrayList<>();
listReceiver.add(receive1);
listReceiver.add(receive2);
// Send mail without attachments
sendEmailNoFile(username,password,receive1," title , No attachment "," Text , No attachment ");
// Send with attachment , Messages with multiple recipients and attachments
sendEmail(username,password,listReceiver," title , With accessories "," Text , With accessories ",list);
System.out.println(" Send successfully ");
}
/** * @Author mabo * @Description E-mail , Include attachments * String sender, Sender email * String password , Mailbox token * List<String> receives, Recipient mailbox collection * String subject, title * String content, Text * List<String> files File name , by null Do not send attachments */
public static void sendEmail(String sender,String password ,List<String> receivers,String subject, String content,List<String> files) throws Exception {
String host;
String port;
if (sender.contains("@163.com")){
host =WangYiHost;
port =WangYiPort;
}
else if (sender.contains("@qq.com")){
host =QQHost;
port =QQPort;
}
else if (sender.contains("@139.com")){
host =E139Host;
port =E139Port;
}
else {
throw new Exception(" The current sender email does not support smtp The server ");
}
// establish Properties Class is used to record some properties of the mailbox
Properties props = new Properties();
// Express SMTP Send E-mail , Authentication is required
props.put("mail.smtp.auth", "true");
// Fill in here SMTP The server
props.put("mail.smtp.host", host);
// Port number ,QQ Mailbox port 587
props.put("mail.smtp.port", port);
// Fill in here , The account number of the writer
props.put("mail.user", sender);
// Fill in here 16 position STMP password
props.put("mail.password", password);
// Build authorization information , Used for SMTP Authentication
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// user name 、 password
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// Use environment properties and authorization information , Create a mail session
Session session = Session.getInstance(props, authenticator);
session.setDebug(true);
try {
List<File> attaches = new ArrayList<File>();
if (files!=null){
for (String file: files) {
File attach = new File(file);
attaches.add(attach);
}
}
// according to session establish MimeMessage
MimeMessage message = new MimeMessage(session);
// Sender
message.setFrom(new InternetAddress(sender));
// The recipient
for (String receive:
receivers) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));
}
// The theme
message.setSubject(subject);
Multipart multipart = new MimeMultipart();
// Message body
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(content, "text/html;charset=utf-8");
multipart.addBodyPart(contentPart);
// Email attachment
if(attaches.size()>0) {
for(File attachment : attaches) {
BodyPart attachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
attachmentPart.setDataHandler(new DataHandler(source));
// Avoid the processing of Chinese garbled code
attachmentPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
multipart.addBodyPart(attachmentPart);
}
}
message.setContent(multipart);
// Save message
message.saveChanges();
Transport.send(message);
} catch (AddressException e) {
throw new AddressException(" Receiving mailbox not found ");
} catch (MessagingException e) {
throw new MessagingException(" fail in send ");
} catch (UnsupportedEncodingException e) {
throw new UnsupportedEncodingException(" fail in send ");
}
}
/** * @Author mabo * @Description Send mail one-to-one , Do not include attachments * String sender, Sender email * String password , Mailbox token * String receives, Recipient email * String subject, title * String content, Text */
public static void sendEmailNoFile(String sender,String password ,String receiver,String subject, String content) throws Exception {
ArrayList<String> objects = new ArrayList<>();
objects.add(receiver);
sendEmail(sender,password,objects,subject,content,null);
}
}
How to open QQ mailbox SMTP service
NetEase and QQ Opening of mailbox SMTP The service settings are similar
- Enter the mailbox , Click Settings

- Click to enter the account or account security

- Glide down to find SMTP service , Click to open , The installation prompt step sending SMS is enabled


4. SMS verification succeeded , Get password , That is... In the code password, Then you can start using it in your code 
How to open Netease mailbox SMTP service
Enter the home page , Click Settings , Click on POP3/SMTP/IMAP

Click to open smtp service

According to the prompt , Send SMS verification code , Finally get the token , You can use smtp service
版权声明
本文为[mabo_ [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170502056989.html
边栏推荐
- POC——DVWA‘s XSS Reflected
- Implementation idea of log adding to database
- C语言初学者之初识代码专项练习
- 【C】 Beam calculator
- MySQL fuzzy matching 1, 11111 similar string problems
- 01_电影推荐(ContentBased)_物品画像
- POC——DVWA‘s SQL Injection
- 02_電影推薦(ContentBased)_用戶畫像
- 第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)
- IDL 读取葵花8(Himawari-8)HSD数据
猜你喜欢

Teddy Cup title a full version optimization update (4/23)

PyGame installation -requirement already satisfied

Harmonyos second training notes

ModelArts第二次培訓筆記

PCA feature dimensionality reduction of machine learning + case practice

用户登录-以及创建验短信证码

用户的管理-限制

mysql数据库实验实训5,数据查询yggl数据库查询(详细)

mysql数据库实验实训6,数据视图(详细)

【C语言_学习_考试_复习第三课】ASCII码与C语言概述
随机推荐
Simply and quickly establish a pytorch environment yolov5 target detection model to run (super simple)
【C】 Beam calculator
用户管理-分页
One article to understand Zipkin
IDL调用6S大气校正
MD5 password encryption
第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)
Harmonyos third training notes
Message converter (JSON)
SQL语句学习
基于SSM框架的考勤签到请假系统
学习C语言第8天
POC——DVWA‘s File Upload
Chat about global filter
读论文《SNUNet-CD: A Densely Connected Siamese Network for Change Detection of VHR Images》
学习C语言的第6天
机器学习之特征提取(类别特征进行数值化、离散化、文本特征进行数值化)
【p5.js】模拟烟花效果-交互媒体设计作业
【C语言—零基础第十三课】字符串的奥秘
PAT乙级1002:写出这个数