Gmail API : send text and HTML in one mail

标签 gmail gmail-api

发送文本或 HTML 的邮件工作得很好,但是当我在一封邮件中两者发送它们时,HTML 以名为“noname.html”的附件形式出现,其中包含 HTML。

我已阅读有关此主题的其他相关问题,但找不到可能存在的问题。

MIME-Version: 1.0
From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3043555e54554270575d51595c1e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>
To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="24564147414d524156644349454d480a474b49" rel="noreferrer noopener nofollow">[email protected]</a>
Subject: test
Content-type: multipart/mixed; boundary="012boundary"

--012boundary
Content-type: text/plain; charset="UTF-8"

Hello plain text!

--012boundary
Content-type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<b>Hello html</b>
--012boundary--`

最佳答案

您想要使用 gmail API 发送 HTML 正文和文本正文。如果我的理解是正确的,那么这个修改怎么样?

来自:

Content-type: multipart/mixed; boundary="012boundary"

致:

Content-type: multipart/alternative; boundary="012boundary"

注意:

  • 使用multipart/alternative内容类型的两个部分:text/plain; charset="UTF-8"Content-type: text/html; charset="UTF-8" 可以发送。

引用:

在我的环境中,我可以确认您修改为 multipart/alternative 的请求正文有效。如果这不适用于您的环境,我很抱歉。

编辑:

为了将文本正文、HTML 正文和 HTML 附件文件作为一封电子邮件发送,可以按如下方式创建请求正文的结构。

  • 多部分/混合
    • 多部分/替代
      • 文本/纯文本
      • 文本/html
    • text/html(附件文件)

示例请求正文:

MIME-Version: 1.0
From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8bbada6acadba88afa5a9a1a4e6aba7a5" rel="noreferrer noopener nofollow">[email protected]</a>
To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63110600060a15061123040e020a0f4d000c0e" rel="noreferrer noopener nofollow">[email protected]</a>
Subject: test
Content-Type: multipart/mixed; boundary=012boundary01

--012boundary01
Content-Type: multipart/alternative; boundary=012boundary02

--012boundary02
Content-type: text/plain; charset=UTF-8

Hello plain text!

--012boundary02
Content-type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<b>Hello html</b>

--012boundary02--
--012boundary01
Content-type: text/html; charset=UTF-8
Content-Disposition: attachment; filename="sample.html"
Content-Transfer-Encoding: quoted-printable

<b>HTML sample attachment file</b>
--012boundary01--

引用文献:

关于Gmail API : send text and HTML in one mail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540051/

相关文章:

gmail - 当 Gmail 以编程方式用作 POP3 服务器时,它表现出哪些非标准行为特征?

ios - ios swift 中的并发任务执行

android - 如果我有 id 或 threadId,我可以在 Gmail 应用程序中打开我的电子邮件线程吗?

python - 配置 Flask-Mail 以使用 GMail

google-apps-script - 无法通过 Gmail 插件访问 Gmail 草稿

c# - 从电子邮件正文中删除换行符

html - GMail 中 <td> 的宽度无效

java - 在 Java 中使用 JavaMail Api 一次又一次地从 gmail 中读取邮件

c# - 如何为日期时间使用 GMAIL API 查询过滤器

javascript - 在用户的 GMail 收件箱中获取消息