JavaMail使用imap协议连接邮箱报错问题解决

2023-12-12 596点热度 0人点赞 0条评论

用JavaMail的imap协议连接邮箱并收取邮件,代码如下:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.search.SubjectTerm;
import org.springframework.stereotype.Service;

import java.util.Properties;

@Service
public class EmailService {

    public void searchEmailsBySubject(String subjectPrefix) {
        // 邮箱配置信息
        String host = "your_imap_server"; // 更换为你的 IMAP 服务器地址
        String username = "[email protected]";
        String password = "your_email_password";

        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imaps");
        properties.put("mail.imaps.host", host);
        properties.put("mail.imaps.port", "993");
        properties.put("mail.imaps.starttls.enable", "true");

        // 创建会话
        Session session = Session.getInstance(properties);

        try {
            // 连接到邮箱
            Store store = session.getStore("imaps");
            store.connect(host, username, password);

            // 获取收件箱
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);

            // 设置搜索条件 - 根据邮件标题搜索
            SearchTerm subjectTerm = new SubjectTerm(subjectPrefix);

            // 执行搜索
            Message[] messages = inbox.search(subjectTerm);

            // 处理搜索到的邮件
            for (Message message : messages) {
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + InternetAddress.toString(message.getFrom()));
                System.out.println("To: " + InternetAddress.toString(message.getRecipients(Message.RecipientType.TO)));
                System.out.println("----------------------------------------");
            }

            // 关闭连接
            inbox.close(false);
            store.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以上代码在有些电脑能运行,有些电脑上报错如下:

javax.mail.MessagingException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate);
  nested exception is:
    javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:695)
    at javax.mail.Service.connect(Service.java:345)
    at javax.mail.Service.connect(Service.java:226)
    at com.terrynow.test.Test2.main(Test2.java:46)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:172)
    at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:103)
    at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:247)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:448)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:426)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:532)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:337)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:116)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:121)
    at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:710)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:659)
    ... 3 more

经查,这个错误通常是因为邮件服务器使用的加密协议与 Java 程序的默认配置不兼容所导致的,手动设置 SSL/TLS 协议的属性,便可以解决,代码如下:

Properties properties = new Properties();
properties.put("mail.store.protocol", "imaps");
properties.put("mail.imaps.host", host);
properties.put("mail.imaps.port", "993");
properties.put("mail.imaps.starttls.enable", "true");
properties.put("mail.imaps.ssl.protocols", "TLSv1.2"); // 指定使用 TLSv1.2 协议

// 创建会话
Session session = Session.getInstance(properties);

// 以下代码省略

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论