使用pop接收yandex郵件產(chǎn)生證書問題

今天遇到個(gè)玄學(xué)問題,用python3寫的pop接收yandex最新郵件可以正常使用,但是java用的jodd-email開源庫(kù)寫的卻報(bào)證書問題。

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安裝了證書,結(jié)果還是不行。更讓人感到不可思議的是,我電腦連的是手機(jī)無(wú)線網(wǎng),結(jié)果手機(jī)上面卻顯示了證書已安裝。

下面是部分python代碼:

# -*- coding:utf-8 -*-
# @Time: 2018/8/10 14:49
# send email with yandex,使用接收郵件時(shí)需要在yandex設(shè)置中開啟要接收的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用于縮進(jìn)顯示:
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轉(zhuǎn)為str的list
    resp, lines, octets = server.retr(index)
    # 獲得整個(gè)郵件的原始文本:
    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()返回所有郵件的編號(hào):
    resp, mails, octets = server.list()
    # print(mails)
    # latest_index = len(mails)
    latest_index = 1 # 經(jīng)測(cè)試,在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好使!

參考資料:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容