Skip to content

Instantly share code, notes, and snippets.

@manishtiwari25
Last active February 19, 2021 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manishtiwari25/f94cb1c70e085700d08828c90a1a6eb0 to your computer and use it in GitHub Desktop.
Save manishtiwari25/f94cb1c70e085700d08828c90a1a6eb0 to your computer and use it in GitHub Desktop.
Send mail using O365 account c#
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using RestSharp;
public class Body
{
[JsonPropertyName("contentType")]
public string ContentType { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }
}
public class EmailAddress
{
[JsonPropertyName("address")]
public string Address { get; set; }
}
public class ToRecipient
{
[JsonPropertyName("emailAddress")]
public EmailAddress EmailAddress { get; set; }
}
public class Attachment
{
[JsonPropertyName("@odata.type")]
public string Type { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("contentType")]
public string ContentType { get; set; }
[JsonPropertyName("contentBytes")]
public string ContentBytes { get; set; }
}
public class From
{
[JsonPropertyName("emailAddress")]
public EmailAddress EmailAddress { get; set; }
}
public class Message
{
[JsonPropertyName("subject")]
public string Subject { get; set; }
[JsonPropertyName("body")]
public Body Body { get; set; }
[JsonPropertyName("toRecipients")]
public List<ToRecipient> ToRecipients { get; set; }
[JsonPropertyName("attachments")]
public List<Attachment> Attachments { get; set; }
[JsonPropertyName("from")]
public From From { get; set; }
}
public class Example
{
[JsonPropertyName("message")]
public Message Message { get; set; }
}
class Solution
{
static void Main(String[] args)
{
var payload = new Example
{
Message = new Message
{
Subject = "Send email",
Body = new Body
{
Content = "wuirbmndf",
ContentType = "text"
},
ToRecipients = new List<ToRecipient> { new ToRecipient { EmailAddress = new EmailAddress { Address = ""}} },
From = new From { EmailAddress = new EmailAddress { Address = "" } },
Attachments = new List<Attachment>
{
new Attachment
{
Type = "#microsoft.graph.fileAttachment",
Name = "attachment.txt",
ContentType="text/plain",
ContentBytes="SGVsbG8gV29ybGQh"
}
}
}
};
var json = JsonSerializer.Serialize(payload);
var token = @"";
RestClient client = new RestClient();
client.BaseUrl = new Uri(String.Format("https://graph.microsoft.com/v1.0/me/sendmail"));
RestRequest request = new RestRequest();
request.Method = Method.POST;
request.AddParameter("Authorization", string.Format("Bearer " + token), ParameterType.HttpHeader);
request.AddParameter("application/json", json, ParameterType.RequestBody);
//request.AddHeader("ContentType", "application/json");
//request.JsonSerializer.ContentType = "string";
var result = client.Execute(request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment