一、电子邮件详解
假设自己的电子邮件是me@163.com,对方的邮件是you@163.com我们编写好文件填写好对方文件,点击发送,这些电子邮件就发出去了
而这些电子邮件被称为MUA:Mail User Agent——邮件用户代理。
Email发送出去的时候,不是直接到达对方的电脑,而是先发送到MTA:Mail Transfer Agent——邮件传输代理。如:网易 Email到达MTA后,MTA会把Emain投递到邮件的最终目的MDA:Mail Delivery Agent——邮件投递代理。如何存放在某个服务器上,我们将这个长期保存的地方叫做电子邮件邮箱。Email不会直接到达对方的电脑,因为电脑不一定开机,开机不一定对方要取邮件,必须通过MUA从MDA把邮件取到自己的电脑上面。
有了上述概念,编写程序来收发和接受文件,本质上就是:
1.编写MUA把邮件发送到MTA
2.编写MUA从MDA上收邮件
发邮件时,MUA和MTA使用的协议就是SMTP:Simple Mail Transfer Protocol,后面的MTA到另一个MTA也是用SMTP协议。
收邮件的时候,MUA和MDA使用的协议有两种:POP:Post Office Protocol,即POP3;IMAP:Internet Message Access Protocol
二、邮件发送
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; /** * 邮件发送 * * @author ltl */ public class SendEmailManger extends Thread { private String mailAdr;//邮箱 private String content;//邮件的内容 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 { //加密的邮件套接字协议工厂 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); final Properties props = new Properties(); // 表示SMTP发送邮件,需要进行身份验证 props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.qq.com"); // smtp登陆的账号、密码 ;需开启smtp登陆 props.setProperty("mail.debug", "true"); props.put("mail.user", "此处写你的qq邮箱"); props.put("mail.password", "此处写你的QQ授权码"); // 特别需要注意,要将ssl协议设置为true,否则会报530错误 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); } }; // 使用环境属性和授权信息,创建邮件会话 Session mailSession = Session.getInstance(props, authenticator); // 创建邮件消息 MimeMessage message = new MimeMessage(mailSession); // 设置发件人 try { InternetAddress form = new InternetAddress(props.getProperty("mail.user")); message.setFrom(form); // 设置收件人 InternetAddress to = new InternetAddress(mailAdr); message.setRecipient(Message.RecipientType.TO, to); // 设置邮件标题 message.setSubject(subject); // 设置邮件的内容体 message.setContent(content, "text/html;charset=UTF-8"); // 发送邮件 Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); } } public static void main(String[] args) { // 1. 创建一个顶层容器(窗口) JFrame jf = new JFrame("发送邮件"); jf.setSize(500, 500); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 2. 创建中间容器(面板容器) JPanel panel = new JPanel(null); // 3. 创建一个基本组件(按钮),并添加到 面板容器 中 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("邮件标题: "); 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("邮件内容: "); 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("发送邮件"); 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. 把 面板容器 作为窗口的内容面板 设置到 窗口 jf.setContentPane(panel); // 5. 显示窗口,前面创建的信息都在内存中,通过 jf.setVisible(true) 把内存中的窗口显示在屏幕上。 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. 创建一个顶层容器(窗口) 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("标题: "); 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("邮件内容: "); 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. 把 面板容器 作为窗口的内容面板 设置到 窗口 jf.setContentPane(panel); // 5. 显示窗口,前面创建的信息都在内存中,通过 jf.setVisible(true) 把内存中的窗口显示在屏幕上。 jf.setVisible(true); } public String[] ReceiveEmailManger() throws Exception { //加密的邮件套接字协议工厂 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); String pop3Server = "pop.qq.com"; String protocol = "pop3"; String username = "此处填写qq邮箱"; String password = "此处填写QQ授权码"; Properties prop = new Properties(); prop.put("mail.store.protocol", protocol); prop.put("mail.pop3.host", pop3Server); // 特别需要注意,要将ssl协议设置为true,否则会报530错误 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); //登录验证 store.connect(pop3Server,username,password); //获得用户的邮件账户,注意通过pop3协议获取某个邮件夹的名称只能为inbox Folder folder = store.getFolder("inbox"); //设置访问权限 folder.open(Folder.READ_ONLY); //获取所有邮件 int size = folder.getMessageCount(); Message message = folder.getMessage(size); //获取第一封发件人地址 String from = message.getFrom()[0].toString(); //获取第一封邮件标题 String subject = message.getSubject(); //获取第一封邮件内容 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; } }
四、导包
使用IDEA编程,在pom.xml文件下导入依赖包
<!-- java发送邮件jar包 --> <dependencies> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> </dependencies>