/
SmtpSocket.cs
101 lines (80 loc) · 2.87 KB
/
SmtpSocket.cs
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
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
namespace SmtpTest
{
public class SmtpSocket : IDisposable
{
private SmtpHost smtp;
private Socket socket;
private string lastResponse;
public SmtpSocket(SmtpHost smtp)
{
this.smtp = smtp;
this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void Connect()
{
socket.Connect(smtp.Host, smtp.Port);
lastResponse = Receive();
}
public bool Authenticate(SmtpLogin login)
{
bool fAuthenticated = false;
Send("EHLO " + System.Net.Dns.GetHostName(), true);
Send("EHLO " + System.Net.Dns.GetHostName(), true);
if ( IsResponseCode ("250") )
{
Send("AUTH LOGIN", true);
if ( IsResponseCode ("334") )
{
string username = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(login.Username));
string password = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(login.Password));
Send(username, true);
Send(password, true);
if ( IsResponseCode ("235") )
{
// TODO send a test mail through these credentials to verify it's correctness. This can be a false-positive aswell.
fAuthenticated = true;
}
}
Send("close", true);
}
return fAuthenticated;
}
private void Send(string data, bool eol)
{
string formed = data + ((eol) ? "\r\n" : string.Empty);
socket.Send(System.Text.Encoding.Unicode.GetBytes(formed));
lastResponse = Receive();
}
private bool IsResponseCode(string expected)
{
// FIXME: improve this
return lastResponse.Contains(expected);
}
private string Receive()
{
StringBuilder response = new StringBuilder();
int bytes;
byte[] buffer = new byte[1024];
while ( ( bytes = socket.Receive(buffer, 0, 1024, SocketFlags.None) ) > 0 )
{
string translated = System.Text.Encoding.ASCII.GetString(buffer, 0, bytes);
response.Append(response);
}
return response.ToString();
}
public void Dispose()
{
if (socket != null)
{
socket.Close();
socket.Dispose();
}
}
}
}