当前位置:网站首页>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
边栏推荐
- The code of yolov5 model for pest identification in Title A of the 10th Teddy cup data mining challenge (has been run through, original works, continuously updated)
- 【C语言—零基础第十一课】旋转大转盘之指针
- POC——DVWA‘s File Upload
- 【p5.js】模拟烟花效果-交互媒体设计作业
- The difference between junit4 and junit5
- 【C语言_学习_考试_复习第三课】ASCII码与C语言概述
- 泰迪杯A题完整版 优化更新(4/23)
- 学习C语言第三天
- 【Es6】利用添加数据,筛选并传输至页面等多项功能实现案例
- 硬核结构体,暴力解读
猜你喜欢

关于New_Online_Judge_1081_哥德巴赫猜想的思考

卷积神经网络

Harmonyos fourth training notes

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

一个问题的探讨

Learn about scheduled tasks in one article

CVE-2022-23131 Zabbix SAML SSO认证绕过漏洞

【LeetCode——编程能力入门第一天】基本数据类型[在区间范围内统计奇数数目/去掉最低工资和最高工资后的工资平均值)

Cve-2017-12635 CouchDB vertical privilege bypass vulnerability recurrence

【C语言—零基础第六课】输入输出语句格式与复合语句
随机推荐
第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)
HarmonyOS第三次培训笔记
Install MySQL
POC——DVWA‘s SQL Injection
SMS verification test without signature template audit
Hire the server, and the pytorch environment training yolov5 model tutorial deployed on pycharm professional edition. Server environment installation library file:
【Es6】利用添加数据,筛选并传输至页面等多项功能实现案例
PyGame aircraft War 1.0 (step + window no response problem)
Message converter (JSON)
关于New_Online_Judge_1081_哥德巴赫猜想的思考
硬核结构体,暴力解读
【C语言—零基础_学习_复习_第四课】数据类型及其运算
【2022第十届‘泰迪杯’挑战赛】A题:害虫识别完整版(大致思路。详细过程和代码以及结果csv在压缩包中)
Elment UI usage
Convolutional neural network
Es6 真实案例解构(多维数组对象)全新案例:
POC——DVWA‘s File Inclusion
Harmonyos second training notes
One article to understand Zipkin
泰迪杯A题完整版 优化更新(4/23)