I’ve implemented some projects using the Gmail API service. I can read my emails and render some of them, but there is an issue. I sent a Gmail that contains only one image and read it in my app, but it does not render. What’s going on?
Here is the code snippet for reading Gmail. Please help me fix it.
string? body = "";
var _date = DateTime.Now;
var dateHeader = emailInfoResponse.Payload.Headers.FirstOrDefault(h => h.Name == "Date")?.Value;
if (DateTime.TryParse(dateHeader, out var emailDate))
{
_date = emailDate;
}
string _from = emailInfoResponse.Payload.Headers.Where(obj => obj.Name == "From").FirstOrDefault()?.Value ?? "";
string _to = emailInfoResponse.Payload.Headers.Where(obj => obj.Name == "To").FirstOrDefault()?.Value ?? "";
string? _subject = emailInfoResponse.Payload.Headers.Where(obj => obj.Name == "Subject").FirstOrDefault()?.Value;
string? _inReplyTo = emailInfoResponse.Payload.Headers.Where(obj => obj.Name == "In-Reply-To").FirstOrDefault()?.Value;
string? _threadId = emailInfoResponse.ThreadId;
if (_from != null)
{
if (emailInfoResponse.Payload.MimeType == "text/html")
{
body = emailInfoResponse.Payload.Body.Data;
}
else if (emailInfoResponse.Payload.MimeType.StartsWith("multipart"))
{
StringBuilder sb = new StringBuilder();
foreach (var part in emailInfoResponse.Payload.Parts)
{
if (part.MimeType == "text/plain" || part.MimeType == "text/html")
{
var body = part.Body.Data;
fullBody.Append(Encoding.UTF8.GetString(DecodeBase64(body)));
}
else if (emailInfoResponse.Payload.MimeType == "multipart/alternative" || emailInfoResponse.Payload.MimeType == "multipart/mixed" || emailInfoResponse.Payload.MimeType == "multipart/related")
As a result, I can’t read the Gmail body that consists of only one image file.
New contributor
Sharma Samuelson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4