Java實(shí)戰(zhàn)之基于swing的QQ郵件收發(fā)功能實(shí)現(xiàn)
一、電子郵件詳解
- 假設(shè)自己的電子郵件是me@163.com,對(duì)方的郵件是you@163.com
我們編寫好文件填寫好對(duì)方文件,點(diǎn)擊發(fā)送,這些電子郵件就發(fā)出去了
而這些電子郵件被稱為MUA:Mail User Agent——郵件用戶代理。
- Email發(fā)送出去的時(shí)候,不是直接到達(dá)對(duì)方的電腦,而是先發(fā)送到MTA:Mail Transfer Agent——郵件傳輸代理。如:網(wǎng)易
- Email到達(dá)MTA后,MTA會(huì)把Emain投遞到郵件的最終目的MDA:Mail Delivery Agent——郵件投遞代理。如何存放在某個(gè)服務(wù)器上,我們將這個(gè)長(zhǎng)期保存的地方叫做電子郵件郵箱。
Email不會(huì)直接到達(dá)對(duì)方的電腦,因?yàn)殡娔X不一定開機(jī),開機(jī)不一定對(duì)方要取郵件,必須通過MUA從MDA把郵件取到自己的電腦上面。
有了上述概念,編寫程序來收發(fā)和接受文件,本質(zhì)上就是:
1.編寫MUA把郵件發(fā)送到MTA
2.編寫MUA從MDA上收郵件
發(fā)郵件時(shí),MUA和MTA使用的協(xié)議就是SMTP:Simple Mail Transfer Protocol,后面的MTA到另一個(gè)MTA也是用SMTP協(xié)議。
收郵件的時(shí)候,MUA和MDA使用的協(xié)議有兩種:POP:Post Office Protocol,即POP3;IMAP:Internet Message Access Protocol
二、郵件發(fā)送
import com.sun.mail.util.MailSSLSocketFactory;
import javax.swing.*;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
/**
* 郵件發(fā)送
*
* @author ltl
*/
public class SendEmailManger extends Thread {
private String mailAdr;//郵箱
private String content;//郵件的內(nèi)容
private String subject;//郵件的題目
public SendEmailManger(String mailAdr, String subject, String content) {
super();
this.mailAdr = mailAdr;
this.subject = subject;
this.content = content;
}
@Override
public void run() {
super.run();
try {
sendMail(mailAdr, subject, content);
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendMail(String mailAdr, String subject, String content) throws Exception {
//加密的郵件套接字協(xié)議工廠
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
final Properties props = new Properties();
// 表示SMTP發(fā)送郵件,需要進(jìn)行身份驗(yàn)證
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
// smtp登陸的賬號(hào)、密碼 ;需開啟smtp登陸
props.setProperty("mail.debug", "true");
props.put("mail.user", "此處寫你的qq郵箱");
props.put("mail.password", "此處寫你的QQ授權(quán)碼");
// 特別需要注意,要將ssl協(xié)議設(shè)置為true,否則會(huì)報(bào)530錯(cuò)誤
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用戶名、密碼
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用環(huán)境屬性和授權(quán)信息,創(chuàng)建郵件會(huì)話
Session mailSession = Session.getInstance(props, authenticator);
// 創(chuàng)建郵件消息
MimeMessage message = new MimeMessage(mailSession);
// 設(shè)置發(fā)件人
try {
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
// 設(shè)置收件人
InternetAddress to = new InternetAddress(mailAdr);
message.setRecipient(Message.RecipientType.TO, to);
// 設(shè)置郵件標(biāo)題
message.setSubject(subject);
// 設(shè)置郵件的內(nèi)容體
message.setContent(content, "text/html;charset=UTF-8");
// 發(fā)送郵件
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 1. 創(chuàng)建一個(gè)頂層容器(窗口)
JFrame jf = new JFrame("發(fā)送郵件");
jf.setSize(500, 500);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 2. 創(chuàng)建中間容器(面板容器)
JPanel panel = new JPanel(null);
// 3. 創(chuàng)建一個(gè)基本組件(按鈕),并添加到 面板容器 中
JLabel jla1 = new JLabel("收件郵箱: ");
jla1.setLocation(50,50);
jla1.setSize(100, 50);
jla1.setFont(new Font("123", 5, 20));
final JTextField textField = new JTextField(8);
textField.setFont(new Font("mailAdr", Font.PLAIN, 20));
textField.setLocation(150,50);
textField.setSize(250, 50);
JLabel jla2 = new JLabel("郵件標(biāo)題: ");
jla2.setLocation(50,150);
jla2.setSize(100, 50);
jla2.setFont(new Font("123", 5, 20));
final JTextField textField1 = new JTextField(8);
textField1.setFont(new Font("subject", Font.PLAIN, 20));
textField1.setLocation(150,150);
textField1.setSize(250, 50);
JLabel jla3 = new JLabel("郵件內(nèi)容: ");
jla3.setLocation(50,250);
jla3.setSize(100, 50);
jla3.setFont(new Font("123", 5, 20));
final JTextField textField3 = new JTextField(8);
textField3.setFont(new Font("content", Font.PLAIN, 20));
textField3.setLocation(150,250);
textField3.setSize(250, 50);
JButton btn = new JButton("發(fā)送郵件");
btn.setLocation(50,350);
btn.setSize(100, 50);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SendEmailManger d = new SendEmailManger(textField.getText(), textField1.getText(), textField3.getText());
d.start();
}
});
panel.add(jla1);
panel.add(jla2);
panel.add(jla3);
panel.add(textField);
panel.add(textField1);
panel.add(textField3);
panel.add(btn);
// 4. 把 面板容器 作為窗口的內(nèi)容面板 設(shè)置到 窗口
jf.setContentPane(panel);
// 5. 顯示窗口,前面創(chuàng)建的信息都在內(nèi)存中,通過 jf.setVisible(true) 把內(nèi)存中的窗口顯示在屏幕上。
jf.setVisible(true);
}
}
三、郵件接收
import com.sun.mail.util.MailSSLSocketFactory;
import com.sun.org.apache.bcel.internal.generic.NEW;
import javax.mail.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;
public class ReceiveEmailManger extends Thread {
public static void main(String[] args){
// 1. 創(chuàng)建一個(gè)頂層容器(窗口)
JFrame jf = new JFrame("接收郵件");
jf.setSize(500, 500);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel(null);
JLabel jla2 = new JLabel("標(biāo)題: ");
jla2.setLocation(50,50);
jla2.setSize(150, 50);
jla2.setFont(new Font("123", 5, 20));
final JTextField textField1 = new JTextField(8);
textField1.setFont(new Font("subject", Font.PLAIN, 20));
textField1.setLocation(150,50);
textField1.setSize(250, 50);
JLabel jla3 = new JLabel("郵件內(nèi)容: ");
jla3.setLocation(50,150);
jla3.setSize(150, 50);
jla3.setFont(new Font("123", 5, 20));
final JTextField textField3 = new JTextField(8);
textField3.setFont(new Font("content", Font.PLAIN, 20));
textField3.setLocation(150,150);
textField3.setSize(250, 50);
final JTextArea jta = new JTextArea();
jta.setLocation(150,150);
jta.setSize(250, 200);
jta.setFont(new Font("content", Font.PLAIN, 30));
JButton btn = new JButton("接收郵件");
btn.setLocation(150,380);
btn.setSize(100, 50);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ReceiveEmailManger re = new ReceiveEmailManger();
String [] mess = re.ReceiveEmailManger();
textField1.setText(mess[1]);
jta.setText(mess[2]);
}catch (Exception ea){
ea.printStackTrace();
}
}
});
panel.add(jla2);
panel.add(jla3);
panel.add(textField1);
panel.add(btn);
panel.add(jta);
// 4. 把 面板容器 作為窗口的內(nèi)容面板 設(shè)置到 窗口
jf.setContentPane(panel);
// 5. 顯示窗口,前面創(chuàng)建的信息都在內(nèi)存中,通過 jf.setVisible(true) 把內(nèi)存中的窗口顯示在屏幕上。
jf.setVisible(true);
}
public String[] ReceiveEmailManger() throws Exception {
//加密的郵件套接字協(xié)議工廠
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
String pop3Server = "pop.qq.com";
String protocol = "pop3";
String username = "此處填寫qq郵箱";
String password = "此處填寫QQ授權(quán)碼";
Properties prop = new Properties();
prop.put("mail.store.protocol", protocol);
prop.put("mail.pop3.host", pop3Server);
// 特別需要注意,要將ssl協(xié)議設(shè)置為true,否則會(huì)報(bào)530錯(cuò)誤
prop.put("mail.pop3.ssl.enable", "true");
prop.put("mail.pop3.ssl.socketFactory", sf);
Session mailSession = Session.getDefaultInstance(prop,null);
mailSession.setDebug(false);
String [] mess = new String[3];
try {
Store store = mailSession.getStore(protocol);
//登錄驗(yàn)證
store.connect(pop3Server,username,password);
//獲得用戶的郵件賬戶,注意通過pop3協(xié)議獲取某個(gè)郵件夾的名稱只能為inbox
Folder folder = store.getFolder("inbox");
//設(shè)置訪問權(quán)限
folder.open(Folder.READ_ONLY);
//獲取所有郵件
int size = folder.getMessageCount();
Message message = folder.getMessage(size);
//獲取第一封發(fā)件人地址
String from = message.getFrom()[0].toString();
//獲取第一封郵件標(biāo)題
String subject = message.getSubject();
//獲取第一封郵件內(nèi)容
String conten = message.getContent().toString();
mess[0]=from;
mess[1]=subject;
mess[2]=conten;
folder.close(false);
store.close();
}catch(Exception e) {
e.printStackTrace();
}
return mess;
}
}
四、導(dǎo)包
使用IDEA編程,在pom.xml文件下導(dǎo)入依賴包
<!-- java發(fā)送郵件jar包 -->
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
到此這篇關(guān)于Java實(shí)戰(zhàn)之基于swing的QQ郵件收發(fā)功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)基于Java swing的QQ郵件收發(fā)功能實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Netty實(shí)現(xiàn)類似Dubbo的遠(yuǎn)程接口調(diào)用的實(shí)現(xiàn)方法
本文介紹了如何使用Netty框架實(shí)現(xiàn)類似Dubbo的遠(yuǎn)程接口調(diào)用,通過自定義編解碼器、通信協(xié)議和服務(wù)注冊(cè)中心等實(shí)現(xiàn)遠(yuǎn)程通信和服務(wù)治理。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04
基于Java事件監(jiān)聽編寫一個(gè)中秋猜燈謎小游戲
眾所周知,JavaSwing是Java中關(guān)于窗口開發(fā)的一個(gè)工具包,可以開發(fā)一些窗口程序,然后由于工具包的一些限制,導(dǎo)致Java在窗口開發(fā)商并沒有太多優(yōu)勢(shì),不過,在JavaSwing中關(guān)于事件的監(jiān)聽機(jī)制是我們需要重點(diǎn)掌握的內(nèi)容,本文將基于Java事件監(jiān)聽編寫一個(gè)中秋猜燈謎小游戲2023-09-09
基于Java實(shí)現(xiàn)XML文件的解析與更新
配置文件可以有很多種格式,包括?INI、JSON、YAML?和?XML。每一種編程語言解析這些格式的方式都不同。本文將通過Java語言實(shí)現(xiàn)XML文件的解析與更新,需要的可以參考一下2022-03-03
Java中的ThreadPoolExecutor線程池原理細(xì)節(jié)解析
這篇文章主要介紹了Java中的ThreadPoolExecutor線程池原理細(xì)節(jié)解析,ThreadPoolExecutor是一個(gè)線程池,最多可使用7個(gè)參數(shù)來控制線程池的生成,使用線程池可以避免創(chuàng)建和銷毀線程的資源損耗,提高響應(yīng)速度,并且可以管理線程池中線程的數(shù)量和狀態(tài)等等,需要的朋友可以參考下2023-12-12
Java線程池并發(fā)執(zhí)行多個(gè)任務(wù)方式
這篇文章主要介紹了Java線程池并發(fā)執(zhí)行多個(gè)任務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

