Working with Gmail Messages and Filters

Getting Started with IGmailClient

Aspose.Email for .NET provides robust support for interacting with Gmail through the IGmailClient interface. You can manage messages and filters in a Gmail mailbox — including listing, fetching, sending, appending, and deleting emails, as well as managing custom filters for automatic classification.

To use Gmail features, you need to initialize an IGmailClient instance using your clientId, clientSecret, refreshToken, and target email address. All examples below assume this setup:

using (IGmailClient client = GmailClient.GetInstance(clientId, clientSecret, refreshToken, email))
{
    // Work with messages or filters
}

List Messages in a Gmail Mailbox

Retrieve all messages from the mailbox as lightweight GmailMessageInfo objects. The following code sample dmeonstrates how to retrieve a list of available messages in a Gmail mailbox without loading full content:

var messages = client.ListMessages();

Fetch and Delete Gmail Messages

The code sample below demonstrates how to read and delete messages from Gmail using their unique identifiers:

  1. Use client.ListMessages() to retrieve a list of messages.
  2. Set up a for loop to iterate through the first three messages.
  3. Inside the loop, fetch each message using client.FetchMessage(messages[i].Id).
  4. Print the subject and body of the fetched message.
  5. Call client.DeleteMessage(messages[i].Id, true) to delete the message and move it to trash.
  6. Confirm deletion with the message.
var messages = client.ListMessages();

for (int i = 0; i < 3; i++)
{
    var msg = client.FetchMessage(messages[i].Id);
    Console.WriteLine($"Message {i + 1}: Subject - {msg.Subject}, Body - {msg.Body}");

    client.DeleteMessage(messages[i].Id, true);
    Console.WriteLine($"Message {i + 1} moved to trash.");
}

Send a Message via Gmail

Compose and send an email, including attachments using the SendMessage() method. The code sample below demonstrates how to send an email through Gmail with an attachment:

MailMessage message = new MailMessage("sender@example.com", "recipient@example.com", "Weekly Report", "Attached is the weekly report.");

string attachmentPath = Path.Combine(TestUtil.GetTestDataPath(), "report.pdf");
message.Attachments.Add(new Attachment(attachmentPath));

string messageId = client.SendMessage(message);
Console.WriteLine($"Message with attachment sent! ID: {messageId}");