2
我需要下載傳入的附件,而無需使用Python腳本通過郵件進行附件。如何從Python腳本下載Outlook附件?
例如:如果任何人此時發送郵件(現在),那麼只將該附件下載到本地驅動器中,而不是過去的附件。
請任何人都幫我使用python腳本或java下載附件。
我需要下載傳入的附件,而無需使用Python腳本通過郵件進行附件。如何從Python腳本下載Outlook附件?
例如:如果任何人此時發送郵件(現在),那麼只將該附件下載到本地驅動器中,而不是過去的附件。
請任何人都幫我使用python腳本或java下載附件。
import email
import imaplib
import os
class FetchEmail():
connection = None
error = None
mail_server="host_name"
username="outlook_username"
password="password"
self.save_attachment(self,msg,download_folder)
def __init__(self, mail_server, username, password):
self.connection = imaplib.IMAP4_SSL(mail_server)
self.connection.login(username, password)
self.connection.select(readonly=False) # so we can mark mails as read
def close_connection(self):
"""
Close the connection to the IMAP server
"""
self.connection.close()
def save_attachment(self, msg, download_folder="/tmp"):
"""
Given a message, save its attachments to the specified
download folder (default is /tmp)
return: file path to attachment
"""
att_path = "No attachment found."
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
att_path = os.path.join(download_folder, filename)
if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
return att_path
def fetch_unread_messages(self):
"""
Retrieve unread messages
"""
emails = []
(result, messages) = self.connection.search(None, 'UnSeen')
if result == "OK":
for message in messages[0].split(' '):
try:
ret, data = self.connection.fetch(message,'(RFC822)')
except:
print "No new emails to read."
self.close_connection()
exit()
msg = email.message_from_string(data[0][1])
if isinstance(msg, str) == False:
emails.append(msg)
response, data = self.connection.store(message, '+FLAGS','\\Seen')
return emails
self.error = "Failed to retreive emails."
return emails
上面的代碼適用於我下載attachment.H這對任何一個真正有幫助。
import win32com.client #pip install pypiwin32 to work with windows operating sysytm
import datetime
import os
# To get today's date in 'day-month-year' format(01-12-2017).
dateToday=datetime.datetime.today()
FormatedDate=('{:02d}'.format(dateToday.day)+'-'+'{:02d}'.format(dateToday.month)+'-'+'{:04d}'.format(dateToday.year))
# Creating an object for the outlook application.
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
# Creating an object to access Inbox of the outlook.
inbox=outlook.GetDefaultFolder(6)
# Creating an object to access items inside the inbox of outlook.
messages=inbox.Items
def save_attachments(subject,which_item,file_name):
# To iterate through inbox emails using inbox.Items object.
for message in messages:
if (message.Subject == subject):
body_content = message.body
# Creating an object for the message.Attachments.
attachment = message.Attachments
# To check which item is selected among the attacments.
print (message.Attachments.Item(which_item))
# To iterate through email items using message.Attachments object.
for attachment in message.Attachments:
# To save the perticular attachment at the desired location in your hard disk.
attachment.SaveAsFile(os.path.join("D:\Script\Monitoring",file_name))
break
看上去需要一個爬蟲,嘗試硒 – kiviak
我已經有Python代碼從gmail.But下載未讀附件我需要下載從Outlook郵件附件。 –