471,305 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Sockets and sending mail

Hello,

I'm currently learning c# and have been trying to write a simple program
with sockets.
The problem is i'm trying to send an email with an attachment, (which is the
program itself) by using base64. When i run it it sends the email ok, but
and attchment doesn't work i get an error when i run it. The attchment seems
to be bigger (20k) then the orginal (16k) i've included the source which i
have been using. Can someone tell me what is wrong or point me in the right
direction?

Robert McNally
using System;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.IO;
using System.Text;

class email{
static char [] b64table = new char []
{'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T','U','V','W ','X','Y',
'Z','a','b','c','d','e','f','g','h','i','j','k','l ','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','0 ','1','2',
'3','4','5','6','7','8','9','+','/'};

public static StringBuilder EncodeString(StringBuilder targetstring)
{
StringBuilder stringcode = new StringBuilder();
StringBuilder codedstring = new StringBuilder();
StringBuilder stringtocode = new StringBuilder();
stringtocode = targetstring;
int l = stringtocode.Length;
int padnum = 0;

//reject zero-length strings
if(l==0)
{
throw new Exception("Error : No characters in string");
}

// convert the string into concatenated binary byte string
for(int i=0;i<l;i++)
{
// convert a character into a binary byte string
int charval = stringtocode[i];
if(charval>127)
{
throw new Exception("Error : Non ASCII character");
}

stringcode = stringcode.Append(dToBs(charval, 8));
}

// figure out how much padding is required
int k = l;
while((k%3)!=0)
{
padnum++;
k++;
}

// insert zeroes at the end of stringcode if required
// to make the string length a multiple of 6 bits
if(padnum!=0)
{
if(padnum==1)
{
stringcode = stringcode.Append("00");
}
else if(padnum==2)
{
stringcode = stringcode.Append("0000");
}
}

int stringcodelength = stringcode.Length;// length of binary string
int cursor = 0;
int maxlength = 0;
StringBuilder bitstring = new StringBuilder();
while(cursor<stringcodelength)
{
// find decimal value of each 6bit group
int value = bsToD(bitstring.Append(stringcode.ToString(cursor, 6)));

cursor = cursor+6;

// the calculated value can't be greater than 63
if(value>63)
{
throw new Exception("Coding error");
}

// update codedstring -- insert crlf after every 76 characters
if(maxlength==76)
{
codedstring = codedstring.Append("\r\n");
maxlength = 0;
}

codedstring = codedstring.Append(b64table[value]);
maxlength++;
value = 0;
bitstring.Remove(0,6);
}

// add required padding
if(padnum==1)
{
codedstring = codedstring.Append("=");
}
else if(padnum==2)
{
codedstring = codedstring.Append("==");
}

return codedstring;
}

public static StringBuilder dToBs (int decnum, int charnum)
{
if (decnum >= (int)Math.Pow (2, charnum) || decnum < 0) {}

StringBuilder bitstring = new StringBuilder();

for (int i = charnum - 1; i > -1; i--)
{
int powerof2 = (int)Math.Pow(2, i);

if (decnum >= powerof2)
{
bitstring = bitstring.Append('1');
decnum = decnum - powerof2;
}
else
{
bitstring = bitstring.Append('0');
}
}

return bitstring;
}

public static int bsToD(StringBuilder bitstring)
{
int stringlength = bitstring.Length;
int cursor = 0;
int value = 0;

for (int i = stringlength - 1; i > -1; i--)
{
if (bitstring[cursor] == '1')
{
value = value + (int)Math.Pow(2, i);
}
cursor++;
}
return value;
}

static void Main (string[] args)
{
TcpClient Smtp = new TcpClient("post.demon.co.uk", 25);
string Data;
byte[] szData;
string CRLF = "\r\n";
string boundary = "BMH.Boundary.02207198300";

Module mod = Assembly.GetExecutingAssembly().GetModules()[0];

string inFileName = mod.FullyQualifiedName, b64data ;
FileStream inFile;
byte[] bs;

inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
bs = new byte [inFile.Length];
int readBytes = inFile.Read (bs, 0, (int) inFile.Length);
inFile.Close();

b64data = Encoding.ASCII.GetString(bs);

try
{
NetworkStream NetStrm = Smtp.GetStream();
StreamReader RdStrm = new StreamReader(Smtp.GetStream());
Console.WriteLine(RdStrm.ReadLine());

Data = "HELO demon.co.uk" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "MAIL FROM: " + "<ro****@demon.co.uk>" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "RCPT TO: " + "<ro****@demon.co.uk>" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "DATA" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());
Console.WriteLine(":: Done");

Data = "Date: Tue, 22 Nov 2005 21:00:00 GMT" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "From: ro****@demon.co.uk" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "To: robert" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Subject: MIME Test" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Transfer-Encoding: 7bit" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Type: multipart/mixed; boundary=\"" + boundary + "\"" +
CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "MIME-Version: 1.0" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "X-Mailer: Microsoft Outlook Express 6.00.2800.1106" + CRLF +
CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Test message" + CRLF + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "--" + boundary + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Transfer-Encoding: base64" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Type: application/octet-stream; name=\"test.exe\"" + CRLF
+ CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = email.EncodeString(new StringBuilder(b64data)).ToString();
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = CRLF + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);

Data = "--" + boundary + "--" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);

