您好,我尝试使用mailjets api发送Excel文档。 我首先使用 mailjets 库将 excel 文件编码为 base64。 然后,我使用 mailjets 库将文件添加为附件并设置 mime 类型 application/vnd.ms-excel。 这是一个例子。
request = new MailjetRequest(Emailv31.resource)
.property(Emailv31.MESSAGES, new JSONArray()
.put(new JSONObject()
.put(Emailv31.Message.FROM, new JSONObject()
.put("Email", "[email protected]")
.put("Name", "Mailjet Pilot"))
.put(Emailv31.Message.TO, new JSONArray()
.put(new JSONObject()
.put("Email", "[email protected]")
.put("Name", "passenger 1")))
.put(Emailv31.Message.SUBJECT, "Your email flight plan!")
.put(Emailv31.Message.TEXTPART, "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!")
.put(Emailv31.Message.HTMLPART, "<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!")
.put(Emailv31.Message.ATTACHMENTS, new JSONArray()
.put(new JSONObject()
.put("ContentType", "application/vnd.ms-excel")
.put("Filename", "test.txt")
.put("Base64Content", "excelFileBase64")))));
response = client.post(request);
但是,我总是收到文件,因为它的字符串表示 base64 编码。
使用 ContentType:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
和测试.xlsx
除了 Drack 的答案之外,我还必须添加“Base64Content”。
所以,我的
Attachments
看起来像这样:
[{'ContentType': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Filename': excel_file_path
'Base64Content': base64_encoded}]
其中
base64_encoded
是使用以下语法创建的:
import base64
data = open(excel_file_path, 'rb').read()
base64_encoded = base64.b64encode(data).decode('UTF-8')