Created
April 15, 2025 11:21
-
-
Save aspose-com-gists/4bc429d05a90b13371c94017c85a4a88 to your computer and use it in GitHub Desktop.
Integrate Gmail into C# applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (IGmailClient client = GmailClient.GetInstance(clientId, clientSecret, refreshToken, email)) | |
{ | |
// Create a message to append to the "Inbox" | |
MailMessage message = new MailMessage("[email protected]", "[email protected]", "Subject for inbox message", "Body of the message"); | |
// Append the message to the inbox with the "INBOX" label | |
string messageId = client.AppendMessage(message, "INBOX"); | |
Console.WriteLine($"Message appended to the Inbox. ID: {messageId}"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (IGmailClient client = GmailClient.GetInstance(clientId, clientSecret, refreshToken, email)) | |
{ | |
// Create a filter for messages with a specific subject | |
Filter filter = new Filter | |
{ | |
MatchingCriteria = new Criteria { Subject = "Important" }, | |
Action = new Action { AddLabelIds = new string[] { "IMPORTANT" } } | |
}; | |
// Create a filter | |
string filterId = client.CreateFilter(filter); | |
Console.WriteLine($"Filter created! ID: {filterId}"); | |
// List all filters | |
var filters = client.ListFilters(); | |
foreach (var f in filters) | |
{ | |
Console.WriteLine($"Filter ID: {f.Id}"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (IGmailClient client = GmailClient.GetInstance(clientId, clientSecret, refreshToken, email)) | |
{ | |
var filters = client.ListFilters(); | |
foreach (var filter in filters) | |
{ | |
client.DeleteFilter(filter.Id); | |
Console.WriteLine($"Filter ID: {filter.Id} deleted."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (IGmailClient client = GmailClient.GetInstance(clientId, clientSecret, refreshToken, email)) | |
{ | |
// List all messages in the mailbox | |
var messages = client.ListMessages(); | |
// Fetch and display the first 3 messages | |
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}"); | |
// Delete the message by moving it to trash | |
client.DeleteMessage(messages[i].Id, true); | |
Console.WriteLine($"Message {i + 1} moved to trash."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Obtain OAuth2 Token for Gmail Access | |
internal static TokenResponse GetAccessTokenByAuthCode(string authorizationCode) | |
{ | |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://oauth2.googleapis.com/token"); | |
request.CookieContainer = new CookieContainer(); | |
request.Method = "POST"; | |
request.ContentType = "application/x-www-form-urlencoded"; | |
string clientId = System.Web.HttpUtility.UrlEncode(userClientId); | |
string clientSecret = System.Web.HttpUtility.UrlEncode(userClientSecret); | |
string authCode = System.Web.HttpUtility.UrlEncode(authorizationCode); | |
string redirectUri = System.Web.HttpUtility.UrlEncode("urn:ietf:wg:oauth:2.0:oob"); | |
string grantType = System.Web.HttpUtility.UrlEncode("authorization_code"); | |
string encodedParameters = $"client_id={clientId}&client_secret={clientSecret}&code={authCode}&redirect_uri={redirectUri}&grant_type={grantType}"; | |
byte[] requestData = Encoding.UTF8.GetBytes(encodedParameters); | |
request.ContentLength = requestData.Length; | |
if (requestData.Length > 0) | |
using (Stream stream = request.GetRequestStream()) | |
stream.Write(requestData, 0, requestData.Length); | |
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | |
string responseText; | |
using (TextReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII)) | |
responseText = reader.ReadToEnd(); | |
TokenResponse tokensResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<TokenResponse>(responseText); | |
return tokensResponse; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using the Gmail API to send an email with an attachment | |
using (IGmailClient client = GmailClient.GetInstance(clientId, clientSecret, refreshToken, email)) | |
{ | |
// Create a new message | |
MailMessage message = new MailMessage("[email protected]", "[email protected]", "Weekly Report", "Attached is the weekly report."); | |
// Add an attachment | |
string attachmentPath = Path.Combine(TestUtil.GetTestDataPath(), "report.pdf"); | |
message.Attachments.Add(new Attachment(attachmentPath)); | |
// Send the message using Gmail Client | |
string messageId = client.SendMessage(message); | |
Console.WriteLine($"Message with attachment sent! ID: {messageId}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment