使用pop接收yandex邮件产生证书问题

今天遇到个玄学问题,用python3写的pop接收yandex最新邮件可以正常使用,但是java用的jodd-email开源库写的却报证书问题。

Caused by: jodd.mail.MailException: Open session error; <--- sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at jodd.mail.MailSession.open(MailSession.java:86)
    at jodd.mail.ReceiveMailSession.open(ReceiveMailSession.java:42)
    at com.github.binarywang.demo.wx.mp.yandex.Ymail.<init>(Ymail.java:38)
    at com.github.binarywang.demo.wx.mp.yandex.Ymail.<clinit>(Ymail.java:18)

挂上梯子后,问题消失,但速度奇慢。我按照yandex官方教程(https://yandex.com/support/mail/mail-clients.html)给windows安装了证书,结果还是不行。更让人感到不可思议的是,我电脑连的是手机无线网,结果手机上面却显示了证书已安装。

下面是部分python代码:

# -*- coding:utf-8 -*-
# @Time: 2018/8/10 14:49
# send email with yandex,使用接收邮件时需要在yandex设置中开启要接收的folder
import smtplib
import poplib
from email.parser import Parser
import time

yandex_mail = "****"
yandex_pass = "****"
send_to_email = "****"


def send_emails(title, msg):
    server = smtplib.SMTP_SSL('smtp.yandex.com', 465)
    server.ehlo()
    server.login(yandex_mail, yandex_pass)
    message = 'Subject: {}\n\n{}'.format(title, msg)
    server.sendmail(yandex_mail, send_to_email, message)
    server.quit()
    print('E-mails successfully sent!')


# indent用于缩进显示:
def get_email_content(message):
    j = 0
    content = ''
    for part in message.walk():
        j = j + 1
        file_name = part.get_filename()
        contentType = part.get_content_type()
        if file_name:  # 保存附件
            pass
        elif contentType == 'text/plain' or contentType == 'text/html':
            data = part.get_payload(decode=True)
            content = data.decode('utf-8', 'ignore')
    # return str(content, 'utf-8')
    return content


def get_server(usr, pwd, url="pop.yandex.com", port=995):
    server = poplib.POP3_SSL(url, port)
    server.user(yandex_mail)
    server.pass_(yandex_pass)
    return server


def get_msg(server, index):
    resp, mails, octets = server.list()
    # 可以查看返回的列表类似['1 82923', '2 2184', ...]
    mails = [str(b, 'utf-8') for b in mails]  # 将bytes的list转为str的list
    resp, lines, octets = server.retr(index)
    # 获得整个邮件的原始文本:
    lines = [str(b, 'utf-8') for b in lines]
    msg_content = "\n".join(list(map(str, lines)))
    # 解析邮件:
    msg = Parser().parsestr(msg_content)
    return msg


def get_receive_time(msg):
    rtime = msg.get('X-Yandex-TimeMark')
    return int(rtime)


def get_from(msg):
    mfrom = msg.get('From')
    return mfrom


def receive_latest_email():
    server = get_server(yandex_mail, yandex_pass)
    # list()返回所有邮件的编号:
    resp, mails, octets = server.list()
    # print(mails)
    # latest_index = len(mails)
    latest_index = 1 # 经测试,在yandex中最新邮箱始终为1
    msg = get_msg(server, latest_index)
    receive_time = int(get_receive_time(msg))
    mfrom = get_from(msg)
    content = get_email_content(msg)
    print(content)
    server.quit()
    return receive_time, mfrom, content


# send_emails('Test Mail', 'Yes its a test mail!')
# print(receive_latest_email())
receive_latest_email()

下面是部分java代码:

package com.github.binarywang.demo.wx.mp.yandex;

import jodd.mail.*;

import java.util.Date;
import java.util.List;

/***
 * Time: 2018/8/12/15:20
 * Description: yandex email
 */
public class Ymail {
    private static final String USER = "****";
    private static final String HOST = "pop.yandex.com";
    private static final String PASSWD = "****";
    private static final int POP_PORT = 995;
    private ReceiveMailSession session = null;
    private ReceivedEmail latestEmail = null;
    private static Ymail ourInstance = new Ymail();

    public static Ymail getInstance() {
        return ourInstance;
    }

    private Ymail() {
        if (this.session == null) {
            Pop3Server server = MailServer.create()
                    .host(HOST)
                    .auth(USER, PASSWD)
                    .ssl(true)
                    .port(POP_PORT)
                    .buildPop3MailServer();

            this.session = server.createSession();
            session.open();
        }
    }

    public void closeSession() {
        if (this.session != null) {
            this.session.close();
        }
    }

    public String latestEmailContent() {
        if (latestEmail == null) {
            refresh();
        }
        List<EmailMessage> messages = this.latestEmail.messages();
        StringBuilder sb = new StringBuilder();
        for (EmailMessage msg : messages) {
            sb.append(msg.getContent());
        }
        return sb.toString();
    }

    public Date latestEmailDate() {
        if (latestEmail == null) {
            refresh();
        }
        return latestEmail.receivedDate();
    }

    public String latestEmailFrom() {
        if (latestEmail == null) {
            refresh();
        }
        return latestEmail.from().getEmail();
    }

    public void refresh() {
        ReceivedEmail[] emails = this.session.receiveEmailAndMarkSeen();
        this.latestEmail = emails[0];
    }
}

解决方法:

private Ymail() {
        if (this.session == null) {
            Pop3Server server = MailServer.create()
                    .host(HOST)
                    .auth(USER, PASSWD)
                    .ssl(true)
                    .port(POP_PORT)
                    .buildPop3MailServer();
            server.getSessionProperties().put("mail.pop3.ssl.trust", "*");
            this.session = server.createSession();
            session.open();
        }
    }

还是google好使!

参考资料:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容