{"id":10313,"date":"2026-09-25T08:59:24","date_gmt":"2026-09-25T07:59:24","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=10313"},"modified":"2026-09-25T09:01:42","modified_gmt":"2026-09-25T08:01:42","slug":"send-mail-with-microsoft-graph","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/send-mail-with-microsoft-graph\/","title":{"rendered":"C# Send mail with Microsoft Graph"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Prerequisites<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install packages: <code>Azure.Identity<\/code> and <code>Microsoft.Graph<\/code>.<\/li>\n\n\n\n<li>Register an application in Azure Entra ID and grant <code>Mail.Send<\/code> permissions (Application or Delegated)<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using Azure.Identity;\nusing Microsoft.Graph;\nusing Microsoft.Graph.Models;\nusing Microsoft.Graph.Users.Item.SendMail;\n\n\/\/ Credentials\nstring tenantId = \"YOUR_TENANT_ID\";\nstring clientId = \"YOUR_CLIENT_ID\";\nstring clientSecret = \"YOUR_CLIENT_SECRET\";\nstring userId = \"sender@yourdomain.com\"; \/\/ User ID or User Principal Name\n\nstring filePath = \"C:\\\\document.pdf\";\nbyte[] fileBytes = await File.ReadAllBytesAsync(filePath);\nstring base64Content = Convert.ToBase64String(fileBytes);\n\nvar attachment = new FileAttachment\n{\n    OdataType = \"#microsoft.graph.fileAttachment\",\n    Name = \"document.pdf\",\n    ContentType = \"application\/pdf\",\n    ContentBytes = Convert.FromBase64String(base64Content)\n};\n\n\nvar scopes = new[] { \"https:\/\/graph.microsoft.com\/.default\" };\n\n\/\/ Authenticate using Client Secret\nvar credential = new ClientSecretCredential(tenantId, clientId, clientSecret);\nvar graphClient = new GraphServiceClient(credential, scopes);\n\n\/\/ Create the message\nvar requestBody = new SendMailPostRequestBody\n{\n    Message = new Message\n    {\n        Subject = \"Hello from Microsoft Graph\",\n        Body = new ItemBody\n        {\n            ContentType = BodyType.Text,\n            Content = \"This is a test email sent using C# and Microsoft Graph.\"\n        },\n        ToRecipients = new List&lt;Recipient>\n        {\n            new Recipient\n            {\n                EmailAddress = new EmailAddress\n                {\n                    Address = \"recipient@example.com\"\n                }\n            }\n        },\n        Attachments = new List&lt;Attachment> { attachment }\n    },\n    SaveToSentItems = true\n};\n\n\/\/ Send the email\nawait graphClient.Users[userId].SendMail.PostAsync(requestBody);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisites<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10313","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/10313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=10313"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/10313\/revisions"}],"predecessor-version":[{"id":10319,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/10313\/revisions\/10319"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=10313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=10313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=10313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}