簡體   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