.setRaw(mailMessageModel.getRawString()) .setLabelIds(importedLabelIds.build()); try {
public static boolean sendMessage(String email, String subject, String text) { if (email.isEmpty()) { logger.info("Email is not sent because the address is empty"); return false; } try { Gmail gmail = new Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("XMage Server").build(); MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties())); mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email)); mimeMessage.setSubject(subject); mimeMessage.setText(text); ByteArrayOutputStream baos = new ByteArrayOutputStream(); mimeMessage.writeTo(baos); Message message = new Message(); message.setRaw(Base64.encodeBase64URLSafeString(baos.toByteArray())); gmail.users().messages().send(ConfigSettings.instance.getGoogleAccount() + (ConfigSettings.instance.getGoogleAccount().endsWith("@gmail.com") ? "" : "@gmail.com"), message).execute(); return true; } catch (MessagingException | IOException ex) { logger.error("Error sending message", ex); } return false; } }
public static Message createMessageWithEmail(MimeMessage emailContent) throws MessagingException, IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); emailContent.writeTo(buffer); byte[] bytes = buffer.toByteArray(); String encodedEmail = Base64.encodeBase64URLSafeString(bytes); Message message = new Message(); message.setRaw(encodedEmail); return message; }