在Python中创建ActiveSync SendMail请求

2 投票
1 回答
1044 浏览
提问于 2025-04-18 18:06

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

我可以成功地进行设备配置、文件夹同步、数据同步,以及读取消息和下载附件。这个库里有这些命令的内置功能,但没有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。假设我可以成功获取一个策略密钥,有没有更简单的方法在Python中执行activesync的SendMail?我找不到其他编码wbxml的方法。感谢任何帮助。

1 个回答

1

我搞明白了,问题出在wbxml上。使用libwbxml这个库就没问题了。不过可惜的是,Python里没有这个库的封装。

撰写回答