Skip to content
Snippets Groups Projects
Commit 8cd76518 authored by joalhelk's avatar joalhelk
Browse files

SMTP palvelin

parent b2564627
No related branches found
No related tags found
No related merge requests found
Showing
with 238 additions and 0 deletions
File added
File added

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31205.134
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "STMPServer", "STMPServer\STMPServer.csproj", "{805BB2D5-8AB1-4C71-A554-7DE78B177C43}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{805BB2D5-8AB1-4C71-A554-7DE78B177C43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{805BB2D5-8AB1-4C71-A554-7DE78B177C43}.Debug|Any CPU.Build.0 = Debug|Any CPU
{805BB2D5-8AB1-4C71-A554-7DE78B177C43}.Release|Any CPU.ActiveCfg = Release|Any CPU
{805BB2D5-8AB1-4C71-A554-7DE78B177C43}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D4E7161E-2448-4CF9-B3E1-FC167ED14DBA}
EndGlobalSection
EndGlobal
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace STMPServer
{
class Program
{
static void Main(string[] args)
{
int port = 25000;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Loopback, port);
socket.Bind(iep);
// Kuinka monta asiakasta saa olla
socket.Listen(1);
bool while_on = true;
Console.WriteLine("SMTP Palvelin käynnissä portissa: " + port);
while(while_on)
{
Socket asiakas = socket.Accept();
string ip = ((IPEndPoint)(asiakas.RemoteEndPoint)).Address.ToString();
string asiakas_port = ((IPEndPoint)(asiakas.RemoteEndPoint)).Port.ToString();
Console.WriteLine("Uusi asiakas: {0} portista: {1}", ip, asiakas_port);
// Aloitetaan keskustelu
asiakas.Send(Encoding.UTF8.GetBytes("220 TIES323 Postipalvelin\r\n"));
bool keskustelu = true;
bool data_vipu = false;
while(keskustelu)
{
byte[] buffer = new byte[2048];
asiakas.Receive(buffer);
string asiakas_vastaus = Encoding.UTF8.GetString(buffer);
Console.WriteLine("[{0}:{1}]: {2}",ip,asiakas_port,asiakas_vastaus);
string[] lines = asiakas_vastaus.Split(
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
int tila = 0;
if(data_vipu)
{
tila = ValidoiData(lines, asiakas);
} else
{
tila = ValidoiVastaus(lines[0], asiakas, data_vipu);
}
switch (tila)
{
case 1:
data_vipu = true;
break;
case 2:
asiakas.Close();
keskustelu = false;
break;
default:
data_vipu = false;
break;
}
}
}
Console.ReadKey();
socket.Close();
}
static int ValidoiVastaus(string vastaus, Socket socket, bool data_vipu)
{
int tila = 0;
string msg = "500 Syntax error!";
if(vastaus.Split(' ')[0] == "HELO")
{
string ip = ((IPEndPoint)(socket.RemoteEndPoint)).Address.ToString();
msg = "250 TIES323 Postipalvelin HELO " + ip;
}
else if (vastaus.Split(':')[0] == "MAIL FROM")
{
msg = "250 2.1.0 Sender... " + vastaus.Split(':')[1].Trim() + " you are ok.";
}
else if (vastaus.Split(':')[0] == "RCPT TO")
{
msg = "250 2.1.5 Recipient... " + vastaus.Split(':')[1].Trim() + " is ok!";
}
else if (vastaus == "DATA")
{
msg = "354 Enter mail, end with \".\" n a line by itself";
tila = 1;
}
else if (vastaus == "QUIT")
{
msg = "221 2.0.0 TIES323 postipalvelin sulkee yhteyden...";
tila = 2;
}
VastaaAsiakas(msg, socket);
return tila;
}
static int ValidoiData(string[] arr, Socket socket)
{
string[] data;
for(int i = 0; i < arr.Length - 1; i++)
{
if(arr[i] == ".")
{
string msg = "250 2.0.0 Message displayed on Server Screen.";
VastaaAsiakas(msg, socket);
return 0;
}
}
return 1;
}
static void VastaaAsiakas(string msg, Socket asiakas)
{
byte[] respond = Encoding.UTF8.GetBytes(msg + "\r\n");
asiakas.Send(respond);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v3.1",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v3.1": {
"STMPServer/1.0.0": {
"runtime": {
"STMPServer.dll": {}
}
}
}
},
"libraries": {
"STMPServer/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
\ No newline at end of file
File added
File added
File added
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\Joppe\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\Joppe\\.nuget\\packages"
]
}
}
\ No newline at end of file
{
"runtimeOptions": {
"tfm": "netcoreapp3.1",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "3.1.0"
}
}
}
\ No newline at end of file
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v3.1", FrameworkDisplayName = "")]
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("STMPServer")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("STMPServer")]
[assembly: System.Reflection.AssemblyTitleAttribute("STMPServer")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.
288f5e67a3f23e091d34108d491ff5e0fbb95d09
File added
1f9858c49b57c0e519074dd288db3e15bdd662aa
E:\Kurssit\ties323\STMPServer\STMPServer\bin\Debug\netcoreapp3.1\STMPServer.exe
E:\Kurssit\ties323\STMPServer\STMPServer\bin\Debug\netcoreapp3.1\STMPServer.deps.json
E:\Kurssit\ties323\STMPServer\STMPServer\bin\Debug\netcoreapp3.1\STMPServer.runtimeconfig.json
E:\Kurssit\ties323\STMPServer\STMPServer\bin\Debug\netcoreapp3.1\STMPServer.runtimeconfig.dev.json
E:\Kurssit\ties323\STMPServer\STMPServer\bin\Debug\netcoreapp3.1\STMPServer.dll
E:\Kurssit\ties323\STMPServer\STMPServer\bin\Debug\netcoreapp3.1\STMPServer.pdb
E:\Kurssit\ties323\STMPServer\STMPServer\obj\Debug\netcoreapp3.1\STMPServer.csprojAssemblyReference.cache
E:\Kurssit\ties323\STMPServer\STMPServer\obj\Debug\netcoreapp3.1\STMPServer.AssemblyInfoInputs.cache
E:\Kurssit\ties323\STMPServer\STMPServer\obj\Debug\netcoreapp3.1\STMPServer.AssemblyInfo.cs
E:\Kurssit\ties323\STMPServer\STMPServer\obj\Debug\netcoreapp3.1\STMPServer.csproj.CoreCompileInputs.cache
E:\Kurssit\ties323\STMPServer\STMPServer\obj\Debug\netcoreapp3.1\STMPServer.dll
E:\Kurssit\ties323\STMPServer\STMPServer\obj\Debug\netcoreapp3.1\STMPServer.pdb
E:\Kurssit\ties323\STMPServer\STMPServer\obj\Debug\netcoreapp3.1\STMPServer.genruntimeconfig.cache
File added
File added
a2240db99a593c701e15ec395af3a989692b6803
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment