Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Gmail

Public general use code classes and xml files that we've compiled and used over the years:

Gmail API support class

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Configuration;
   4:  using System.IO;
   5:  using System.Text;
   6:  using System.Threading;
   7:  using System.Threading.Tasks;
   8:  using Google.Apis.Auth.OAuth2;
   9:  using Google.Apis.Gmail.v1;
  10:  using Google.Apis.Gmail.v1.Data;
  11:  using Google.Apis.Services;
  12:  using Google.Apis.Util.Store;
  13:   
  14:  namespace Ia.Cl.Model
  15:  {
  16:      ////////////////////////////////////////////////////////////////////////////
  17:   
  18:      /// <summary publish="true">
  19:      /// Gmail API support class
  20:      /// </summary>
  21:      /// 
  22:      /// </summary>
  23:      /// <remarks> 
  24:      /// Copyright © 2017-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  25:      ///
  26:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  27:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  28:      ///
  29:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  30:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  31:      /// 
  32:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  33:      /// 
  34:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  35:      /// </remarks> 
  36:      public class Gmail
  37:      {
  38:          private string serverUser, defaultMailbox;
  39:   
  40:          // If modifying these scopes, delete your previously saved credentials at ~/.credentials/gmail-dotnet-quickstart.json
  41:          private static string[] scopeList;
  42:          private static string applicationName;
  43:          private GmailService service;
  44:          private UsersResource.LabelsResource.ListRequest request;
  45:          private UsersResource.MessagesResource.SendRequest sendRequest;
  46:   
  47:          public enum MailType { Plain, Html };
  48:   
  49:          ////////////////////////////////////////////////////////////////////////////
  50:   
  51:          /// <summary>
  52:          ///
  53:          /// </summary>
  54:          public Gmail()
  55:          {
  56:              /*
  57:               * app.config
  58:               * <add key="imapServerUser" value="*" />
  59:               */
  60:   
  61:              // serverUser = ConfigurationManager.AppSettings["imapServerUser"].ToString();
  62:   
  63:              UserCredential credential;
  64:   
  65:              defaultMailbox = "Inbox";
  66:   
  67:              scopeList = new string[] { GmailService.Scope.GmailReadonly, GmailService.Scope.GmailSend };
  68:   
  69:              applicationName = "Gmail API .NET Quickstart";
  70:   
  71:              using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
  72:              {
  73:                  string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
  74:   
  75:                  credPath = Path.Combine(credPath, ".credentials/gmail-dotnet-quickstart.json");
  76:   
  77:                  credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, scopeList, "ofn.gov.kw@gmail.com", CancellationToken.None, new FileDataStore(credPath, true)).Result;
  78:   
  79:                  Console.WriteLine("Credential file saved to: " + credPath);
  80:              }
  81:   
  82:              // Create Gmail API service.
  83:              service = new GmailService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = applicationName });
  84:   
  85:              // Define parameters of request.
  86:              request = service.Users.Labels.List("me");
  87:          }
  88:   
  89:          ////////////////////////////////////////////////////////////////////////////
  90:   
  91:          /// <summary>
  92:          ///
  93:          /// </summary>
  94:          public void SendEmail(string name, string email, string subject, string content)
  95:          {
  96:              Message mailMessage = new global::Google.Apis.Gmail.v1.Data.Message();
  97:   
  98:              mailMessage.Payload = new MessagePart();
  99:              mailMessage.Payload.Headers = new List<MessagePartHeader>();
 100:   
 101:              mailMessage.Payload.Headers.Add(new MessagePartHeader() { Name = "From", Value = "ofn.gov.kw@gmail.com" });
 102:              mailMessage.Payload.Headers.Add(new MessagePartHeader() { Name = "Date", Value = DateTime.UtcNow.AddHours(3).ToString() });
 103:              mailMessage.Payload.Headers.Add(new MessagePartHeader() { Name = "Subject", Value = "Testing..." });
 104:              mailMessage.Payload.Headers.Add(new MessagePartHeader() { Name = "To", Value = email });
 105:   
 106:              mailMessage.Payload.Body = new MessagePartBody();
 107:              mailMessage.Payload.Body.Data = content;
 108:              //mailMessage.Payload.Body.Data...IsBodyHtml = (mailType == MailType.Html);
 109:   
 110:              /*
 111:              foreach (System.Net.Mail.Attachment attachment in email.Attachments)
 112:              {
 113:                  mailMessage.Attachments.Add(attachment);
 114:              }
 115:              */
 116:   
 117:              //var mimeMessage = global::Google.Apis.Gmail.v1.Data.MimeMessage.CreateFromMailMessage(mailMessage);
 118:   
 119:              /*
 120:              var gmailMessage = new global::Google.Apis.Gmail.v1.Data.Message
 121:              {
 122:                  Raw = Encode(mimeMessage.ToString())
 123:              };
 124:              */
 125:   
 126:              sendRequest = service.Users.Messages.Send(mailMessage, "ofn.gov.kw@gmail.com");
 127:   
 128:              sendRequest.Execute();
 129:          }
 130:   
 131:          ////////////////////////////////////////////////////////////////////////////
 132:   
 133:          /// <summary>
 134:          ///
 135:          /// </summary>
 136:          public static string Encode(string text)
 137:          {
 138:              byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
 139:   
 140:              return System.Convert.ToBase64String(bytes)
 141:                  .Replace('+', '-')
 142:                  .Replace('/', '_')
 143:                  .Replace("=", "");
 144:          }
 145:   
 146:          ////////////////////////////////////////////////////////////////////////////
 147:   
 148:          /// <summary>
 149:          ///
 150:          /// </summary>
 151:          public List<string> LabelList
 152:          {
 153:              get
 154:              {
 155:                  // case sensitive
 156:   
 157:                  List<string> labelList;
 158:                  IList<Label> labelIList;
 159:   
 160:                  labelList = new List<string>();
 161:                  labelIList = request.Execute().Labels;
 162:   
 163:                  Console.WriteLine("Labels:");
 164:   
 165:                  if (labelIList != null && labelIList.Count > 0)
 166:                  {
 167:                      foreach (var labelItem in labelIList)
 168:                      {
 169:                          labelList.Add(labelItem.Name);
 170:                          Console.WriteLine("{0}", labelItem.Name);
 171:                      }
 172:                  }
 173:                  else
 174:                  {
 175:                      Console.WriteLine("No labels found.");
 176:                  }
 177:   
 178:                  return labelList;
 179:              }
 180:          }
 181:   
 182:          ////////////////////////////////////////////////////////////////////////////
 183:   
 184:          /// <summary>
 185:          ///
 186:          /// </summary>
 187:          public void Test()
 188:          {
 189:              string body, from, date, subject;
 190:              UsersResource.MessagesResource.ListRequest listRequest;
 191:              ListMessagesResponse listMessagesResponse;
 192:   
 193:              from = date = string.Empty;
 194:   
 195:              listRequest = service.Users.Messages.List("ofn.gov.kw@gmail.com");
 196:              listRequest.LabelIds = "SENT"; // case sensitive
 197:              listRequest.IncludeSpamTrash = false;
 198:   
 199:              listMessagesResponse = listRequest.Execute();
 200:   
 201:              if (listMessagesResponse != null && listMessagesResponse.Messages != null)
 202:              {
 203:                  //loop through each email and get what fields you want... 
 204:                  foreach (var email in listMessagesResponse.Messages)
 205:                  {
 206:                      var emailInfoRequest = service.Users.Messages.Get("ofn.gov.kw@gmail.com", email.Id);
 207:                      var emailInfoResponse = emailInfoRequest.Execute();
 208:   
 209:                      if (emailInfoResponse != null)
 210:                      {
 211:                          //loop through the headers to get from, date, subject, body  
 212:   
 213:                          foreach (var messagePartHeader in emailInfoResponse.Payload.Headers)
 214:                          {
 215:                              if (messagePartHeader.Name == "Date") date = messagePartHeader.Value;
 216:                              else if (messagePartHeader.Name == "From") from = messagePartHeader.Value;
 217:                              else if (messagePartHeader.Name == "Subject") subject = messagePartHeader.Value;
 218:   
 219:                              if (date != "" && from != "")
 220:                              {
 221:                                  if (emailInfoResponse.Payload.MimeType == "text/html")
 222:                                  {
 223:                                      if (emailInfoResponse.Payload.Parts == null && emailInfoResponse.Payload.Body != null)
 224:                                      {
 225:                                          body = emailInfoResponse.Payload.Body.Data;
 226:                                      }
 227:                                      else
 228:                                      {
 229:                                          body = GetNestedParts(emailInfoResponse.Payload.Parts, "");
 230:                                      }
 231:   
 232:                                      byte[] data = FromBase64ForUrlString(body);
 233:                                      string decodedString = Encoding.UTF8.GetString(data);
 234:                                  }
 235:                              }
 236:                              else
 237:                              {
 238:   
 239:                              }
 240:                          }
 241:                      }
 242:                  }
 243:              }
 244:          }
 245:   
 246:          ////////////////////////////////////////////////////////////////////////////
 247:   
 248:          /// <summary>
 249:          ///
 250:          /// </summary>
 251:          private static String GetNestedParts(IList<MessagePart> part, string curr)
 252:          {
 253:              string s;
 254:   
 255:              s = curr;
 256:   
 257:              if (part == null)
 258:              {
 259:                  return s;
 260:              }
 261:              else
 262:              {
 263:                  foreach (var partList in part)
 264:                  {
 265:                      if (partList.Parts == null)
 266:                      {
 267:                          if (partList.Body != null && partList.Body.Data != null)
 268:                          {
 269:                              s += partList.Body.Data;
 270:                          }
 271:                      }
 272:                      else
 273:                      {
 274:                          return GetNestedParts(partList.Parts, s);
 275:                      }
 276:                  }
 277:   
 278:                  return s;
 279:              }
 280:          }
 281:   
 282:          ////////////////////////////////////////////////////////////////////////////
 283:   
 284:          /// <summary>
 285:          ///
 286:          /// </summary>
 287:          public List<string> MailboxList
 288:          {
 289:              get
 290:              {
 291:                  List<string> mailboxList;
 292:   
 293:                  mailboxList = new List<string>();
 294:   
 295:                  //foreach (Mailbox mailbox in imap.Mailboxes) mailboxList.Add(mailbox.Name);
 296:   
 297:                  return mailboxList;
 298:              }
 299:          }
 300:   
 301:          /*
 302:          ////////////////////////////////////////////////////////////////////////////
 303:  
 304:          /// <summary>
 305:          ///
 306:          /// </summary>
 307:          public void CreateMailbox(string mailboxName)
 308:          {
 309:              imap.CreateMailbox(mailboxName);
 310:          }
 311:  
 312:          ////////////////////////////////////////////////////////////////////////////
 313:  
 314:          /// <summary>
 315:          ///
 316:          /// </summary>
 317:          public string DeleteMailbox(string mailboxName)
 318:          {
 319:              return imap.DeleteMailbox(mailboxName);
 320:          }
 321:  
 322:          ////////////////////////////////////////////////////////////////////////////
 323:  
 324:          /// <summary>
 325:          ///
 326:          /// </summary>
 327:          public Header ReadHeader(Mailbox mailbox, int i)
 328:          {
 329:              Header header;
 330:              ActiveUp.Net.Mail.Header aHeader;
 331:  
 332:              header = new Header();
 333:  
 334:              try
 335:              {
 336:                  aHeader = mailbox.Fetch.HeaderObject(i);
 337:  
 338:                  header.MessageId = aHeader.MessageId;
 339:                  header.From = aHeader.From.Email;
 340:                  header.Subject = aHeader.Subject;
 341:              }
 342:              catch (Imap4Exception iex)
 343:              {
 344:              }
 345:              catch (Exception ex)
 346:              {
 347:              }
 348:              finally
 349:              {
 350:              }
 351:  
 352:              return header;
 353:          }
 354:  
 355:          ////////////////////////////////////////////////////////////////////////////
 356:  
 357:          /// <summary>
 358:          ///
 359:          /// </summary>
 360:          public int MoveMessagesFromEmailToMailbox(string email, string destinationMailboxName)
 361:          {
 362:              int numberOfMessagesMoved;
 363:              int[] messageOrdinalList;
 364:              string searchPhrase;
 365:              Mailbox mailbox;
 366:  
 367:              numberOfMessagesMoved = 0;
 368:  
 369:              mailbox = imap.SelectMailbox(defaultMailbox);
 370:  
 371:              searchPhrase = @"FROM """ + email + @"""";
 372:  
 373:              messageOrdinalList = mailbox.Search(searchPhrase);
 374:  
 375:              if (messageOrdinalList != null && messageOrdinalList.Length > 0)
 376:              {
 377:                  // read message and check that from-email value before moving
 378:                  for (int i = messageOrdinalList.Length - 1; i >= 0; i--)
 379:                  {
 380:                      MoveMessage(mailbox, messageOrdinalList[i], destinationMailboxName);
 381:                      numberOfMessagesMoved++;
 382:                  }
 383:              }
 384:  
 385:              return numberOfMessagesMoved;
 386:          }
 387:  
 388:          ////////////////////////////////////////////////////////////////////////////
 389:  
 390:          /// <summary>
 391:          ///
 392:          /// </summary>
 393:          public void MoveMessage(string messageId, string destinationMailboxName)
 394:          {
 395:              int[] messageOrdinalList;
 396:              string searchPhrase;
 397:              Mailbox mailbox;
 398:              ActiveUp.Net.Mail.Header header;
 399:  
 400:              mailbox = imap.SelectMailbox(defaultMailbox);
 401:  
 402:              searchPhrase = @"ALL";
 403:  
 404:              messageOrdinalList = mailbox.Search(searchPhrase);
 405:  
 406:              if (messageOrdinalList != null && messageOrdinalList.Length > 0)
 407:              {
 408:                  for (int i = messageOrdinalList.Length - 1; i >= 0; i--)
 409:                  {
 410:                      header = mailbox.Fetch.HeaderObject(messageOrdinalList[i]);
 411:  
 412:                      if (header.MessageId == messageId)
 413:                      {
 414:                          MoveMessage(mailbox, messageOrdinalList[i], destinationMailboxName);
 415:  
 416:                          break;
 417:                      }
 418:                  }
 419:              }
 420:          }
 421:  
 422:          ////////////////////////////////////////////////////////////////////////////
 423:  
 424:          /// <summary>
 425:          ///
 426:          /// </summary>
 427:          public void MoveMessage(Mailbox mailbox, int messageOrdinal, string destinationMailboxName)
 428:          {
 429:              mailbox.MoveMessage(messageOrdinal, destinationMailboxName);
 430:          }
 431:  
 432:          ////////////////////////////////////////////////////////////////////////////
 433:  
 434:          /// <summary>
 435:          /// Message list from Inbox
 436:          /// </summary>
 437:          public void MessageList(out List<Message> messageList)
 438:          {
 439:              string searchPhrase;
 440:  
 441:              searchPhrase = "ALL";
 442:  
 443:              SearchPhraseMessageList(searchPhrase, out messageList);
 444:          }
 445:  
 446:          ////////////////////////////////////////////////////////////////////////////
 447:  
 448:          /// <summary>
 449:          /// List of messages from Inbox that were sent from email
 450:          /// </summary>
 451:          public void MessageList(string email, out List<Message> messageList)
 452:          {
 453:              string searchPhrase;
 454:  
 455:              searchPhrase = @"FROM """ + email + @"""";
 456:  
 457:              SearchPhraseMessageList(searchPhrase, out messageList);
 458:          }
 459:  
 460:          ////////////////////////////////////////////////////////////////////////////
 461:  
 462:          /// <summary>
 463:          ///
 464:          /// </summary>
 465:          private void SearchPhraseMessageList(string searchPhrase, out List<Message> messageList)
 466:          {
 467:              Message message;
 468:              Mailbox mailbox;
 469:              MessageCollection messageCollection;
 470:  
 471:              messageList = new List<Message>();
 472:  
 473:              mailbox = imap.SelectMailbox(defaultMailbox);
 474:  
 475:              try
 476:              {
 477:                  messageCollection = mailbox.SearchParse(searchPhrase);
 478:  
 479:                  foreach (ActiveUp.Net.Mail.Message m in messageCollection)
 480:                  {
 481:                      message = new Message();
 482:  
 483:                      message.MessageId = m.MessageId;
 484:                      message.From = m.From.Email;
 485:                      message.Subject = m.Subject;
 486:                      message.BodyText = m.BodyText.TextStripped;
 487:                      message.Date = m.Date;
 488:                      message.ReceivedDate = m.ReceivedDate;
 489:  
 490:                      foreach (ActiveUp.Net.Mail.MimePart mp in m.Attachments)
 491:                      {
 492:                          //if (mp.IsText)
 493:                          //{
 494:                          message.Attachments.Add(new Attachment
 495:                          {
 496:                              FileName = mp.Filename,
 497:                              ContentType = mp.ContentType.MimeType,
 498:                              Content = mp.TextContent
 499:                          });
 500:                          //}
 501:                      }
 502:  
 503:                      messageList.Add(message);
 504:  
 505:                      this.Log(string.Format("Success: Message read: {0},{1},{2}", m.MessageId, m.From, m.Subject));
 506:                  }
 507:              }
 508:              catch (Imap4Exception iex)
 509:              {
 510:                  this.Log(string.Format("Imap4 Error: {0}", iex.Message));
 511:              }
 512:              catch (Exception ex)
 513:              {
 514:                  this.Log(string.Format("Failed: {0}", ex.Message));
 515:              }
 516:              finally
 517:              {
 518:              }
 519:          }
 520:  
 521:          ////////////////////////////////////////////////////////////////////////////
 522:  
 523:          /// <summary>
 524:          ///
 525:          /// </summary>
 526:          public List<Header> HeaderList(Mailbox mailbox)
 527:          {
 528:              Header header;
 529:              List<Header> headerList;
 530:              ActiveUp.Net.Mail.Header aHeader;
 531:  
 532:              headerList = new List<Header>(mailbox.MessageCount);
 533:  
 534:              try
 535:              {
 536:                  for (int i = 1; i <= mailbox.MessageCount; i++)
 537:                  {
 538:                      header = new Header();
 539:  
 540:                      aHeader = mailbox.Fetch.MessageObject(i);
 541:  
 542:                      header.MessageId = aHeader.MessageId;
 543:                      header.From = aHeader.From.Email;
 544:                      header.Subject = aHeader.Subject;
 545:  
 546:                      this.Log(string.Format("Success: Header read: {0},{1},{2}", header.MessageId, header.From, header.Subject));
 547:                  }
 548:              }
 549:              catch (Imap4Exception iex)
 550:              {
 551:              }
 552:              catch (Exception ex)
 553:              {
 554:              }
 555:              finally
 556:              {
 557:              }
 558:  
 559:              return headerList;
 560:          }
 561:  
 562:          ////////////////////////////////////////////////////////////////////////////
 563:  
 564:          /// <summary>
 565:          ///
 566:          /// </summary>
 567:          public void DeleteMessage(string messageId)
 568:          {
 569:              int[] messageOrdinalList;
 570:              string searchPhrase;
 571:              Mailbox mailbox;
 572:              ActiveUp.Net.Mail.Header header;
 573:  
 574:              mailbox = imap.SelectMailbox(defaultMailbox);
 575:  
 576:              searchPhrase = @"ALL";
 577:  
 578:              messageOrdinalList = mailbox.Search(searchPhrase);
 579:  
 580:              if (messageOrdinalList != null && messageOrdinalList.Length > 0)
 581:              {
 582:                  for (int i = messageOrdinalList.Length - 1; i >= 0; i--)
 583:                  {
 584:                      header = mailbox.Fetch.HeaderObject(messageOrdinalList[i]);
 585:  
 586:                      if (header.MessageId == messageId)
 587:                      {
 588:                          DeleteMessage(mailbox, messageOrdinalList[i]);
 589:                      }
 590:                  }
 591:              }
 592:          }
 593:  
 594:          ////////////////////////////////////////////////////////////////////////////
 595:  
 596:          /// <summary>
 597:          ///
 598:          /// </summary>
 599:          private void DeleteMessage(Mailbox mailbox, int messageOrdinal)
 600:          {
 601:              mailbox.DeleteMessage(messageOrdinal, true);
 602:          }
 603:          */
 604:   
 605:          ////////////////////////////////////////////////////////////////////////////
 606:   
 607:          /// <summary>
 608:          ///
 609:          /// </summary>
 610:          public static byte[] FromBase64ForUrlString(string base64ForUrlInput)
 611:          {
 612:              int padChars = (base64ForUrlInput.Length % 4) == 0 ? 0 : (4 - (base64ForUrlInput.Length % 4));
 613:              StringBuilder result = new StringBuilder(base64ForUrlInput, base64ForUrlInput.Length + padChars);
 614:   
 615:              result.Append(String.Empty.PadRight(padChars, '='));
 616:              result.Replace('-', '+');
 617:              result.Replace('_', '/');
 618:   
 619:              return Convert.FromBase64String(result.ToString());
 620:          }
 621:   
 622:          ////////////////////////////////////////////////////////////////////////////
 623:          ////////////////////////////////////////////////////////////////////////////
 624:      }
 625:  }