-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMailbot.cs
More file actions
94 lines (75 loc) · 2.94 KB
/
Mailbot.cs
File metadata and controls
94 lines (75 loc) · 2.94 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Mailbot.cs" company="Lord Design">
// © Lord Design. Modified GPL: You may use freely and commercially without modification; you can modify if result
// is also free.
// </copyright>
// <summary>
// Multi-threaded mailer to keep your systems running by sending messages asynchronously. If you're using it in a
// service that sends a lot of messages, be aware that you'll be limited by your .NET working threads.
// </summary>
// <author>aaron@lorddesign.net</author>
// --------------------------------------------------------------------------------------------------------------------
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
namespace Devlord.Utilities.Mail
{
/// <summary>
/// Multi-threaded mailer to keep your systems running by sending messages asynchronously. If you're using it in a
/// service that sends a lot of messages, be aware that you'll be limited by your .NET working threads.
/// </summary>
public class Mailbot
{
#region Fields
/// <summary>
/// The throttles.
/// </summary>
internal Throttles Throttles = new Throttles();
#endregion
#region Constructors and Destructors
/// <summary>
/// Private constructor to enforce use of singleton.
/// </summary>
internal Mailbot()
{
}
#endregion
#region Public Properties
/// <summary>
/// Gets the SMTP server.
/// </summary>
public string SmtpServer { get; internal set; }
public int SmtpPort { get; internal set; }
public string SmtpLogin { get; internal set; }
public string SmtpPassword { get; internal set; }
#endregion
#region Methods
// Per minute 180, Per hour 3600, Per day 10,000
public async Task QueueMail(Botmail message)
{
await SendMail(message);
}
// Per minute 180, Per hour 3600, Per day 10,000
private async Task SendMail(Botmail botmail)
{
Throttles.Wait();
var mm = new MimeMessage();
mm.From.Add(MailboxAddress.Parse(botmail.Addresser));
mm.Subject = botmail.Subject;
mm.Body = new TextPart(botmail.Format.ToString().ToLower())
{
Text = botmail.Body
};
botmail.Addressees.ForEach(a => mm.To.Add(MailboxAddress.Parse(a)));
using (var client = new SmtpClient())
{
await client.ConnectAsync(SmtpServer, SmtpPort, SecureSocketOptions.SslOnConnect);
await client.AuthenticateAsync(SmtpLogin, SmtpPassword);
await client.SendAsync(mm);
Throttles.Increment();
client.Disconnect(true);
}
}
#endregion
}
}