forked from cosullivan/SmtpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleMailClient.cs
More file actions
63 lines (52 loc) · 1.66 KB
/
Copy pathSampleMailClient.cs
File metadata and controls
63 lines (52 loc) · 1.66 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Threading;
using MailKit.Net.Smtp;
using MimeKit;
namespace SampleApp
{
public static class SampleMailClient
{
public static void Send(
string from = null,
string to = null,
string subject = null,
string user = null,
string password = null,
MimeEntity body = null,
int count = 1,
int recipients = 1,
bool useSsl = false,
int port = 9025)
{
var message = new MimeMessage();
message.From.Add(MailboxAddress.Parse(from ?? "from@sample.com"));
for (var i = 0; i < recipients; i++)
{
message.To.Add(MailboxAddress.Parse(to ?? $"to_{i}@sample.com"));
}
message.Subject = subject ?? "Hello";
message.Body = body ?? new TextPart("plain")
{
Text = "Hello World"
};
using var client = new SmtpClientEx();
client.Connect("localhost", port, useSsl);
if (user != null && password != null)
{
client.Authenticate(user, password);
}
client.SendUnknownCommand("ABCD EFGH IJKL");
while (count-- > 0)
{
client.Send(message);
}
client.Disconnect(true);
}
internal class SmtpClientEx : SmtpClient
{
public SmtpResponse SendUnknownCommand(string command, CancellationToken cancellationToken = default)
{
return SendCommand(command, cancellationToken);
}
}
}
}