M365 使用 Microsoft Graph 寄信範例

by | 10 月 15, 2024 | M365, 程式 | 0 comments

Views: 6

公司換了M365信箱,原本的SMTP Server正式退休,原本有個系統會代替大家發信的,但退休之後就不能用了,因此開始研究M365怎麼寄信

申請Microsoft Graph Api權限

系統權限設定

  1. 要去應用程式註冊一個API
  2. 要申請Mail.Send權限,已委派跟應用程式都要
  3. 這個權限註冊之後要系統管理員同意。

開始寫程式

Nuget

Nuget安裝

  1. Azure.Identity
  2. Microsoft.Graph;

程式碼

  1. 採用.Net Core 8,我弄比較久是Api key那邊….AI給的位置不是很準,位置對應我寫在註解了
  2. 要採用可以支援非同步的.Net版本才能Work,所以太舊的不能用
using Azure.Identity;
using Microsoft.Graph;
using System.Threading.Tasks;
using Microsoft.Graph.Models;

namespace M365_MailTest
{

	internal class Program
	{
		//Microsoft Entra admin center 
		//https://entra.microsoft.com/

		/// <summary>
		/// 應用程式 (用戶端) 識別碼
		/// </summary>
		/// <remarks>
		/// 應用程式註冊 > 應用程式 (用戶端) 識別碼
		/// Applications > App Registrations > Application (client) ID
		/// </remarks>
		static string clientId = "";

		/// <summary>
		/// 租用戶識別碼
		/// </summary>
		/// <remarks>
		/// 概觀 > 租用戶識別碼
		/// Overview >Tenant Id
		/// </remarks>

		static string tenantId = "";
		//Certificates & secrets > Client secrets > Secret ID

		/// <summary>
		/// 用戶端密碼
		/// </summary>
		/// <remarks>
		///  應用程式註冊 > 憑證及秘密 > 用戶端密碼 > 值
		///  Certificates & secrets > Client secrets > Secret ID > Value
		/// </remarks>
		static string clientSecret = "";

		static async Task Main(string[] args)
		{
			
			var options = new ClientSecretCredentialOptions
			{
				AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
			};
			var clientCredential = new ClientSecretCredential(
				tenantId, clientId, clientSecret, options);
			var graphClient = new GraphServiceClient(clientCredential);

			var message = new Microsoft.Graph.Models.Message
			{
				//主旨
				Subject = "Test Email from Microsoft Graph",
				Body = new ItemBody
				{
					//內文類型
					ContentType = BodyType.Text,
					//內文
					Content = "This is a test email",
				},
				ToRecipients = new List<Recipient>
				{
					//收件人1
					new Recipient
					{
						EmailAddress = new EmailAddress
						{
							//收件人
							Address = "",
						}
					},
					//收件人2
					new Recipient()
					{
						EmailAddress = new EmailAddress{
							Name = "",
							Address = "",
							
						} 
					}

				},
				// 副本收件人 (CC)
				CcRecipients = new List<Recipient>
				{
					new Recipient
					{
						EmailAddress = new EmailAddress
						{
							Address = ""
						}
					}
				},
				// 密件副本收件人 (BCC)
				BccRecipients = new List<Recipient>
				{
					new Recipient
					{
						EmailAddress = new EmailAddress
						{
							Address = ""
						}
					}
				}
			};

			Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody body = new()
			{
				Message = message,
				//要不要保留寄件備份
				SaveToSentItems = true
			};
			try
			{
				await graphClient.Users["寄件人信箱"]
					.SendMail
					.PostAsync(body);
			}
			catch (Exception ex)
			{
				// log your exception here
			}
		}		
	}
}

參考資訊

  1. ChatGpt
  2. https://stackoverflow.com/questions/70771954/how-to-send-email-from-any-one-email-using-microsoft-graph

0 Comments

Submit a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *