Skip to content

Commit dee46f9

Browse files
committed
Figured out you can just send the MIME...
1 parent c138898 commit dee46f9

1 file changed

Lines changed: 30 additions & 67 deletions

File tree

SMTP2Graph/Program.cs

Lines changed: 30 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
using System.Net.Sockets;
44
using System.Net;
55
using System.Text.RegularExpressions;
6-
using Microsoft.Graph.Models;
76
using System.Text;
7+
using Microsoft.Kiota.Abstractions;
88

99
Console.WriteLine("Initializing...");
1010
var mail_listener = new TcpListener(IPAddress.Any, 25);
1111
var scopes = new[] { "https://graph.microsoft.com/.default" };
12+
1213
var options = new ClientSecretCredentialOptions
1314
{
1415
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
1516
};
17+
1618
var clientSecretCredential = new ClientSecretCredential(
1719
Environment.GetEnvironmentVariable("TENANT_ID"),
1820
Environment.GetEnvironmentVariable("CLIENT_ID"),
@@ -53,85 +55,55 @@
5355
void HandleConnection(TcpClient client, Stream stream, StreamWriter sw, StreamReader sr)
5456
{
5557
string From = string.Empty;
56-
List<string> To = new();
57-
string Body = string.Empty;
58-
string Subject = string.Empty;
59-
BodyType BType = BodyType.Text;
58+
string MIME = string.Empty;
6059
while (true)
6160
{
6261
var msg = sr.ReadLine();
6362
if (msg.StartsWith("HELO") || msg.StartsWith("EHLO"))
6463
{
64+
Console.WriteLine("HELO");
6565
sw.Write("250 Whatchu want?\r\n");
6666
continue;
6767
}
6868
if (msg.StartsWith("MAIL FROM:"))
6969
{
70+
Console.WriteLine("MAIL FROM");
7071
From = Regex.Match(msg, "<(.*)>").Groups[1].Value;
7172
sw.Write("250 You again?!\r\n");
7273
continue;
7374
}
7475
if (msg.StartsWith("RCPT TO:"))
7576
{
76-
To.Add(Regex.Match(msg, "<(.*)>").Groups[1].Value);
77+
Console.WriteLine("RCPT TO");
7778
sw.Write("250 Ok fine...\r\n");
7879
continue;
7980
}
8081
if (msg.StartsWith("DATA"))
8182
{
83+
Console.WriteLine("DATA");
8284
sw.Write("354 And whats so important that you have to bother me...?\r\n");
83-
(BType, Subject, Body) = ReadData(sr);
85+
MIME = ReadMIME(sr);
8486
sw.Write("250 Oh yea, that was *very* important... SMH.\r\n");
87+
Console.WriteLine("Sending to MS Graph...");
88+
SendMessage(From, MIME);
89+
Console.WriteLine("Sent.");
8590
continue;
8691
}
8792
if (msg.StartsWith("QUIT"))
8893
{
94+
Console.WriteLine("QUIT");
8995
sw.Write("221 Bye, don't talk to me...\r\n");
9096
break;
9197
}
9298
}
99+
Console.WriteLine("Closing connection...");
93100
client.Close();
94-
Console.WriteLine("Sending to MS Graph...");
95-
SendMessage(From, To, Subject, Body, BType);
96101
Console.WriteLine("Done.");
97102
}
98103

99-
(BodyType, string, string) ReadData(StreamReader sr)
100-
{
101-
string Subject = string.Empty;
102-
string Body;
103-
BodyType BType = BodyType.Text;
104-
bool Base64 = false;
105-
while (true)
106-
{
107-
var msg = sr.ReadLine();
108-
if (msg.StartsWith("Subject:"))
109-
{
110-
Subject = Regex.Match(msg, "Subject: (.*)").Groups[1].Value;
111-
continue;
112-
}
113-
if (msg.StartsWith("Content-Type:"))
114-
{
115-
if (msg.Contains("html")) BType = BodyType.Html;
116-
continue;
117-
}
118-
if (msg == "Content-Transfer-Encoding: base64")
119-
{
120-
Base64 = true;
121-
continue;
122-
}
123-
if (msg == string.Empty)
124-
{
125-
Body = ReadBody(sr, Base64);
126-
break;
127-
}
128-
}
129-
return (BType, Subject, Body);
130-
}
131-
132-
string ReadBody(StreamReader sr, bool Base64)
104+
string ReadMIME(StreamReader sr)
133105
{
134-
string Body = string.Empty;
106+
string MIME = string.Empty;
135107
while (true)
136108
{
137109
var msg = sr.ReadLine();
@@ -140,31 +112,22 @@ string ReadBody(StreamReader sr, bool Base64)
140112
{
141113
msg = ".";
142114
}
143-
Body += msg;
115+
MIME += msg + "\r\n";
144116
}
145-
if (Base64)
146-
{
147-
Body = Encoding.UTF8.GetString(Convert.FromBase64String(Body));
148-
}
149-
return Body;
117+
return MIME;
150118
}
151119

152-
void SendMessage(string From, List<string> To, string Subject, string Body, BodyType BType)
120+
void SendMessage(string From, string MIME)
153121
{
154-
List<Recipient> recipients = new();
155-
foreach (string email in To)
156-
{
157-
recipients.Add(new() { EmailAddress = new() { Address = email } });
158-
}
159-
_ = graphClient.Users[From].SendMail.PostAsync(new()
160-
{
161-
Message = new()
162-
{
163-
From = new() { EmailAddress = new() { Address = From } },
164-
ToRecipients = recipients,
165-
Subject = Subject,
166-
Body = new() { ContentType = BType, Content = Body }
167-
},
168-
SaveToSentItems = false
169-
});
122+
var mimeb64bytes = Encoding.UTF8.GetBytes(
123+
Convert.ToBase64String(
124+
Encoding.UTF8.GetBytes(MIME)
125+
)
126+
);
127+
var request = new RequestInformation();
128+
request.URI = new($"https://graph.microsoft.com/v1.0/users/{From}/sendMail");
129+
request.HttpMethod = Method.POST;
130+
request.Headers.Add("Content-Type", "text/plain");
131+
request.Content = new MemoryStream(mimeb64bytes);
132+
_ = graphClient.RequestAdapter.SendNoContentAsync(request);
170133
}

0 commit comments

Comments
 (0)