How to reply to a message using Gmail API in java?
I am trying to reply to a message using the gmail api, and I am confused in setting the In-Reply-To and references header, I am not sure what to set the value to and how to set the value. Please find my code below:
public static Message createMessageWithEmail(MimeMessage emailContent) throws Exception {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
emailContent.addRecipient(javax.mail.Message.RecipientType.TO, emailContent.getFrom()[0]);
emailContent.setReplyTo(emailContent.getFrom());
emailContent.setHeader("In-Reply-To", emailContent.getMessageID());
emailContent.setHeader("References",emailContent.getMessageID());
emailContent.setText("hi thank you");
emailContent.writeTo(buffer);
byte bytes = buffer.toByteArray();
String encodedEmail = Base64.encodeBase64URLSafeString(bytes);
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
private static Message replyMessage(Gmail service, String userId, MimeMessage emailContent,Message messages) throws Exception {
Message message = createMessageWithEmail(emailContent);
message.setThreadId(messages.getThreadId());
message.setId(messages.getId());
message = service.users().messages().send(userId, message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
return message;
}
java gmail gmail-api
add a comment |
I am trying to reply to a message using the gmail api, and I am confused in setting the In-Reply-To and references header, I am not sure what to set the value to and how to set the value. Please find my code below:
public static Message createMessageWithEmail(MimeMessage emailContent) throws Exception {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
emailContent.addRecipient(javax.mail.Message.RecipientType.TO, emailContent.getFrom()[0]);
emailContent.setReplyTo(emailContent.getFrom());
emailContent.setHeader("In-Reply-To", emailContent.getMessageID());
emailContent.setHeader("References",emailContent.getMessageID());
emailContent.setText("hi thank you");
emailContent.writeTo(buffer);
byte bytes = buffer.toByteArray();
String encodedEmail = Base64.encodeBase64URLSafeString(bytes);
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
private static Message replyMessage(Gmail service, String userId, MimeMessage emailContent,Message messages) throws Exception {
Message message = createMessageWithEmail(emailContent);
message.setThreadId(messages.getThreadId());
message.setId(messages.getId());
message = service.users().messages().send(userId, message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
return message;
}
java gmail gmail-api
add a comment |
I am trying to reply to a message using the gmail api, and I am confused in setting the In-Reply-To and references header, I am not sure what to set the value to and how to set the value. Please find my code below:
public static Message createMessageWithEmail(MimeMessage emailContent) throws Exception {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
emailContent.addRecipient(javax.mail.Message.RecipientType.TO, emailContent.getFrom()[0]);
emailContent.setReplyTo(emailContent.getFrom());
emailContent.setHeader("In-Reply-To", emailContent.getMessageID());
emailContent.setHeader("References",emailContent.getMessageID());
emailContent.setText("hi thank you");
emailContent.writeTo(buffer);
byte bytes = buffer.toByteArray();
String encodedEmail = Base64.encodeBase64URLSafeString(bytes);
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
private static Message replyMessage(Gmail service, String userId, MimeMessage emailContent,Message messages) throws Exception {
Message message = createMessageWithEmail(emailContent);
message.setThreadId(messages.getThreadId());
message.setId(messages.getId());
message = service.users().messages().send(userId, message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
return message;
}
java gmail gmail-api
I am trying to reply to a message using the gmail api, and I am confused in setting the In-Reply-To and references header, I am not sure what to set the value to and how to set the value. Please find my code below:
public static Message createMessageWithEmail(MimeMessage emailContent) throws Exception {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
emailContent.addRecipient(javax.mail.Message.RecipientType.TO, emailContent.getFrom()[0]);
emailContent.setReplyTo(emailContent.getFrom());
emailContent.setHeader("In-Reply-To", emailContent.getMessageID());
emailContent.setHeader("References",emailContent.getMessageID());
emailContent.setText("hi thank you");
emailContent.writeTo(buffer);
byte bytes = buffer.toByteArray();
String encodedEmail = Base64.encodeBase64URLSafeString(bytes);
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
private static Message replyMessage(Gmail service, String userId, MimeMessage emailContent,Message messages) throws Exception {
Message message = createMessageWithEmail(emailContent);
message.setThreadId(messages.getThreadId());
message.setId(messages.getId());
message = service.users().messages().send(userId, message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
return message;
}
java gmail gmail-api
java gmail gmail-api
asked Nov 14 '18 at 22:33
user2987322user2987322
327
327
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Both of these headers come from RFC2822.
The parts relevant to your question are included on pages 23-24 (emphasis and formatting mine):
When creating a reply to a message, the "In-Reply-To:" and
"References:" fields of the resultant message are constructed as
follows:
The "In-Reply-To:" field will contain the contents of the "Message-ID:" field of the message to which this one is a reply (the "parent message"). If there is more than one parent message, then the "In-Reply-To:" field will contain the contents of all of the parents' "Message-ID:" fields. If there is no "Message-ID:" field in any of the parent messages, then the new message will have no "In-Reply-To:" field.
The "References:" field will contain the contents of the parent's "References:" field (if any) followed by the contents of the parent's "Message-ID:" field (if any). If the parent message does not contain a "References:" field but does have an "In-Reply-To:" field > containing a single message identifier, then the "References:" field will contain the contents of the parent's "In-Reply-To:" field followed by the contents of the parent's "Message-ID:" field (if any). If the parent has none of the "References:", "In-Reply-To:", or "Message-ID:" fields, then the new message will have no "References:" field.
What is meant by contain the contents of the "Message-ID:" does that mean the message id in itself if that is the case i think I am adding it here emailContent.setHeader("In-Reply-To", emailContent.getMessageID()); emailContent.setHeader("References",emailContent.getMessageID());
– user2987322
Nov 14 '18 at 23:17
@user2987322 yes, you are setting this values correctly. The points of these headers is to link the messages, for example in the same thread, although GMail also usesSubject
field for that purpose.
– syntagma
Nov 14 '18 at 23:22
i am not sure if i understand that. sorry!
– user2987322
Nov 14 '18 at 23:42
@user2987322 let's you have message A with id 42, then someone replies to that message with message B. That message B should have 42 set in both of the headers you are asking about, in order for mail clients to be able to correctly link the messages (i.e. understand that both come from the same thread).
– syntagma
Nov 14 '18 at 23:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309746%2fhow-to-reply-to-a-message-using-gmail-api-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Both of these headers come from RFC2822.
The parts relevant to your question are included on pages 23-24 (emphasis and formatting mine):
When creating a reply to a message, the "In-Reply-To:" and
"References:" fields of the resultant message are constructed as
follows:
The "In-Reply-To:" field will contain the contents of the "Message-ID:" field of the message to which this one is a reply (the "parent message"). If there is more than one parent message, then the "In-Reply-To:" field will contain the contents of all of the parents' "Message-ID:" fields. If there is no "Message-ID:" field in any of the parent messages, then the new message will have no "In-Reply-To:" field.
The "References:" field will contain the contents of the parent's "References:" field (if any) followed by the contents of the parent's "Message-ID:" field (if any). If the parent message does not contain a "References:" field but does have an "In-Reply-To:" field > containing a single message identifier, then the "References:" field will contain the contents of the parent's "In-Reply-To:" field followed by the contents of the parent's "Message-ID:" field (if any). If the parent has none of the "References:", "In-Reply-To:", or "Message-ID:" fields, then the new message will have no "References:" field.
What is meant by contain the contents of the "Message-ID:" does that mean the message id in itself if that is the case i think I am adding it here emailContent.setHeader("In-Reply-To", emailContent.getMessageID()); emailContent.setHeader("References",emailContent.getMessageID());
– user2987322
Nov 14 '18 at 23:17
@user2987322 yes, you are setting this values correctly. The points of these headers is to link the messages, for example in the same thread, although GMail also usesSubject
field for that purpose.
– syntagma
Nov 14 '18 at 23:22
i am not sure if i understand that. sorry!
– user2987322
Nov 14 '18 at 23:42
@user2987322 let's you have message A with id 42, then someone replies to that message with message B. That message B should have 42 set in both of the headers you are asking about, in order for mail clients to be able to correctly link the messages (i.e. understand that both come from the same thread).
– syntagma
Nov 14 '18 at 23:44
add a comment |
Both of these headers come from RFC2822.
The parts relevant to your question are included on pages 23-24 (emphasis and formatting mine):
When creating a reply to a message, the "In-Reply-To:" and
"References:" fields of the resultant message are constructed as
follows:
The "In-Reply-To:" field will contain the contents of the "Message-ID:" field of the message to which this one is a reply (the "parent message"). If there is more than one parent message, then the "In-Reply-To:" field will contain the contents of all of the parents' "Message-ID:" fields. If there is no "Message-ID:" field in any of the parent messages, then the new message will have no "In-Reply-To:" field.
The "References:" field will contain the contents of the parent's "References:" field (if any) followed by the contents of the parent's "Message-ID:" field (if any). If the parent message does not contain a "References:" field but does have an "In-Reply-To:" field > containing a single message identifier, then the "References:" field will contain the contents of the parent's "In-Reply-To:" field followed by the contents of the parent's "Message-ID:" field (if any). If the parent has none of the "References:", "In-Reply-To:", or "Message-ID:" fields, then the new message will have no "References:" field.
What is meant by contain the contents of the "Message-ID:" does that mean the message id in itself if that is the case i think I am adding it here emailContent.setHeader("In-Reply-To", emailContent.getMessageID()); emailContent.setHeader("References",emailContent.getMessageID());
– user2987322
Nov 14 '18 at 23:17
@user2987322 yes, you are setting this values correctly. The points of these headers is to link the messages, for example in the same thread, although GMail also usesSubject
field for that purpose.
– syntagma
Nov 14 '18 at 23:22
i am not sure if i understand that. sorry!
– user2987322
Nov 14 '18 at 23:42
@user2987322 let's you have message A with id 42, then someone replies to that message with message B. That message B should have 42 set in both of the headers you are asking about, in order for mail clients to be able to correctly link the messages (i.e. understand that both come from the same thread).
– syntagma
Nov 14 '18 at 23:44
add a comment |
Both of these headers come from RFC2822.
The parts relevant to your question are included on pages 23-24 (emphasis and formatting mine):
When creating a reply to a message, the "In-Reply-To:" and
"References:" fields of the resultant message are constructed as
follows:
The "In-Reply-To:" field will contain the contents of the "Message-ID:" field of the message to which this one is a reply (the "parent message"). If there is more than one parent message, then the "In-Reply-To:" field will contain the contents of all of the parents' "Message-ID:" fields. If there is no "Message-ID:" field in any of the parent messages, then the new message will have no "In-Reply-To:" field.
The "References:" field will contain the contents of the parent's "References:" field (if any) followed by the contents of the parent's "Message-ID:" field (if any). If the parent message does not contain a "References:" field but does have an "In-Reply-To:" field > containing a single message identifier, then the "References:" field will contain the contents of the parent's "In-Reply-To:" field followed by the contents of the parent's "Message-ID:" field (if any). If the parent has none of the "References:", "In-Reply-To:", or "Message-ID:" fields, then the new message will have no "References:" field.
Both of these headers come from RFC2822.
The parts relevant to your question are included on pages 23-24 (emphasis and formatting mine):
When creating a reply to a message, the "In-Reply-To:" and
"References:" fields of the resultant message are constructed as
follows:
The "In-Reply-To:" field will contain the contents of the "Message-ID:" field of the message to which this one is a reply (the "parent message"). If there is more than one parent message, then the "In-Reply-To:" field will contain the contents of all of the parents' "Message-ID:" fields. If there is no "Message-ID:" field in any of the parent messages, then the new message will have no "In-Reply-To:" field.
The "References:" field will contain the contents of the parent's "References:" field (if any) followed by the contents of the parent's "Message-ID:" field (if any). If the parent message does not contain a "References:" field but does have an "In-Reply-To:" field > containing a single message identifier, then the "References:" field will contain the contents of the parent's "In-Reply-To:" field followed by the contents of the parent's "Message-ID:" field (if any). If the parent has none of the "References:", "In-Reply-To:", or "Message-ID:" fields, then the new message will have no "References:" field.
answered Nov 14 '18 at 22:58
syntagmasyntagma
12.9k1249106
12.9k1249106
What is meant by contain the contents of the "Message-ID:" does that mean the message id in itself if that is the case i think I am adding it here emailContent.setHeader("In-Reply-To", emailContent.getMessageID()); emailContent.setHeader("References",emailContent.getMessageID());
– user2987322
Nov 14 '18 at 23:17
@user2987322 yes, you are setting this values correctly. The points of these headers is to link the messages, for example in the same thread, although GMail also usesSubject
field for that purpose.
– syntagma
Nov 14 '18 at 23:22
i am not sure if i understand that. sorry!
– user2987322
Nov 14 '18 at 23:42
@user2987322 let's you have message A with id 42, then someone replies to that message with message B. That message B should have 42 set in both of the headers you are asking about, in order for mail clients to be able to correctly link the messages (i.e. understand that both come from the same thread).
– syntagma
Nov 14 '18 at 23:44
add a comment |
What is meant by contain the contents of the "Message-ID:" does that mean the message id in itself if that is the case i think I am adding it here emailContent.setHeader("In-Reply-To", emailContent.getMessageID()); emailContent.setHeader("References",emailContent.getMessageID());
– user2987322
Nov 14 '18 at 23:17
@user2987322 yes, you are setting this values correctly. The points of these headers is to link the messages, for example in the same thread, although GMail also usesSubject
field for that purpose.
– syntagma
Nov 14 '18 at 23:22
i am not sure if i understand that. sorry!
– user2987322
Nov 14 '18 at 23:42
@user2987322 let's you have message A with id 42, then someone replies to that message with message B. That message B should have 42 set in both of the headers you are asking about, in order for mail clients to be able to correctly link the messages (i.e. understand that both come from the same thread).
– syntagma
Nov 14 '18 at 23:44
What is meant by contain the contents of the "Message-ID:" does that mean the message id in itself if that is the case i think I am adding it here emailContent.setHeader("In-Reply-To", emailContent.getMessageID()); emailContent.setHeader("References",emailContent.getMessageID());
– user2987322
Nov 14 '18 at 23:17
What is meant by contain the contents of the "Message-ID:" does that mean the message id in itself if that is the case i think I am adding it here emailContent.setHeader("In-Reply-To", emailContent.getMessageID()); emailContent.setHeader("References",emailContent.getMessageID());
– user2987322
Nov 14 '18 at 23:17
@user2987322 yes, you are setting this values correctly. The points of these headers is to link the messages, for example in the same thread, although GMail also uses
Subject
field for that purpose.– syntagma
Nov 14 '18 at 23:22
@user2987322 yes, you are setting this values correctly. The points of these headers is to link the messages, for example in the same thread, although GMail also uses
Subject
field for that purpose.– syntagma
Nov 14 '18 at 23:22
i am not sure if i understand that. sorry!
– user2987322
Nov 14 '18 at 23:42
i am not sure if i understand that. sorry!
– user2987322
Nov 14 '18 at 23:42
@user2987322 let's you have message A with id 42, then someone replies to that message with message B. That message B should have 42 set in both of the headers you are asking about, in order for mail clients to be able to correctly link the messages (i.e. understand that both come from the same thread).
– syntagma
Nov 14 '18 at 23:44
@user2987322 let's you have message A with id 42, then someone replies to that message with message B. That message B should have 42 set in both of the headers you are asking about, in order for mail clients to be able to correctly link the messages (i.e. understand that both come from the same thread).
– syntagma
Nov 14 '18 at 23:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309746%2fhow-to-reply-to-a-message-using-gmail-api-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown