Skip to content

Commit b0428e2

Browse files
committed
Use mailkit, add optional credentials
1 parent 597d031 commit b0428e2

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/InEngine.Core/IO/Mail.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1-
using System;
2-
using System.Net.Mail;
1+
using MailKit.Net.Smtp;
2+
using MimeKit;
33

44
namespace InEngine.Core.IO
55
{
66
public class Mail
77
{
88
public string Host { get; set; }
99
public int Port { get; set; }
10+
public string Username { get; set; }
11+
public string Password { get; set; }
1012

1113
public void Send(string fromAddress, string toAddress, string subject, string body)
1214
{
13-
new SmtpClient() {
14-
Host = Host,
15-
Port = Port
16-
}.Send(new MailMessage(fromAddress, toAddress, subject, body));
15+
var message = new MimeMessage();
16+
message.From.Add(new MailboxAddress(fromAddress));
17+
message.To.Add(new MailboxAddress(toAddress));
18+
message.Subject = subject;
19+
message.Body = new TextPart("plain") {
20+
Text = body
21+
};
22+
23+
using (var client = new SmtpClient())
24+
{
25+
client.Connect(Host, Port, false);
26+
client.AuthenticationMechanisms.Remove("XOAUTH2");
27+
if (!string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password))
28+
client.Authenticate(Username, Password);
29+
client.Send(message);
30+
client.Disconnect(true);
31+
}
1732
}
1833
}
1934
}

src/InEngine.Core/InEngine.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<PackageReference Include="Goblinfactory.Konsole" Version="3.1.0" />
2929
<PackageReference Include="EntityFramework" Version="6.2.0" />
3030
<PackageReference Include="RestSharp" Version="106.1.0" />
31+
<PackageReference Include="MailKit" Version="1.22.0" />
3132
</ItemGroup>
3233
<ItemGroup>
3334
<Folder Include="Scheduling\" />

src/InEngine.Core/MailSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ public class MailSettings
66
public string Host { get; set; }
77
public int Port { get; set; }
88
public string From { get; set; }
9+
public string Username { get; set; }
10+
public string Password { get; set; }
911
}
1012
}

src/InEngine.Core/Scheduling/ExecutionLifeCycle.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ public void FirePostActions(AbstractCommand command)
7070
try
7171
{
7272
if (ShouldEmailOutput)
73-
new IO.Mail()
74-
{
73+
new IO.Mail() {
7574
Host = MailSettings.Host,
7675
Port = MailSettings.Port,
77-
}.Send(MailSettings.From, EmailOutputToAddress, emailSubject, commandOutput);
76+
Username = MailSettings.Username,
77+
Password = MailSettings.Password,
78+
}
79+
.Send(MailSettings.From, EmailOutputToAddress, emailSubject, commandOutput);
7880
}
7981
catch (Exception exception)
8082
{

0 commit comments

Comments
 (0)