--- /dev/null
+using Microsoft.Graph;
+using Microsoft.Identity.Client;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MailDivSender
+{
+ /// <summary>
+ /// Office365メール送信
+ /// </summary>
+ public class Office365SendMail : BaseSendMail
+ {
+ /// <summary>
+ /// コンストラクタ
+ /// </summary>
+ public Office365SendMail()
+ {
+ }
+
+ /// <summary>
+ /// ログインプロパイダー
+ /// </summary>
+ public class MsalAuthenticationProvider : IAuthenticationProvider
+ {
+ /// <summary>
+ /// アプリID
+ /// </summary>
+ public string ClientID;
+
+ /// <summary>
+ /// コールバックURI
+ /// </summary>
+ public string MyUri;
+
+ /// <summary>
+ /// トークン
+ /// </summary>
+ public string AccessToken = string.Empty;
+
+ /// <summary>
+ /// 認証要求
+ /// </summary>
+ /// <param name="request"></param>
+ /// <returns></returns>
+ public async Task AuthenticateRequestAsync(HttpRequestMessage request)
+ {
+ var Scopes = new[] { "User.Read", "Mail.Send" };
+
+ if (AccessToken == string.Empty)
+ {
+ //クライアント構築
+ var clientApplication = PublicClientApplicationBuilder.Create(ClientID).WithRedirectUri(MyUri).Build();
+ var ar = await clientApplication.AcquireTokenInteractive(Scopes).ExecuteAsync();
+ AccessToken = ar.AccessToken;
+ }
+
+ request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AccessToken);
+ }
+ }
+
+ /// <summary>
+ /// 送信
+ /// </summary>
+ /// <param name="callback"></param>
+ /// <param name="countcallback"></param>
+ /// <param name="addresspack"></param>
+ /// <returns></returns>
+ public (bool result, Exception exception) Send(SendMailProgressCallback callback, SendMailProgressCountCallback countcallback, MailInfoPack addresspack)
+ {
+ int iCount = 0;
+ int iAllCount = addresspack.AttachList.Count;
+
+ var ClientID = Properties.Settings.Default.Office365ClientID;
+ var MyUri = Properties.Settings.Default.Office365Uri;
+ Exception exception = null;
+
+ try
+ {
+ var gsc = new GraphServiceClient(new MsalAuthenticationProvider()
+ {
+ ClientID = ClientID,
+ MyUri = MyUri,
+ });
+
+ foreach (var filepath in addresspack.AttachList)
+ {
+ callback($"送信中 {filepath}");
+
+ var email = new Message
+ {
+ ToRecipients = new[]
+ {
+ new Recipient
+ {
+ EmailAddress = new EmailAddress
+ {
+ Name = addresspack.ToAddress.Name,
+ Address = addresspack.ToAddress.Address
+ }
+ }
+ },
+ From = new Recipient
+ {
+ EmailAddress = new EmailAddress
+ {
+ Name = addresspack.FromAddress.Name,
+ Address = addresspack.FromAddress.Address
+ }
+ },
+ Subject = $"{addresspack.Subject} ({iCount + 1}/{iAllCount})",
+ Body = new ItemBody
+ {
+ ContentType = BodyType.Text,
+ Content = addresspack.Message,
+ },
+ };
+ //CC
+ foreach (var sendinfopack in addresspack.SendAddressList)
+ {
+ if (sendinfopack.Kind == SelectedAddressControl.CCKindType.CC)
+ {
+ if(email.CcRecipients == null )
+ email.CcRecipients = new List<Recipient>();
+
+ (email.CcRecipients as List<Recipient>).Add(
+ new Recipient
+ {
+ EmailAddress = new EmailAddress
+ {
+ Name = sendinfopack.Address.Name,
+ Address = sendinfopack.Address.Address
+ }
+ });
+ }
+ if (sendinfopack.Kind == SelectedAddressControl.CCKindType.BCC)
+ {
+ if (email.BccRecipients == null)
+ email.BccRecipients = new List<Recipient>();
+
+ (email.BccRecipients as List<Recipient>).Add(
+ new Recipient
+ {
+ EmailAddress = new EmailAddress
+ {
+ Name = sendinfopack.Address.Name,
+ Address = sendinfopack.Address.Address
+ }
+ });
+ }
+ }
+
+ //添付
+ email.Attachments = new MessageAttachmentsCollectionPage();
+ email.Attachments.Add(new FileAttachment
+ {
+ ODataType = "#microsoft.graph.fileAttachment",
+ ContentBytes = System.IO.File.ReadAllBytes(filepath),
+ Name = Path.GetFileName(filepath),
+ });
+
+ gsc.Me.SendMail(email).Request().PostAsync().Wait();
+
+ callback($"完了 {filepath}");
+
+ iCount++;
+
+ countcallback(iCount);
+ }
+
+ return (result: true, exception: null);
+ }
+ catch( Exception ex )
+ {
+ exception = ex;
+ }
+
+ return (result: false, exception: exception);
+ }
+ }
+}