Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29923

Is there any way to send an email in the same thread?

$
0
0

I'm trying to send emails into one thread so it looks as similar as possible as sending via Gmail web UI.

According to the Gmail documentation, I wrote the next code on Kotlin that works correctly, but not completely

val DEFAULT_CHARSET: String = Charsets.UTF_8.name()

// get headers from original email
val messageId = originalMessage.payload.headers.find { it.name.equals("Message-ID", true) }?.value
val subject = originalMessage.payload.headers.find { it.name.equals("Subject", true) }?.value

// creating message
val message = MimeMessage(Session.getDefaultInstance(Properties()))

message.setFrom(InternetAddress(emailFrom, senderNameFrom, DEFAULT_CHARSET))
message.addRecipient(javax.mail.Message.RecipientType.TO, InternetAddress(emailTo))

message.setSubject(subject, DEFAULT_CHARSET)

// this headers are required accoring to [RFC 2822][1] standard
message.setHeader("In-Reply-To", messageId)
message.setHeader("References", messageId)
message.setHeader("Subject", "Re:$subject")

// creates message part
val messageBodyPart = MimeBodyPart()
messageBodyPart.setContent(content, "text/html; charset=UTF-8")

// creates multi-part
val multipart = MimeMultipart()
multipart.addBodyPart(messageBodyPart)


// sets the multi-part as e-mail's content
message.setContent(multipart)

val buffer = ByteArrayOutputStream()
emailMessage.writeTo(buffer)
val requestMessage = Message()
requestMessage.raw = Base64.encodeBase64URLSafeString(buffer.toByteArray())

// this thread ID you should collect from the original message
requestMessage.threadId = threadId

// from the result variable you can collect id, threadId, labels, etc.
val result = gmail.users().messages().send("me", requestMessage).execute()

As a result, emails send into one thread (inbox looks like this), but there are no three dots "show trimmed content" that hides the content of previous messages: What I have

Is it possible to make it looks like? What I need Other languages and libraries will also suit me.

P.S. I also tried sending emails as a Gmail draft, but this also doesn't work.

Test was done with java-libraries:

  • javax.mail:mail:1.5.0-b01
  • com.sun.mail:javax.mail:1.6.2

Viewing all articles
Browse latest Browse all 29923

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>