Data = "." + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "QUIT" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());
Console.WriteLine("All Finished");
Console.ReadLine();

NetStrm.Close();
RdStrm.Close();
}
catch (InvalidOperationException err)
{
Console.WriteLine(err.ToString());
}
Console.ReadLine();
}
}
Dec 6 '05 #1
4 2112
Couple observations:
1) Anytime you attempt to email an attachment that consists of a running
program executable, you are gonna run into issues (file being locked as it is
in memory, etc).
2) I understand you are trying to learn c# which is great, but why not just
use the built-in System.Web.Mail (or in 2.0, System.Net.Mail) class?
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Robert McNally" wrote:
Hello,

I'm currently learning c# and have been trying to write a simple program
with sockets.
The problem is i'm trying to send an email with an attachment, (which is the
program itself) by using base64. When i run it it sends the email ok, but
and attchment doesn't work i get an error when i run it. The attchment seems
to be bigger (20k) then the orginal (16k) i've included the source which i
have been using. Can someone tell me what is wrong or point me in the right
direction?

Robert McNally
using System;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.IO;
using System.Text;

class email{
static char [] b64table = new char []
{'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T','U','V','W ','X','Y',
'Z','a','b','c','d','e','f','g','h','i','j','k','l ','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','0 ','1','2',
'3','4','5','6','7','8','9','+','/'};

public static StringBuilder EncodeString(StringBuilder targetstring)
{
StringBuilder stringcode = new StringBuilder();
StringBuilder codedstring = new StringBuilder();
StringBuilder stringtocode = new StringBuilder();
stringtocode = targetstring;
int l = stringtocode.Length;
int padnum = 0;

//reject zero-length strings
if(l==0)
{
throw new Exception("Error : No characters in string");
}

// convert the string into concatenated binary byte string
for(int i=0;i<l;i++)
{
// convert a character into a binary byte string
int charval = stringtocode[i];
if(charval>127)
{
throw new Exception("Error : Non ASCII character");
}

stringcode = stringcode.Append(dToBs(charval, 8));
}

// figure out how much padding is required
int k = l;
while((k%3)!=0)
{
padnum++;
k++;
}

// insert zeroes at the end of stringcode if required
// to make the string length a multiple of 6 bits
if(padnum!=0)
{
if(padnum==1)
{
stringcode = stringcode.Append("00");
}
else if(padnum==2)
{
stringcode = stringcode.Append("0000");
}
}

int stringcodelength = stringcode.Length;// length of binary string
int cursor = 0;
int maxlength = 0;
StringBuilder bitstring = new StringBuilder();
while(cursor<stringcodelength)
{
// find decimal value of each 6bit group
int value = bsToD(bitstring.Append(stringcode.ToString(cursor, 6)));

cursor = cursor+6;

// the calculated value can't be greater than 63
if(value>63)
{
throw new Exception("Coding error");
}

// update codedstring -- insert crlf after every 76 characters
if(maxlength==76)
{
codedstring = codedstring.Append("\r\n");
maxlength = 0;
}

codedstring = codedstring.Append(b64table[value]);
maxlength++;
value = 0;
bitstring.Remove(0,6);
}

// add required padding
if(padnum==1)
{
codedstring = codedstring.Append("=");
}
else if(padnum==2)
{
codedstring = codedstring.Append("==");
}

return codedstring;
}

public static StringBuilder dToBs (int decnum, int charnum)
{
if (decnum >= (int)Math.Pow (2, charnum) || decnum < 0) {}

StringBuilder bitstring = new StringBuilder();

for (int i = charnum - 1; i > -1; i--)
{
int powerof2 = (int)Math.Pow(2, i);

if (decnum >= powerof2)
{
bitstring = bitstring.Append('1');
decnum = decnum - powerof2;
}
else
{
bitstring = bitstring.Append('0');
}
}

return bitstring;
}

public static int bsToD(StringBuilder bitstring)
{
int stringlength = bitstring.Length;
int cursor = 0;
int value = 0;

for (int i = stringlength - 1; i > -1; i--)
{
if (bitstring[cursor] == '1')
{
value = value + (int)Math.Pow(2, i);
}
cursor++;
}
return value;
}

static void Main (string[] args)
{
TcpClient Smtp = new TcpClient("post.demon.co.uk", 25);
string Data;
byte[] szData;
string CRLF = "\r\n";
string boundary = "BMH.Boundary.02207198300";

Module mod = Assembly.GetExecutingAssembly().GetModules()[0];

string inFileName = mod.FullyQualifiedName, b64data ;
FileStream inFile;
byte[] bs;

inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
bs = new byte [inFile.Length];
int readBytes = inFile.Read (bs, 0, (int) inFile.Length);
inFile.Close();

b64data = Encoding.ASCII.GetString(bs);

try
{
NetworkStream NetStrm = Smtp.GetStream();
StreamReader RdStrm = new StreamReader(Smtp.GetStream());
Console.WriteLine(RdStrm.ReadLine());

Data = "HELO demon.co.uk" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "MAIL FROM: " + "<ro****@demon.co.uk>" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "RCPT TO: " + "<ro****@demon.co.uk>" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "DATA" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());
Console.WriteLine(":: Done");

Data = "Date: Tue, 22 Nov 2005 21:00:00 GMT" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "From: ro****@demon.co.uk" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "To: robert" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Subject: MIME Test" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Transfer-Encoding: 7bit" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Type: multipart/mixed; boundary=\"" + boundary + "\"" +
CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "MIME-Version: 1.0" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "X-Mailer: Microsoft Outlook Express 6.00.2800.1106" + CRLF +
CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Test message" + CRLF + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "--" + boundary + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Transfer-Encoding: base64" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Type: application/octet-stream; name=\"test.exe\"" + CRLF
+ CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = email.EncodeString(new StringBuilder(b64data)).ToString();
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = CRLF + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);

Data = "--" + boundary + "--" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);

