Although this seems very easy to fix, my .net application simply doesn't see this as a valid base64 string for some reason.
I'm using GMail API to fetch the messages and on the last part where I try to retrieve the body, I hit the wall with the following error message:
Exception thrown: 'System.FormatException' in mscorlib.dll
System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
at System.Convert.FromBase64_ComputeResultLength(Char* inputPtr, Int32 inputLength)
at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
at System.Convert.FromBase64String(String s)
at wForm.Form1.<Form1_Load>d__9.MoveNext() in `E:\Programs\Programming\wForms\wForm\wForm\Form1.cs:line 127`
and this is the encoded string (which decodes fine in other apps, such as the utilities-info website) : https://pastebin.ca/3893521
My code for loading the windows form and fetching those messages:
private async void Form1_Load(object sender, EventArgs e)
{
txtEmail_ID.Focus();
try
{
requestLabel = service.Users.Labels.List("me");
requestMessage = service.Users.Messages.List("me");
//requestMessage.LabelIds = "INBOX";
//requestMessage.IncludeSpamTrash = false;
// requestMessage.Q = "is:unread";
var messageResponse = await requestMessage.ExecuteAsync();
if (messageResponse != null && messageResponse.Messages != null)
{
foreach (var message in messageResponse.Messages)
{
var currentMessage = new messageInfo();
var requestNestedMessage = service.Users.Messages.Get("me", message.Id);
var nestedMessageResponse = await requestNestedMessage.ExecuteAsync();
if (nestedMessageResponse != null) // Check if we have nested params
{
foreach (var mParts in nestedMessageResponse.Payload.Headers) // Loop msg headers
{
if (mParts.Name == "Date")
{
currentMessage.msgDate = mParts.Value; // Get msg date
}
else if (mParts.Name == "From")
{
currentMessage.msgSender = mParts.Value; // Get msg sender
}
else if (mParts.Name == "Subject")
{
currentMessage.msgTitle = mParts.Value; // Get msg subject
}
}
if (nestedMessageResponse.Payload.Parts == null && nestedMessageResponse.Payload.Body != null)
{
currentMessage.msgBody = nestedMessageResponse.Payload.Body.Data;
}
else
{
currentMessage.msgBody = returnNestedParts(nestedMessageResponse.Payload.Parts, "");
}
Console.WriteLine(currentMessage.msgSender);
String codedString = currentMessage.msgBody.Replace("-", "+");
codedString = codedString.Replace("_", "/");
byte[] data = Convert.FromBase64String(codedString);
currentMessage.msgBody = Encoding.UTF8.GetString(data);
mInfo.Add(currentMessage);
}
}
}
} catch (Exception ex)
{
Console.WriteLine(ex);
}
//comboFolders.SelectedIndex = 0;
}
The program throws the exception on every message it fetches and the "Line 127" is on "byte[] data"
I have tried searching around in this forum for similar questions, however, none of their resolutions seem to have worked as most of them offered just to change the - and _ symbols to appropriate ones for base64url type.
I have resolved this, narrowed it down to the payload parts. The error appears when I want to join two payload parts of a message together, leaving trailing '=' signs un-handled and therefore forcing an exception on myself.