用于从具有特定主题的电子邮件下载附件的脚本

时间:2017-05-10 17:05:38

标签: python outlook

以下代码从具有特定主题行(ADP文件)的Outlook电子邮件中下载附件。当我执行脚本时,它会生成一个错误FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\rnandipati\\desktop\\EMAIL ADP\\attachments\\1494075600130_1640807852.xls'。 可能是什么问题?

import imaplib
import email
import os

svdir = 'C:\\Users\\rnandipati\\desktop\\EMAIL ADP'

mail = imaplib.IMAP4_SSL('outlook.office365.com',993)
mail.login("rnai@jmas.com", "R7!")
mail.select("Inbox")

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_bytes(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()

1 个答案:

答案 0 :(得分:1)

不要使用var obj = JsonConvert.DeserializeObject<RootObject>(json); foreach (var offer in obj.Reserved.Values.SelectMany(v => v.Values)) { Console.WriteLine(offer.effectiveDate); foreach (var priceDim in offer.priceDimensions.Values) Console.WriteLine("\t" + priceDim.description); } // ..................... public class RootObject { public Dictionary<string, Dictionary<string, Offer>> Reserved { get; set; } } public class Offer { public string offerTermCode { get; set; } public string sku { get; set; } public DateTime effectiveDate { get; set; } public Dictionary<string, PriceDimension> priceDimensions { get; set; } public TermAttributes termAttributes { get; set; } } public class PriceDimension { public string rateCode { get; set; } public string description { get; set; } public string beginRange { get; set; } public string endRange { get; set; } public string unit { get; set; } public Dictionary<string, string> pricePerUnit { get; set; } public object[] appliesTo { get; set; } } public class TermAttributes { public string LeaseContractLength { get; set; } public string OfferingClass { get; set; } public string PurchaseOption { get; set; } } 条款,它应该可以完美运行:)