Data = "." + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "QUIT" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());
Console.WriteLine("All Finished");
Console.ReadLine();

NetStrm.Close();

Dec 6 '05 #2
Size-wise, you could have mistaken between the actually file "size" and
"size on disk".
Actual bytes used on your harddrive(size on disk) is(in my case, after
copying and pasting your source on a filee), it was 12kb and the "size"
was "9k" When I attached it to email, it was displaying "9k".

About the program not running, are you sending your source as an
attachment to a Linux-based computers to run the program through Mono?
If that is the case, there could have been some problem with newline
characters since Windows uses two characters(\r\n) for a newline while
*NIX based OS's use "\n" only.

Dec 6 '05 #3
Few things to make your life easyer:

Converting ASCII string to Base64:

public string Ascii2B64(string ASCIIString)
{
return
System.Convert.ToBase64String(System.Text.ASCIIEnc oding.ASCII.GetBytes(ASCIIString));
}

Same can be done for your binary attachments....
Sending & recieving data made easy (since all data is sent/recieved
synchonous):

private string SendData(NetworkStream ns, string data)
{
szData = Encoding.ASCII.GetBytes(data + "\r\n");
ns.Write(szData, 0, szData.Length);
return RdStrm.ReadLine();
}
Not the awnsers you asked for, but at least helpfull :)

Also, i am not sure that ' Module mod =
Assembly.GetExecutingAssembly().GetModules()[0];' will allow you get
get a 100% copy of the current running program, for example: any
resources will not be included in this command... so if i was you, i'd
look into that some more.

Dec 6 '05 #4
Why can't you use SmtpMail instead?

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Robert McNally" <ro*******@gmx.net> wrote in message
news:dn*******************@news.demon.co.uk...
Hello,

I'm currently learning c# and have been trying to write a simple program
with sockets.
The problem is i'm trying to send an email with an attachment, (which is
the program itself) by using base64. When i run it it sends the email ok,
but and attchment doesn't work i get an error when i run it. The attchment
seems to be bigger (20k) then the orginal (16k) i've included the source
which i have been using. Can someone tell me what is wrong or point me in
the right direction?

Robert McNally
using System;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.IO;
using System.Text;

