Java获取QQ邮箱的最近一封邮件的内容

前置条件

开启qq邮箱的授权码,使用授权码登录.

maven

        <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.15.3</version>
        </dependency>

代码示例

QQ内容Class

public class QqEmailContent {
    private String subject;  // 邮箱标题
    private String from;  // 邮箱发送者
    private String content;  // 邮箱内容

    public QqEmailContent(String subject, String from, String content) {
        this.subject = subject;
        this.from = from;
        this.content = content;
    }

    public QqEmailContent() {
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "QqEmailContent{" +
                "subject='" + subject + '\'' +
                ", from='" + from + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

获取QQ邮箱最近一封邮件,根据自己的需求更改服务"smtp.qq.com"和端口,我默认是关闭了加密

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class QQEmailUtils {
    protected static String username = "xxx@qq.com"; // 使用QQ邮箱账号
    protected static String password = "xxxx"; // 使用授权码

    public static QqEmailContent getFistMailContent() {
        QqEmailContent qqEmailContent = new QqEmailContent();
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "false"); // 关闭TLS加密
        props.put("mail.smtp.ssl.enable", "false"); // 关闭SSL加密

        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            Store store = session.getStore("imap"); // 使用IMAP协议
            store.connect("imap.qq.com", username, password); // 连接IMAP服务器

            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);

            int messageCount = inbox.getMessageCount(); // 获取邮件总数
            Message[] messages = inbox.getMessages(messageCount, messageCount); // 获取最新的一封邮件

            for (Message message : messages) {
                qqEmailContent.setSubject(message.getSubject());
                qqEmailContent.setFrom(String.valueOf(message.getFrom()[0]));
                qqEmailContent.setContent(parseRelatedMessage((MimeMessage) message));
                return qqEmailContent;

            }
            inbox.close(false);
            store.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public static String parseRelatedMessage(MimeMessage message) throws Exception {
        Multipart multipart = (Multipart) message.getContent();
        String mainContent = null;
        for (int i = 0; i < multipart.getCount(); i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            if (bodyPart.getContentType().toLowerCase().contains("related")) {
                // 获取主体内容
                mainContent = getText(bodyPart);
                assert mainContent != null;
                Document doc = null;
                try {
                    doc = Jsoup.parse(mainContent);
                } catch (Exception e) {
                    return mainContent;
                }
                mainContent = doc.text();
            }
        }
        return mainContent;
    }

    private static String getText(BodyPart bodyPart) throws Exception {
        if (bodyPart.getContent() instanceof String) {
            return (String) bodyPart.getContent();
        } else if (bodyPart.getContent() instanceof Multipart) {
            StringBuilder sb = new StringBuilder();
            Multipart multipart = (Multipart) bodyPart.getContent();
            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart part = multipart.getBodyPart(i);
                if (part.getDisposition() == null && (part.getContentType().contains("TEXT/PLAIN") || part.getContentType().contains("TEXT/HTML"))) {
                    sb.append(part.getContent().toString());
                }
            }
            return sb.toString();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getFistMailContent());
    }
}


实际运行验证

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Franciz小测测

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值