python自动下载邮件附件_python中的脚本以下载电子邮件附件

I have the following script :

import imaplib

import email

import os

svdir = 'c:/downloads'

mail = imaplib.IMAP4('https://outlook.office365.com/mapi/emsmdb/?'

'MailboxId=6ebd46ee-55df-452e-a0af-44e7b248d992@jmawireless.com')

mail.login("RNa", "RN!")

mail.select("ADP Files")

typ, msgs = mail.search(None, '(SUBJECT "ADP Files")')

msgs = msgs[0].split()

for emailid in msgs:

resp, data = mail.fetch(emailid, "(RFC822)")

email_body = data[0][1]

m = email.message_from_string(email_body)

if m.get_content_maintype() != 'multipart':

continue

for part in m.walk():

if part.get_content_maintype() == 'multipart':

continue

if part.get('Content-Disposition') is None:

continue

filename = part.get_filename()

if filename is not None:

sv_path = os.path.join(svdir, filename)

if not os.path.isfile(sv_path):

print(sv_path)

fp = open(sv_path, 'wb')

fp.write(part.get_payload(decode=True))

fp.close()

The error being produced is:

C:\Users\rnandipati\Desktop\Batch Files\EMAIL ADP>email.py

Traceback (most recent call last):

File "C:\Python34\lib\encodings\idna.py", line 165, in encode

raise UnicodeError("label empty or too long") UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File "C:\Users\rnandipati\Desktop\Batch Files\EMAIL ADP\email.py", line 2, in

import email

File "C:\Users\rnandipati\Desktop\Batch Files\EMAIL ADP\email.py", line 8, in

mail=imaplib.IMAP4( 'https://outlook.office365.com/mapi/emsmdb/?MailboxId=6ebd46ee-55df-452e-a0af-44e7b248d992@jmawireless.com')

File "C:\Python34\lib\imaplib.py", line 181, in __init__

self.open(host, port)

File "C:\Python34\lib\imaplib.py", line 257, in open

self.sock = self._create_socket()

File "C:\Python34\lib\imaplib.py", line 247, in _create_socket

return socket.create_connection((self.host, self.port))

File "C:\Python34\lib\socket.py", line 494, in create_connection

for res in getaddrinfo(host, port, 0, SOCK_STREAM):

File "C:\Python34\lib\socket.py", line 533, in getaddrinfo

for res in _socket.getaddrinfo(host, port, family, type, proto, flags): UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or to o long)

I do not know why is this error produced. I am fairly new to this kind of scripting.

解决方案

According to the documentation (and the source code too), the constructor imaplib.IMAP4() takes two parameters: host: the host name (usually a server name or IP address), defaulting to "localhost", and port: the port number, defaulting to the standard IMAP4 port).

Here, you are using an URL, which is wrong.

Try to open a connection like this:

with imaplib.IMAP4("outlook.office365.com") as imap4:

...

Consult this page to have the connection settings for Office 360: POP and IMAP settings for Outlook Office 365 for business

You may need SSL connection for Outlook Office 365. Here is a more useful usage example:

import imaplib

username = 'user@example.com'

password = 'password'

with imaplib.IMAP4_SSL('outlook.office365.com') as imap4:

imap4.login(username, password)

# retrieve a list of the mailboxes and select one

result, mailboxes = imap4.list()

imap4.select("inbox")

EDIT: alternative without with statement

try:

imap4.login(username, password)

# retrieve a list of the mailboxes and select one

result, mailboxes = imap4.list()

imap4.select("inbox")

finally:

imap4.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值