79609053

Date: 2025-05-06 15:51:14
Score: 1
Natty:
Report link

Thank you to everyone who responded.

I received some input here: https://www.googlecloudcommunity.com/gc/Workspace-Developer/Gmail-API-HTML-message-with-UTF-8-character-set-extended/m-p/903889#M2940 which indeed solved the problem by adding additional calls to set the UTF-8 in additional places in the code.

Here are the updates that worked:

            Properties props = new Properties();
            props.put("mail.mime.charset", StandardCharsets.UTF_8.displayName());
            Session session = Session.getInstance(props);
            MimeMessage email = new MimeMessage(session);
            
            email.setFrom(new InternetAddress(appConfig.getThreadedApp()));
            email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toEmailAddress));

         ...

            email.setSubject(subject, StandardCharsets.UTF_8.displayName());
            email.setContent(htmlBodyText, ContentType.TEXT_HTML.getMimeType()+ "; charset="+StandardCharsets.UTF_8.displayName());
            
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            email.writeTo(buffer);
            byte[] rawMessageBytes = buffer.toByteArray();
            String encodedEmail = Base64.encodeBase64URLSafeString(rawMessageBytes);
            Message message = new Message();
            message.setRaw(encodedEmail);

         ...

            message = service.users().messages().send(myEmailAddress, message).execute();
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: jn4