I'm having trouble getting more than just the snippet for text data for the message I am trying to retrieve using the Gmail API. Here is the piece of test code I am working with:
public string GetMail()
{
GmailService service = (GmailService)HttpContext.Current.Session["service"];
Message messageFeed = service.Users.Messages.List("me").Execute().Messages.First();
UsersResource.MessagesResource.GetRequest getReq = new UsersResource.MessagesResource.GetRequest(service, "me", messageFeed.Id);
getReq.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full;
Message message = getReq.Execute();
return message.Raw;
}
For some reason, when I call message.Raw, it is returning null. I am able to retrieve other properties as what the format=minimal setting would based off of the API playground example I was playing with.
However in my code, I am setting the format enum to "full", yet I am still unable to retrieve the full data of the message.
Am I completely missing something here?
Seems like you're mixing up formats and response types. If you want the raw message as a string in Message.raw then you need to set:
getReq.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;
If you want the parsed message back (in the "payload" field) then you can use getReq.Format of Full like you have.
Acceptable values are:
"full": Returns the parsed email message content in the payload field and the raw field is not used. (default)
"minimal": Only returns email message metadata such as identifiers and labels, it does not return the email headers, body, or payload.
"raw": Returns the entire email message content in the raw field as a string and the payload field is not used. This includes the identifiers, labels, metadata, MIME structure, and small body parts (typically less than 2KB).
from: https://developers.google.com/gmail/api/v1/reference/users/messages/get
Related
I have coded a bot to send a message response to a given command, !a. I want to store additional data on the message object, so that if the user reacts to that message the bot can read the hidden data property and know the original message resulted from !a.
Ideas I had:
Create a custom property on the message object: Message.Custom_Prop
Hijack an unused property: Maybe Message.webhookId?
Store hidden text in the form of an embed or message content.
I haven't been able to get any of these to work though.
The best way that I can think of is to store an array of message IDs that were triggered by !a. Something like this:
// At the start of your script
const commandResponses = [];
// Logic for sending the command response
message.channel.send("response here").then(msg => commandResponses.push(msg.id));
// Checking whether the message was a response
if (commandResponses.includes(reaction.message.id)) {
// It's a response so do stuff here
}
(Not tested so it's possible that this won't work)
I'm trying to get the message count of my Gmail Inbox from C#, but it turns out that the message counts are not returned for ANY of my labels.
The following returns my label names (both personal and system created) just fine, but the the respective message counts are always null. I've tried all of the message counts: MessagesUnread, MessagesTotal, ThreadsUnread, ThreadsTotal, and always get "No" below.
ListLabelsResponse response = service.Users.Labels.List("me").Execute();
foreach (Label label in response.Labels)
{
Console.Write("{0}, has messages? ", label.Name);
if (label.MessagesUnread.HasValue)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
What am I doing wrong?
Apparently the Google API is too lazy to fill in the count details on the List method. Instead I tried the Get...
Label rsp = service.Users.Labels.Get("me", "INBOX").Execute();
..and everything shows up nicely in the Label object. Problem solved.
I wanna write a small application that copy Gmail messages from one user folder to another. My approach is next:
Get raw message data to temporary placeholder;
Compose message using raw message data from the previous step;
Add message to required folder.
I can get raw message data as RFC 2822 formatted and base64url encoded string using this get request.
Also I can insert message to user mailbox with insert request. As documentation states, I can use one of the next two approaches:
use /upload URL (then I need an uploadType string and I should have a content-type of message/rfc822);
not using /upload URL and simply set message metadata in the request body -
something like the next JSON:
{
"labelIds": [
"INBOX",
"CATEGORY_PROMOTIONS"
],
"raw": "UmVjZWl2ZWQ6IGZyb20gMjkyODI0MTMyMDgyDQoJ..."
}
Unfortunately, when I don't use /upload URL (approach (2)) I can't insert message with attachment bigger than 5MB. But if I use /upload URL (approach (1)) I get response with error
"Media type 'message/rfc2822' is not supported. Valid media types: [message/rfc822]".
How can I support insertion of messages to Gmail without restriction on messages attachment size?
I have been trying to detect encoding of an email manually, to see whether the email that i have just fetched needed to be base64 decoded or not, but that wasn't a complete solution.
So now I'm trying to download the headers (fields) of an email first, check them what kind of email it is and then proceed to decoding it with base64 in case the header says that it is a base64 encoded email or just skip it if it is a plain text or HTML text.
The problem is that the libcurl commands for fetching these fields doesn't really work, most of the time the "Content-Type" returns just an empty string or says that it is a multipart/alternative but when i check the email manually it is just a plain text which obviously doesn't need to be decoded.??
"Content-Type" field multipart/alternative usually have different parts like text, html, base64 encoded text, attachments etc.
In cases of multipart/alternative the "Content-Transfer-Encoding" field doesn't return anything at all, and this is the most important header field for me to know what it contains.
My imaps request to Gmail account looks like this:
curl_easy_setopt(curl, CURLOPT_URL,"imaps://imap.gmail.com:993/INBOX;UID=33;SECTION=HEADER.FIELDS%%20(Content-Transfer-Encoding%%20Subject%%20From%%20Content-Type)");
Which returns this:
Subject: my subject
From: myname
Content-Type: multipart/alternative; boundary="001a114930a049c6da05dskdls9"
As you can see it doesn't returns the "Content-Transfer-Encoding" field.
This email actually contains the word "Yup" only, so it is a plain text and no attachments, when checked by clicking on the "show original" message in browser.
Sys info:
Linux, C , libcurl, gmail
I've created a custom transport for CakeEmail (to allow me to use Mandrill to send email). However, whenever I access the content of the message (which is cake email template driven), it doesn't encode the characters correctly (it changes 'é' to 'é', etc). If I use CakeEmail and bypass the transport, it displays the characters correctly in the email. I've narrowed this down to $email->message('html') in the transport code. If I output $email->message('html'), the characters are already incorrect.
App::uses('AbstractTransport', 'Network/Email');
App::uses('HttpSocket', 'Network/Http');
class MandrillTransport extends AbstractTransport {
public function send(CakeEmail $email) {
debug($email->message('html'));exit;
}
}
Thoughts?
You most probably have an encoding mismatch somewhere, if for example your App.encoding doesn't match your CakeEmail::$charset, CakeEmail would try to convert the content from App.encoding to CakeEmail::$charset.
https://github.com/cakephp/.../2.6.2/lib/Cake/Network/Email/CakeEmail.php#L1338
If for example the former were iso-8859-1, and the latter utf-8, just like the content, you would end up with the result you are showing here.
// outputs é when displayed as utf-8/unicode
echo mb_convert_encoding('é', 'utf-8', 'iso-8859-1');
You'll have to do some further debugging to trace down where exactly things are going wrong.