C Sharp - Email

C Sharp Email

Support for email was added in .net core 2.0. You used the SmtpClient object. But then Microsoft marked that class as depreciated and started recommending you use the MailKit library instead. Here’s a poniter to that file.

https://dotnetcoretutorials.com/2017/11/02/using-mailkit-send-receive-email-asp-net-core/

In the Package Manager Console execute the following statement
Install-Package MailKit

I created a test program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using MailKit.Net.Smtp;
using MailKit;
using MimeKit;

using System;
using System.Collections.Generic;
using System.Linq;

using System.Threading.Tasks;

namespace DDv4.API.eMail
{
public class emailService
{
//
// sample from
// https://github.com/jstedfast/MailKit
//
//public static void TestMessage(string[] args)
public static void TestMessage()
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Joey Tribbiani", "joey@friends.com"));
message.To.Add(new MailboxAddress("Mark Wachdorf", "wachdorfm@hotmail.com"));
message.Subject = "How you doin'?";

message.Body = new TextPart("plain")
{
Text = @"Hey Chandler,

I just wanted to let you know that Monica and I were going to go play some paintball, you in?

-- Joey"
};

using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, false);

// Note: only needed if the SMTP server requires authentication
client.Authenticate("emailaddress@gamil.com", "password");

client.Send(message);
client.Disconnect(true);
}
}
}
}

And am getting authentication errors. Hotmail didn’t work either.

According to
https://stackoverflow.com/questions/33496290/how-to-send-email-by-using-mailkit

I need to setup an OAuth 2.0 account.
https://developers.google.com/identity/protocols/oauth2