错误连接 IMAP 服务器 使用 python imaplib 和 mailcow

huangapple go评论73阅读模式
英文:

Error connecting IMAP server with python imaplib and mailcow

问题

我刚刚在一个刚刚安装的Ubuntu 22.04 VPS上安装了mailcow邮件服务器。可以通过Evolution成功连接,但是尝试使用Python脚本使用imaplib连接时出现问题。客户端是Ubuntu 22.10 Mate,Python版本是3.10。

import imaplib
imap = imaplib.IMAP4_SSL("mail.mysite.com", 143)
imap.login('me@mysite.com', 'a_strong_password')

出现错误:

Traceback (most recent call last):
  File "/home/cc1/python/mail/imap_test.py", line 12, in <module>
    imap = imaplib.IMAP4_SSL("mail.mysite.com", 143)
  File "/usr/lib/python3.10/imaplib.py", line 1323, in __init__
    IMAP4.__init__(self, host, port, timeout)
  ...
  ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)

我尝试过使用ssl context的不同选项,但都没有成功。这是我的邮件客户端的配置,可以正常工作:mail client。我猜这可能与服务器端配置有关,但我对mailcow和dovecot都不太熟悉。

英文:

I just install a mailcow mail server on a ubuntu 22.04(fresh install) vps, i can connect well with my mail client (Evolution), but have issue when trying to connect from a python script using imaplib:

my client side is Ubuntu 22.10 Mate with Python 3.10

import imaplib 
imap = imaplib.IMAP4_SSL(&quot;mail.mysite.com&quot;, 143) 
imap.login(&#39;me@mysite.com&#39;,&#39;a_strong_password&#39;)

getting this error:

Traceback (most recent call last):   
File &quot;/home/cc1/python/mail/imap_test.py&quot;, line 12, in &lt;module&gt;
     imap = imaplib.IMAP4_SSL(&quot;mail.mysite.com&quot;, 143)   
File &quot;/usr/lib/python3.10/imaplib.py&quot;, line 1323, in __init__
     IMAP4.__init__(self, host, port, timeout)   
File &quot;/usr/lib/python3.10/imaplib.py&quot;, line 202, in __init__
     self.open(host, port, timeout)   
File &quot;/usr/lib/python3.10/imaplib.py&quot;, line 1336, in open
     IMAP4.open(self, host, port, timeout)   
File &quot;/usr/lib/python3.10/imaplib.py&quot;, line 312, in open
     self.sock = self._create_socket(timeout)   
File &quot;/usr/lib/python3.10/imaplib.py&quot;, line 1327, in _create_socket
     return self.ssl_context.wrap_socket(sock,   
File &quot;/usr/lib/python3.10/ssl.py&quot;, line 513, in wrap_socket
     return self.sslsocket_class._create(   
File &quot;/usr/lib/python3.10/ssl.py&quot;, line 1071, in _create
     self.do_handshake()   
File &quot;/usr/lib/python3.10/ssl.py&quot;, line 1342, in do_handshake
     self._sslobj.do_handshake()
 ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)

I already try using ssl context with both options:

# PROTOCOL_SSLv23
# PROTOCOL_TLS
# PROTOCOL_TLS_CLIENT
# PROTOCOL_TLSv1
# PROTOCOL_TLSv1_1
# PROTOCOL_TLSv1_2

here the config of my mail client working well:
mail client

i guess its about some server side config, but i'm new to mailcow and dovecot

答案1

得分: 1

只需找出我的问题:


imap = imaplib.IMAP4_SSL(&quot;mail.mysite.com&quot;, 143)
替换为
imap = imaplib.IMAP4_SSL(&quot;mail.mysite.com&quot;)

然后它就可以正常工作。

英文:

Just find out my problem:

replacing
imap = imaplib.IMAP4_SSL(&quot;mail.mysite.com&quot;, 143)
by
imap = imaplib.IMAP4_SSL(&quot;mail.mysite.com&quot;)

and then it works well

huangapple
  • 本文由 发表于 2023年4月17日 13:38:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定