Java uses qq mailbox to send and receive mail

Java uses qq mailbox to send and receive mail

1. Import the required jar package:
link: click here
insert image description here

Just download the jar package and import it into the corresponding project. How to import the package will not be introduced too much.
2. Log in to the official website of QQ mailbox
①Find the account in the mailbox setting
insert image description here

②Find "POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV service" and open the IMAP/SMTP service (for the specific meaning of the service, please refer to: the difference and connection between POP3, SMTP and IMAP)
insert image description here

3. Obtain the authorization code through mobile SMS verification, and write down the authorization code
insert image description here insert image description here

4. Specific code implementation:

Send email using SMTP protocol


package com.souvc.mail;
 
import java.util.Date;
import java.util.Properties;
import java.util.UUID;
 
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
/**
 * souvc.com
 */
 
public class SendMail {
    
    
 
    private static SendMail instance = null;
 
    private SendMail() {
    
    
 
    }
 
    public static SendMail getInstance() {
    
    
        if (instance == null) {
    
    
            instance = new SendMail();
        }
        return instance;
    }
 
    public void send() {
    
    
        try {
    
    
 
            // 接收者的邮箱
            String to[] = {
    
     "[email protected]" };
 
            // 配置发送邮箱的配置--
            Properties p = new Properties();
            p.put("mail.smtp.auth", "true");
            p.put("mail.transport.protocol", "smtp");
            p.put("mail.smtp.host", "smtp.163.com");
            p.put("mail.smtp.port", "25");
 
            // 建立会话
            Session session = Session.getInstance(p);
            // 建立信息
            Message msg = new MimeMessage(session);
            // 发件人
            msg.setFrom(new InternetAddress("[email protected]"));
            // 收件人
            String toList = getMailList(to);
            InternetAddress[] iaToList = new InternetAddress().parse(toList);
            msg.setRecipients(Message.RecipientType.TO, iaToList);
            // 发送日期
            msg.setSentDate(new Date());
            // 主题
            msg.setSubject("测试邮件");
            // 内容
            msg.setText("注意,这是测试程序发的,请不要回复!");
            // 邮件服务器进行验证
            Transport tran = session.getTransport("smtp");
            // *配置发送者的邮箱账户名和密码
            tran.connect("smtp.163.com", "[email protected]", "mima");
            // 发送
            tran.sendMessage(msg, msg.getAllRecipients());
            System.out.println("邮件发送成功");
 
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
 
    private String getMailList(String[] mailArray) {
    
    
 
        StringBuffer toList = new StringBuffer();
        int length = mailArray.length;
        if (mailArray != null && length < 2) {
    
    
            toList.append(mailArray[0]);
        } else {
    
    
            for (int i = 0; i < length; i++) {
    
    
                toList.append(mailArray[i]);
                if (i != (length - 1)) {
    
    
                    toList.append(",");
                }
            }
        }
        return toList.toString();
 
    }
 
    public static void main(String[] args) {
    
    
        SendMail sendMail = SendMail.getInstance();
        sendMail.send();
        // System.out.println(System.nanoTime());
        // UUID uuid = UUID.randomUUID();
        // System.out.println(uuid);
    }
}

Receive and parse emails using the IMAP protocol

package com.souvc.controller;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
 
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeUtility;
 
import com.sun.mail.imap.IMAPMessage;
 
/**
 * <b>使用IMAP协议接收邮件</b><br/>
 * <p>POP3和IMAP协议的区别:</p>
 * <b>POP3</b>协议允许电子邮件客户端下载服务器上的邮件,但是在客户端的操作(如移动邮件、标记已读等),不会反馈到服务器上,<br/>
 * 比如通过客户端收取了邮箱中的3封邮件并移动到其它文件夹,邮箱服务器上的这些邮件是没有同时被移动的。<br/>
 * <p><b>IMAP</b>协议提供webmail与电子邮件客户端之间的双向通信,客户端的操作都会同步反应到服务器上,对邮件进行的操作,服务
 * 上的邮件也会做相应的动作。比如在客户端收取了邮箱中的3封邮件,并将其中一封标记为已读,将另外两封标记为删除,这些操作会
 * 即时反馈到服务器上。</p>
 * <p>两种协议相比,IMAP 整体上为用户带来更为便捷和可靠的体验。POP3更易丢失邮件或多次下载相同的邮件,但IMAP通过邮件客户端
 * 与webmail之间的双向同步功能很好地避免了这些问题。</p>
 */
public class IMAPReceiveMailTest {
    
    
 
    public static void main(String[] args) throws Exception {
    
    
        // 准备连接服务器的会话信息
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imap");
        props.setProperty("mail.imap.host", "imap.163.com");
        props.setProperty("mail.imap.port", "143");
 
        // 创建Session实例对象
        Session session = Session.getInstance(props);
 
        // 创建IMAP协议的Store对象
        Store store = session.getStore("imap");
 
        // 连接邮件服务器
        store.connect(null,"[email protected]", "xxxx");
 
        // 获得收件箱
        Folder folder = store.getFolder("INBOX");
        // 以读写模式打开收件箱
        folder.open(Folder.READ_WRITE);
 
        // 获得收件箱的邮件列表
        Message[] messages = folder.getMessages();
 
        // 打印不同状态的邮件数量
        System.out.println("收件箱中共" + messages.length + "封邮件!");
        System.out.println("收件箱中共" + folder.getUnreadMessageCount() + "封未读邮件!");
        System.out.println("收件箱中共" + folder.getNewMessageCount() + "封新邮件!");
        System.out.println("收件箱中共" + folder.getDeletedMessageCount() + "封已删除邮件!");
 
        System.out.println("------------------------开始解析邮件----------------------------------");
 
        // 解析邮件
        for (Message message : messages) {
    
    
            IMAPMessage msg = (IMAPMessage) message;
            String subject = MimeUtility.decodeText(msg.getSubject());
            System.out.println("[" + subject + "]未读,是否需要阅读此邮件(yes/no)?");
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String answer = reader.readLine();
            if ("yes".equalsIgnoreCase(answer)) {
    
    
                POP3ReceiveMailTest.parseMessage(msg);    // 解析邮件
                // 第二个参数如果设置为true,则将修改反馈给服务器。false则不反馈给服务器
                msg.setFlag(Flag.SEEN, true);    //设置已读标志
            }
        }
 
        // 关闭资源
        folder.close(false);
        store.close();
    }
}

Note: Remember to modify the corresponding configuration in the above code


This is the end of today's sharing

Welcome to like and comment

insert image description here

Guess you like

Origin blog.csdn.net/nings666/article/details/130303303