Izgleda da si dovoljno naucio ali evo da postujem i ovaj source kojeg sam nasao u mojoj "staroj" arhivi ali nisam imao vremena da ga postujem (nadam se da ne smeta :)).
Code:
using System.Net.Sockets;
using System.Collections;
using System.IO;
using System.Net;
using System;
class SMTPMailer
{
string SMTPServer;
string host;
public SMTPMailer(string server, string _host){
SMTPServer = server;
host = _host;
}
public void Send(Message msg)
{
TCPClient sender = new TCPClient(SMTPServer,25);
Byte[] outbytes;
string input;
try{
NetworkStream ns = sender.GetStream();
StreamReader sr = new StreamReader(sender.GetStream() );
Console.WriteLine(sr.ReadLine() );
input = "HELO " + host + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
Console.WriteLine(sr.ReadLine() );
input = "MAIL FROM: " +"<" + msg.From + ">" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
Console.WriteLine(sr.ReadLine() );
input = "RCPT TO: " + "<" + msg.To + ">" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
Console.WriteLine(sr.ReadLine() );
input = "DATA" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
Console.WriteLine(sr.ReadLine() );
input ="Subject: " + msg.Subject + "\r\n" + msg.Body + "\r\n" + "." + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
Console.WriteLine(sr.ReadLine() );
input ="QUIT" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
Console.WriteLine(sr.ReadLine() );
sr.Close();
ns.Close();
}
catch(InvalidOperationException ioe){
Console.WriteLine("Could not connect to mail server");
}
}
public static void Main()
{
SMTPMailer mlr = new SMTPMailer("smtp-server", "main");
Message msg = new Message("
[email protected]","
[email protected]",
"Hello!", "This is a test...");
mlr.Send(msg);
Console.ReadLine();
}
}
class Message
{
string to;
string from;
string subject;
string body;
public Message(){
}
public Message(string _to, string _from, string _subject, string _body){
to = _to;
from = _from;
body = _body;
subject = _subject;
}
public string To {
get {return to;}
set {to = value;}
}
public string From {
get {return from;}
set {from = value;}
}
public string Body {
get {return body;}
set {body = value;}
}
public string Subject {
get {return subject;}
set {subject = value;}
}
}