How to reply back to an email message using gmail API in c# asp.net
so that it make trails of all messages. Got solutions for python
, php
, javascript
online but not for c#
. I can get message details but don't know how to reply.
My code for getting message:
public void getmessage()
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/gmail-dotnet-quickstart2.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scope,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var re = service.Users.Messages.List("me");
re.LabelIds = "INBOX";
re.Q = "from:emailaddress AND subject:subject";
var res = re.Execute();
if (res != null && res.Messages != null)
{
DataTable dt = new DataTable("msgs");
dt.Columns.Add("Date");
dt.Columns.Add("From");
dt.Columns.Add("Subject");
dt.Columns.Add("Body");
foreach (var email in res.Messages)
{
var emailInfoReq = service.Users.Messages.Get("me", email.Id);
var emailInfoResponse = emailInfoReq.Execute();
if (emailInfoResponse != null)
{
String from = "";
String date = "";
String subject = "";
String body = "";
//loop through the headers and get the fields we need...
foreach (var mParts in emailInfoResponse.Payload.Headers)
{
if (mParts.Name == "Date")
{
date = mParts.Value;
}
else if (mParts.Name == "From")
{
from = mParts.Value;
}
else if (mParts.Name == "Subject")
{
subject = mParts.Value;
}
//else if (mParts.Name == "Message-ID")
//{
// var abc = mParts.Value;
//}
if (date != "" && from != "")
{
if (emailInfoResponse.Payload.Parts == null && emailInfoResponse.Payload.Body != null)
body = DecodeBase64String(emailInfoResponse.Payload.Body.Data);
else
body = GetNestedBodyParts(emailInfoResponse.Payload.Parts, "");
}
}
DataRow row = dt.NewRow();
row[0] = date;
row[1] = from;
row[2] = subject;
row[3] = body;
dt.Rows.Add(row);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Getting message-ID as well but no References and In-Rely-To for first email. Can send simple email message using MimeKit but have no idea about reply.
Any help would be appreciated. Thank you in advance!!