class email{
static char [] b64table = new char []
{'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T','U','V','W ','X','Y',
'Z','a','b','c','d','e','f','g','h','i','j','k','l ','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','0 ','1','2',
'3','4','5','6','7','8','9','+','/'};

public static StringBuilder EncodeString(StringBuilder targetstring)
{
StringBuilder stringcode = new StringBuilder();
StringBuilder codedstring = new StringBuilder();
StringBuilder stringtocode = new StringBuilder();
stringtocode = targetstring;
int l = stringtocode.Length;
int padnum = 0;

//reject zero-length strings
if(l==0)
{
throw new Exception("Error : No characters in string");
}

// convert the string into concatenated binary byte string
for(int i=0;i<l;i++)
{
// convert a character into a binary byte string
int charval = stringtocode[i];
if(charval>127)
{
throw new Exception("Error : Non ASCII character");
}

stringcode = stringcode.Append(dToBs(charval, 8));
}

// figure out how much padding is required
int k = l;
while((k%3)!=0)
{
padnum++;
k++;
}

// insert zeroes at the end of stringcode if required
// to make the string length a multiple of 6 bits
if(padnum!=0)
{
if(padnum==1)
{
stringcode = stringcode.Append("00");
}
else if(padnum==2)
{
stringcode = stringcode.Append("0000");
}
}

int stringcodelength = stringcode.Length;// length of binary string
int cursor = 0;
int maxlength = 0;
StringBuilder bitstring = new StringBuilder();
while(cursor<stringcodelength)
{
// find decimal value of each 6bit group
int value = bsToD(bitstring.Append(stringcode.ToString(cursor, 6)));

cursor = cursor+6;

// the calculated value can't be greater than 63
if(value>63)
{
throw new Exception("Coding error");
}

// update codedstring -- insert crlf after every 76 characters
if(maxlength==76)
{
codedstring = codedstring.Append("\r\n");
maxlength = 0;
}

codedstring = codedstring.Append(b64table[value]);
maxlength++;
value = 0;
bitstring.Remove(0,6);
}

// add required padding
if(padnum==1)
{
codedstring = codedstring.Append("=");
}
else if(padnum==2)
{
codedstring = codedstring.Append("==");
}

return codedstring;
}

public static StringBuilder dToBs (int decnum, int charnum)
{
if (decnum >= (int)Math.Pow (2, charnum) || decnum < 0) {}

StringBuilder bitstring = new StringBuilder();

for (int i = charnum - 1; i > -1; i--)
{
int powerof2 = (int)Math.Pow(2, i);

if (decnum >= powerof2)
{
bitstring = bitstring.Append('1');
decnum = decnum - powerof2;
}
else
{
bitstring = bitstring.Append('0');
}
}

return bitstring;
}

public static int bsToD(StringBuilder bitstring)
{
int stringlength = bitstring.Length;
int cursor = 0;
int value = 0;

for (int i = stringlength - 1; i > -1; i--)
{
if (bitstring[cursor] == '1')
{
value = value + (int)Math.Pow(2, i);
}
cursor++;
}
return value;
}

static void Main (string[] args)
{
TcpClient Smtp = new TcpClient("post.demon.co.uk", 25);
string Data;
byte[] szData;
string CRLF = "\r\n";
string boundary = "BMH.Boundary.02207198300";

Module mod = Assembly.GetExecutingAssembly().GetModules()[0];

string inFileName = mod.FullyQualifiedName, b64data ;
FileStream inFile;
byte[] bs;

inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
bs = new byte [inFile.Length];
int readBytes = inFile.Read (bs, 0, (int) inFile.Length);
inFile.Close();

b64data = Encoding.ASCII.GetString(bs);

try
{
NetworkStream NetStrm = Smtp.GetStream();
StreamReader RdStrm = new StreamReader(Smtp.GetStream());
Console.WriteLine(RdStrm.ReadLine());

Data = "HELO demon.co.uk" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "MAIL FROM: " + "<ro****@demon.co.uk>" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "RCPT TO: " + "<ro****@demon.co.uk>" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "DATA" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());
Console.WriteLine(":: Done");

Data = "Date: Tue, 22 Nov 2005 21:00:00 GMT" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "From: ro****@demon.co.uk" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "To: robert" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Subject: MIME Test" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Transfer-Encoding: 7bit" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Type: multipart/mixed; boundary=\"" + boundary + "\"" +
CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "MIME-Version: 1.0" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "X-Mailer: Microsoft Outlook Express 6.00.2800.1106" + CRLF +
CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Test message" + CRLF + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "--" + boundary + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Transfer-Encoding: base64" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = "Content-Type: application/octet-stream; name=\"test.exe\"" +
CRLF + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = email.EncodeString(new StringBuilder(b64data)).ToString();
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(":: Done");

Data = CRLF + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);

Data = "--" + boundary + "--" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);

Data = "." + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "QUIT" + CRLF;
szData = Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0 ,szData.Length);
Console.WriteLine(RdStrm.ReadLine());
Console.WriteLine("All Finished");
Console.ReadLine();

NetStrm.Close();
RdStrm.Close();
}
catch (InvalidOperationException err)
{
Console.WriteLine(err.ToString());
}
Console.ReadLine();
}
}

Dec 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Google Mike | last post: by
6 posts views Thread by Laxmikant Rashinkar | last post: by
4 posts views Thread by WAkthar | last post: by
9 posts views Thread by Gita George | last post: by
7 posts views Thread by Steven | last post: by
7 posts views Thread by D. Patrick | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.