Skip to main content
 首页 » 编程设计

python之在 python 中创建 ActiveSync SendMail 请求

2025年05月04日15itcoder

我正在尝试在 python 中实现简单的 EAS 客户端功能并使用这个库:https://code.google.com/p/py-eas-client/

我可以成功配置、FolderSync、同步和获取来阅读邮件和下载附件。该库为这些命令内置了函数,但没有为 SendMail 内置函数。所以我尝试自己实现 SendMail:

def sendmail(self): 
    send_url = self.add_parameters(self.get_url(), 
                                   {"Cmd": "SendMail", "User": self.username, "DeviceId": self.device_id, 
                                    "DeviceType": self.device_type}) 
    d = self.agent.request( 
        'POST', 
        send_url, 
        Headers({'User-Agent': ['python-EAS-Client 1.0'], 
                 'Host': [self.server], 
                 'MS-ASProtocolVersion': [self.server_version], 
                 'X-MS-PolicyKey': [str(self.policy_key)], 
                 'Content-Type': ["application/vnd.ms-sync.wbxml"], 
                 'Authorization': [self.authorization_header()]}), 
        SendMailProducer()) 
    d.addCallback(self.wbxml_response) 
    return d 



然后这里是 SendMail wbxml 生产者:

class SendMailProducer(WBXMLProducer): 
    def __init__(self): 
        msg = """From: xyz@xyz.org 
To: xyz@xyz.org 
Subject: From NSync 
MIME-Version: 1.0 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350 
This is the body text.""" 
        wbdict = { 
        "SendMail": [ 
            ("ClientId", "12312312312352173263123"),                    
            ("Mime", msg) 
        ] 
        } 
 
        wb = convert_dict_to_wbxml(wbdict, default_page_num=3) 
        print wb 
        return WBXMLProducer.__init__(self, wb, verbose=True) 



在 dewbxml.py 中,我必须添加对 ComposeMail 命名空间的支持:

{ # Page 3 - ComposeMail 
            "name":"ComposeMail", 
                0x05: ('SendMail', None), 
                0x10: ('Mime', None), 
                0x11: ('ClientId', None) 
            } 



使用这个我得到一个 503 错误响应。我几乎可以肯定我没有正确准备 wbxml。假设我能成功获得一个Policy Key,有没有更简单的方法在python中执行activesync SendMail?我一直没能找到任何其他方式来编码 wbxml。感谢您的帮助。

请您参考如下方法:

搞清楚了,这是 wbxml 的问题。使用库 libwbxml 工作正常。但不幸的是python中没有包装器