繁体   English   中英

对于从 java 程序发送的 email,html 内容中的 png 图像未显示在 gmail 客户端上

[英]png images in html content are not showing up on gmail client for the email sent from java program

我正在尝试使用 sendgrid 将 html 内容从 java 程序发送到 gmail/outlook。

我在 html 中使用 base64 代码代替图像,outlook 中的 email 按预期正确显示,但在 gmail 上,由于 base 64 图像代码,它被剪裁了。

在我尝试 html 下面给出的 png 图像路径之后。现在在 gmail 上,内容显示但图像是空白的。 我该如何解决这个问题。

下面是 html 和代码。

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="em_full_wrap" bgcolor="#ffffff" style="table-layout:fixed;">
  <tr>
    <td align="center" valign="top"><table align="center" width="600" border="0" cellspacing="0" cellpadding="0" class="em_main_table" style="width:600px; table-layout:fixed;">
        <tr>
          <td align="center" valign="top" ><a href="https://www.testexample.com/" target="_blank" style="text-decoration: none;"><img src="emails/header_img.png" width="600" alt="TESTEXAMPLE &#62; &#174; | ComericA Bank &#174;" label="testexample_header" border="0" style="display: block; max-width: 600px; font-family: Arial, sans-serif; font-size: 14px; line-height: 16px; color: #000000;" class="em_full_img" /></a></td>
        </tr>
      </table></td>

我正在使用以下代码发送 email。

        
        EmailParams emailParams = new EmailParams();
        Content content = new Content();
        content.setType("text/html");   
        content.setValue("<htmlcontent as a string>");
        List<Content> contents = new ArrayList<Content>();
        contents.add(content);
        EmailObject from = new EmailObject();
        from.setEmail("no-reply@testexample.com");
        EmailObject to = new EmailObject();
        to.setEmail("test@gmail.com");
        List<EmailObject> tos = new ArrayList<EmailObject>();
        tos.add(to);

        Personalization personalization = new Personalization();
        personalization.setSubject("subject");
        personalization.setTo(tos);
        List<Personalization> personalizations = new ArrayList<Personalization>();
        personalizations.add(personalization);
         
        emailParams.setContent(contents);
        emailParams.setFrom(from);
        emailParams.setPersonalizations(personalizations);

        final String requestJson = objectMapper.writeValueAsString(emailParams);
        final HttpEntity<String> requestHttpEntity = new HttpEntity<String>(requestJson,
                getHttpHeaders(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON));
        final String endPointUri = new StringBuffer(<SENDGRID_URL>).toString();
        final ResponseEntity<String> messageResponse = restTemplate.exchange(endPointUri, HttpMethod.POST,
                requestHttpEntity, String.class);
    
        return "success";
      }
private HttpHeaders getHttpHeaders(MediaType contentType, MediaType accept) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAccept(Collections.singletonList(accept));
        httpHeaders.setContentType(contentType);
        httpHeaders.setBearerAuth("<bearer token value>");
        return httpHeaders;

    }```

Twilio SendGrid 开发人员在这里。

您应该将图像作为附件发送,并使用 CID(内容 ID)在 email 的内容中引用该图像。

您可以使用如下代码创建附件:

    Attachments attachments = new Attachments();
    attachments.setContent("BwdW"); // Insert actual content here
    attachments.setType("image/png");
    attachments.setFilename("banner.png");
    attachments.setDisposition("inline"); // This means it can appear inline
    attachments.setContentId("banner"); // This sets the CID
    emailParams.addAttachments(attachments);

然后,您可以在<img>标记中使用 CID 来引用图像,如下所示:

<img src="cid:banner" />

有关详细信息,请参阅有关在电子邮件中嵌入图像的文章和Java 中的厨房水槽 email 示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM