SMTP vs Microsoft Graph: Which Should You Use for Email in .NET?

SMTP vs Microsoft Graph: Which Should You Use for Email in .NET?

 Sending emails is a common requirement in .NET applications. Traditionally, developers have used SMTP, but Microsoft 365 applications can also use Microsoft Graph.

So, which approach is better?

1. SMTP

SMTP (Simple Mail Transfer Protocol) is the traditional method of sending email through a mail server.

.NET Example

using System.Net;
using System.Net.Mail;
var smtp = new SmtpClient("smtp.example.com", 587)
{
    EnableSsl = true,
    Credentials = new NetworkCredential(
        "sender@example.com",
        "password")
};
var mail = new MailMessage(
    "sender@example.com",
    "user@example.com");
mail.Subject = "Welcome";
mail.Body = "Welcome to our application.";
smtp.Send(mail);

SMTP is simple and works with many email providers. However, applications need to manage SMTP authentication and credentials carefully.

2. Microsoft Graph

Microsoft Graph provides an API-based approach for sending email through Exchange Online.

Instead of connecting directly to an SMTP server, the application uses HTTPS and OAuth authentication.

Microsoft Graph Request

POST https://graph.microsoft.com/v1.0/users/{user}/sendMail
Authorization: Bearer {access-token}
Content-Type: application/json

Request body:
{
  "message": {
    "subject": "Welcome",
    "body": {
      "contentType": "HTML",
      "content": "<h1>Welcome!</h1><p>Thanks for joining us.</p>"
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "user@example.com"
        }
      }
    ]
  }
}

Microsoft Graph with .NET

Using the Microsoft Graph SDK, the implementation can look like:

var message = new Message
{
    Subject = "Welcome",
    Body = new ItemBody
    {
        ContentType = BodyType.Html,
        Content = "<h1>Welcome!</h1>"
    },
    ToRecipients = new List<Recipient>
    {
        new Recipient
        {
            EmailAddress = new EmailAddress
            {
                Address = "user@example.com"
            }
        }
    }
};
await graphClient.Users["sender@example.com"]
    .SendMail
    .PostAsync(new SendMailPostRequestBody
    {
        Message = message,
        SaveToSentItems = true
    });

The application authenticates using Microsoft Entra ID/OAuth 2.0 rather than relying on a mailbox password.

SMTP vs Microsoft Graph

Which One Should You Choose?

Use SMTP when you need simple, provider-independent email delivery or already have an SMTP infrastructure.

Use Microsoft Graph when your application is built around Microsoft 365, Exchange Online, Azure, and Microsoft Entra ID.

Final Takeaway

SMTP remains a reliable email protocol, but Microsoft Graph is generally the better choice for new Microsoft 365-centric .NET applications because it provides modern OAuth authentication and deeper integration with the Microsoft ecosystem.

Post a Comment

0 Comments