Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace Lab_SMTP
- {
- public class SMTPClient
- {
- private bool _authRequest = false;
- private Socket _socket;
- public bool AuthRequest => _authRequest;
- public async Task<bool> Connect(string serverDomain)
- {
- Socket s = GetSocket(serverDomain, 25);
- if (s == null) return false;
- _socket = s;
- string hsResponse = await SendRequestWithResponse(_socket, "EHLO yandex.ru \r\n");
- _authRequest = hsResponse.Contains("AUTH LOGIN");
- return hsResponse.Contains("250");
- }
- public async Task<bool> SendAuth(string username, string password)
- {
- if (_socket == null || !_socket.Connected) return false;
- string response = await SendRequestWithResponse(_socket, "AUTH LOGIN\r\n");
- if (!response.Contains("334")) return false;
- string encodedUsername = Convert.ToBase64String(Encoding.UTF8.GetBytes(username));
- string encodedPassword = Convert.ToBase64String(Encoding.UTF8.GetBytes(password));
- response = await SendRequestWithResponse(_socket, $"{encodedUsername}\r\n");
- if (!response.Contains("334")) return false;
- response = await SendRequestWithResponse(_socket, $"{encodedPassword}\r\n");
- return response.Contains("235");
- }
- public async Task<bool> SendFrom(string from) {
- if (_socket == null || !_socket.Connected) return false;
- string response = await SendRequestWithResponse(_socket, $"MAIL FROM:<{from}>\r\n");
- return response.Contains("250");
- }
- public async Task<bool> SendTo(string to) {
- if (_socket == null || !_socket.Connected) return false;
- string response = await SendRequestWithResponse(_socket, $"RCPT TO:<{to}>\r\n");
- return response.Contains("250");
- }
- public async Task<bool> SendData(string data) {
- if (_socket == null || !_socket.Connected) return false;
- string response = await SendRequestWithResponse(_socket, "DATA \r\n");
- if (!response.Contains("354")) return false;
- StringReader reader = new StringReader(data);
- while (true)
- {
- string str = reader.ReadLine();
- if (str == null) break;
- await SendRequest(_socket, $"{str}\r\n");
- }
- response = await SendRequestWithResponse(_socket, ".\r\n");
- return response.Contains("250");
- }
- public async Task<bool> SendQuit(string to) {
- if (_socket == null || !_socket.Connected) return false;
- string response = await SendRequestWithResponse(_socket, $"QUIT\r\n");
- return response.Contains("250");
- }
- public static Socket GetSocket(string host, int port) {
- Socket s = null;
- var tempSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try {
- tempSocket.Connect(host, port);
- }
- catch (SocketException ex) {
- return null;
- }
- if (tempSocket.Connected)
- s = tempSocket;
- return s;
- }
- public static Task<int> ReceiveAsync(Socket socket, byte[] buffer, int offset, int size, SocketFlags socketFlags) {
- var tcs = new TaskCompletionSource<int>(socket);
- socket.BeginReceive(buffer, offset, size, socketFlags, iar =>
- {
- var t = (TaskCompletionSource<int>)iar.AsyncState;
- var s = (Socket)t.Task.AsyncState;
- try { t.TrySetResult(s.EndReceive(iar)); }
- catch (Exception exc) { t.TrySetException(exc); }
- }, tcs);
- return tcs.Task;
- }
- public static async Task SendRequest(Socket socket, string request) {
- Byte[] bytesSent = Encoding.Default.GetBytes(request);
- socket.Send(bytesSent, bytesSent.Length, 0);
- await Task.Delay(100);
- }
- public static async Task<string> SendRequestWithResponse(Socket socket, string request) {
- return await SendRequestWithResponse(socket, socket, request);
- }
- public static async Task<string> SendRequestWithResponse(Socket socketToSend, Socket socketToRecieve, string request) {
- Byte[] bytesReceived = new Byte[256];
- int bytes;
- string response = "";
- await SendRequest(socketToSend, request);
- do {
- bytes = await ReceiveAsync(socketToRecieve, bytesReceived, 0, bytesReceived.Length, 0);
- response = response + Encoding.Default.GetString(bytesReceived, 0, bytes);
- if (bytes < 255) break;
- }
- while (bytes > 0);
- return response;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement