Skip to content

Commit efcedf2

Browse files
committed
Added a thread id for the Gmail account replies which use aliases for sending.| #145.
1 parent f189039 commit efcedf2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/SendMessageSyncTask.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@
2828
import com.flowcrypt.email.security.SecurityStorageConnector;
2929
import com.google.api.client.util.Base64;
3030
import com.google.api.services.gmail.Gmail;
31+
import com.google.api.services.gmail.model.ListMessagesResponse;
3132
import com.sun.mail.imap.IMAPFolder;
3233

3334
import org.apache.commons.io.FileUtils;
3435
import org.apache.commons.io.IOUtils;
3536

3637
import java.io.BufferedInputStream;
38+
import java.io.ByteArrayInputStream;
3739
import java.io.ByteArrayOutputStream;
3840
import java.io.File;
3941
import java.io.IOException;
4042
import java.io.InputStream;
4143
import java.io.OutputStream;
4244
import java.nio.charset.StandardCharsets;
4345
import java.util.ArrayList;
46+
import java.util.List;
4447

4548
import javax.activation.DataHandler;
4649
import javax.activation.DataSource;
@@ -120,9 +123,18 @@ public void runSMTPAction(AccountDao accountDao, Session session, Store store, S
120123
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
121124
mimeMessage.writeTo(byteArrayOutputStream);
122125

126+
MimeMessage originalMimeMessage = new MimeMessage(session,
127+
new ByteArrayInputStream(outgoingMessageInfo.getRawReplyMessage().getBytes()));
128+
129+
String threadId = getGmailMessageThreadID(gmailApiService, originalMimeMessage.getMessageID());
123130
com.google.api.services.gmail.model.Message sentMessage
124131
= new com.google.api.services.gmail.model.Message();
125132
sentMessage.setRaw(Base64.encodeBase64URLSafeString(byteArrayOutputStream.toByteArray()));
133+
134+
if (!TextUtils.isEmpty(threadId)) {
135+
sentMessage.setThreadId(threadId);
136+
}
137+
126138
sentMessage = gmailApiService.users().messages().send("me", sentMessage).execute();
127139
isMessageSent = sentMessage.getId() != null;
128140
}
@@ -364,6 +376,26 @@ private String getAccountPublicKey(Context context, Js js, AccountDao accountDao
364376
throw new IllegalArgumentException("The sender doesn't have a public key");
365377
}
366378

379+
/**
380+
* Retrive a Gmail message thread id.
381+
*
382+
* @param service A {@link Gmail} reference.
383+
* @param rfc822msgidValue An rfc822 Message-Id value of the input message.
384+
* @return The input message thread id.
385+
* @throws IOException
386+
*/
387+
private String getGmailMessageThreadID(Gmail service, String rfc822msgidValue) throws IOException {
388+
ListMessagesResponse response = service.users().messages().list("me").setQ(
389+
"rfc822msgid:" + rfc822msgidValue).execute();
390+
391+
if (response.getMessages() != null && response.getMessages().size() == 1) {
392+
List<com.google.api.services.gmail.model.Message> messages = response.getMessages();
393+
return messages.get(0).getThreadId();
394+
}
395+
396+
return null;
397+
}
398+
367399
/**
368400
* The {@link DataSource} realization for a file which received from {@link Uri}
369401
*/

0 commit comments

Comments
 (0)