1

My goal is to retrieve all emails that exist under one label within my gmail account and forward those emails to another email address. Here's what i have so far:

string[] Scopes = { GmailService.Scope.GmailReadonly, GmailService.Scope.GmailSend, };
string ApplicationName = "APP NAME";
UserCredential credential;
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
    string credPath = "token.json";
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(   
           GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "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 labelList = service.Users.Messages.List("me@gmail.com");
labelList.LabelIds = "LABEL_NAME";
var emailListResponse = labelList.Execute();
if (emailListResponse != null && emailListResponse.Messages != null)
{
    foreach (var email in emailListResponse.Messages)
    {
       //THIS IS WHERE I WOULD LIKE TO FORWARD this email TO ANOTHER EMAIL ADDRESS?
       //How to set TO?
       //service.Users.Messages.Send(email, "me@gmail.com").Execute();
    }
}

I am able to retrieve the emails but i'm not sure how to forward. Thank you for your help

user2370664
  • 381
  • 5
  • 8
  • 30
  • 2
    Have a look at [this answer](https://stackoverflow.com/a/32803366) – Matt.G Nov 12 '19 at 20:30
  • i definitely thought there would be an easier way just to forward the exiting message but it doesn't look that way. Thanks for this link – user2370664 Nov 14 '19 at 15:29

1 Answers1

0

To forward a message you have to get it and add the recipient's email to the "To:" field.

Alternatively, you could set up a filter to forward all emails with the Label you have to another address.

ZektorH
  • 2,680
  • 1
  • 7
  • 20