2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

c#でgraph apiを使ってメールにファイルを添付し、メールを送信するサンプル

Last updated at Posted at 2023-02-22
// Create the message with attachment.
byte[] contentBytes = System.IO.File.ReadAllBytes("test.xlsx");
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
MessageAttachmentsCollectionPage attachments = new MessageAttachmentsCollectionPage();

attachments.Add(new FileAttachment
{
    ODataType = "#microsoft.graph.fileAttachment",
    ContentBytes = contentBytes,
    ContentType = contentType,
    ContentId = "test",
    Name = "test.xlsx"
});

var message = new Message
{
    Subject = "こちらは件名です",
    Body = new ItemBody
    {
        ContentType = BodyType.Text,
        Content = "こちらは本文です。"
    },
    ToRecipients = new List<Recipient>()
    {
        new Recipient
        {
            EmailAddress = new EmailAddress
            {
                Address = "meganb@contoso.onmicrosoft.com"
            }
        }
    },
   Attachments = attachments
};

//await graphClient.Users["test@outlook.jp"]→アプリケーション認証

//委任認証
await graphClient.Me
    .SendMail(message,true)
    .Request()
    .PostAsync();

参考サイト

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?