This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MailQQUtils.SIMON.java
71 lines (60 loc) · 1.76 KB
/
MailQQUtils.SIMON.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package org.xueyao.email;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailQQUtils {
/**
* 发送邮件
* @param to 接收邮件的地址
* @param subject 邮件主题
* @param msgText 邮件内容
*/
public static void send(String to, String subject, String msgText) {
//发邮件的地址
String from = "[email protected]";
String password = "授权码";
//邮件发送服务器地址
String host = "smtp.qq.com";
//是否开启debug模式
boolean debug = true;
// 设置发送邮件的配置信息
Properties props = new Properties();
props.put("mail.smtp.host", host);
if (debug) {
props.put("mail.debug", debug);
}
//添加auth认证
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
//邮件会话
Session session = Session.getInstance(props, null);
session.setDebug(debug);
try {
//创建邮件
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
//设置主题
msg.setSubject(subject);
//设置发送时间
msg.setSentDate(new Date());
//设置邮件的内容
msg.setText(msgText);
//发送邮件
Transport.send(msg,from,password);
} catch (Exception mex) {
mex.printStackTrace();
}
}
public static void main(String[] args) {
String to = "[email protected]";
String subject = "如何学习?";
String msgText = "解决学习困扰,就是天天晚上熬夜学习";
MailQQUtils.send(to, subject, msgText);
}
}