using System ;
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Data ;
using System.Drawing ;
using System.Linq ;
using System.Text ;
using System.Windows.Forms ;
using System.IO ;
using System.Net.Sockets ;
using System.Threading ;
using System.Net ;
using System.Web ;
namespace RawMail
{
public partial class Form1 : Form
{
public static string SmtpServer = " mail.domain.com " ;
public Form1 ()
{
InitializeComponent () ;
}
private void Form1_Load ( object sender , EventArgs e )
{
}
private void button1_Click ( object sender , EventArgs e )
{
scrlog . Clear () ;
bool _sonuc = Send ( alici . Text , icerik . Text ) ;
if ( _sonuc == false )
{
sondurum . Text = openFileDialog1 . FileName + " Gönderim Hatası. " ;
}
else
{
sondurum . Text = openFileDialog1 . FileName + " Gönderildi " ;
alici . Text = "" ;
icerik . Text = "" ;
scrlog . Text = "" ;
}
}
private void Form1_FormClosing ( object sender , FormClosingEventArgs e )
{
}
private enum SMTPResponse : int
{
CONNECT_SUCCESS = 220 ,
GENERIC_SUCCESS = 250 ,
DATA_SUCCESS = 354 ,
QUIT_SUCCESS = 221
}
private void LogTut ( string _bilgim )
{
string _metnim = DateTime . Now . Hour + " : " + DateTime . Now . Minute + " : " + DateTime . Now . Second +
" -> " + _bilgim + " \r\n " ;
scrlog . Text = scrlog . Text + _metnim ;
scrlog . SelectionStart = scrlog . Text . Length ;
scrlog . ScrollToCaret () ;
Application . DoEvents () ;
}
private bool Send ( string _kime , string _govde )
{
IPHostEntry IPhst = Dns . Resolve ( SmtpServer ) ;
IPEndPoint endPt = new IPEndPoint ( IPhst . AddressList [ 0 ], 25 ) ;
Socket s = new Socket ( endPt . AddressFamily , SocketType . Stream , ProtocolType . Tcp ) ;
s . Connect ( endPt ) ;
if ( ! Check_Response ( s , SMTPResponse . CONNECT_SUCCESS ))
{
s . Close () ;
return false;
}
Senddata ( s , string . Format ( " HELO {0} \r\n " , Dns . GetHostName ())) ;
if ( ! Check_Response ( s , SMTPResponse . GENERIC_SUCCESS ))
{
s . Close () ;
return false;
}
Senddata ( s , string . Format ( " MAIL From: {0} \r\n " , _kime )) ;
if ( ! Check_Response ( s , SMTPResponse . GENERIC_SUCCESS ))
{
s . Close () ;
return false;
}
string _To = _kime ;
string [] Tos = _To . Split ( new char [] { ' ; ' }) ;
foreach ( string To in Tos )
{
Senddata ( s , string . Format ( " RCPT TO: {0} \r\n " , To )) ;
if ( ! Check_Response ( s , SMTPResponse . GENERIC_SUCCESS ))
{
s . Close () ;
return false;
}
}
Senddata ( s , ( " DATA \r\n " )) ;
if ( ! Check_Response ( s , SMTPResponse . DATA_SUCCESS ))
{
s . Close () ;
return false;
}
StringBuilder Header = new StringBuilder () ;
Header . Append ( _govde ) ;
Header . Append ( " \r\n " ) ;
Header . Append ( " . \r\n " ) ;
Senddata ( s , Header . ToString ()) ;
if ( ! Check_Response ( s , SMTPResponse . GENERIC_SUCCESS ))
{
s . Close () ;
return false;
}
Senddata ( s , " quit \r\n " ) ;
Check_Response ( s , SMTPResponse . QUIT_SUCCESS ) ;
s . Close () ;
return true;
}
private void Senddata ( Socket s , string msg )
{
byte [] _msg = Encoding . ASCII . GetBytes ( msg ) ;
s . Send ( _msg , 0 , _msg . Length , SocketFlags . None ) ;
LogTut ( msg ) ;
}
private bool Check_Response ( Socket s , SMTPResponse response_expected )
{
string sResponse ;
int response ;
byte [] bytes = new byte [ 1024 ] ;
while ( s . Available == 0 )
{
System . Threading . Thread . Sleep ( 100 ) ;
}
s . Receive ( bytes , 0 , s . Available , SocketFlags . None ) ;
sResponse = Encoding . ASCII . GetString ( bytes ) ;
response = Convert . ToInt32 ( sResponse . Substring ( 0 , 3 )) ;
LogTut ( sResponse ) ;
if ( response != ( int ) response_expected )
return false;
return true;
}
private void button2_Click ( object sender , EventArgs e )
{
try
{
openFileDialog1 . ShowDialog () ;
string _dosyaYOL = openFileDialog1 . FileName ;
icerik . Text = File . ReadAllText ( _dosyaYOL ). ToString () ;
int _toYERI = 0 ;
int _dene = 0 ;
int _baslamaYERI = 0 ;
bool _yerBULUNDU = false;
while ( _yerBULUNDU == false )
{
_dene = icerik . Text . IndexOf ( " To: " , _baslamaYERI ) + 3 ;
if ( icerik . Text . Substring ( _dene - 4 , 5 ). Contains ( " -To " ))
{
_yerBULUNDU = false;
_baslamaYERI = _dene ;
}
else
{
_yerBULUNDU = true;
_toYERI = _dene ;
}
}
int _bitisYERI = icerik . Text . IndexOf ( " com " , _toYERI ) + 3 ;
int _alinacak = _bitisYERI - _toYERI ;
alici . Text = icerik . Text . Substring ( _toYERI , _alinacak ). Replace ( " < " , "" ). Replace ( " > " , "" ). Replace ( " " , "" ) ;
int _toplamBOYUT = Convert . ToInt32 ( icerik . Text . Length ) ;
iletiboyutu . Text = ( _toplamBOYUT / 1000 ). ToString ( " n0 " ) + " KByte " ;
}
catch
{
MessageBox . Show ( " İleti okunamadı yada 8bit,base64 gibi bir yöntemle kodlanmış olabilir. " ) ;
}
}
private void progressBar1_CursorChanged ( object sender , EventArgs e )
{
Application . DoEvents () ;
}
}
}
< a href = " http://www.codelama.com/media/wpics/2010/11/Logo_C_Sharp.png " >< img class = " alignnone size-full wp-image-293 " title = " Logo_C_Sharp " src = " http://www.codelama.com/media/wpics/2010/11/Logo_C_Sharp.png " alt = "" width = " 256 " height = " 256 " ></ a >