473,241 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 2214
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Google Mike | last post by:
Using Sockets (fsockopen, fputs, fgets, etc.) I'm able to interact with an SMTP server to send mail. However, do you know the steps necessary in order to send a file attachment as well? (Note,...
1
by: Tim Black | last post by:
My application requires sending a large piece (~2MB) of data to several devices on a network via TCP sockets. I have experimented with different methods for doing this and this has raised some...
6
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
4
by: WAkthar | last post by:
I am in the process of converting an MFC client and server application to C#. The communication between the client and server was done using simple WM_COPYDATA. I want to use sockets for the C#...
4
by: TomHL | last post by:
Hello, I want to send an additional info with byte array wiith sockets as "one packet". I know how to send the byte array by himself, but how can I send the additional info with it at the same...
9
by: Gita George | last post by:
I'm trying to write a program (a pop3 mail checker) and I'm having a problem. I'm using the socket control and I've noticed that I do not have any events !? How do I use the socket to receive...
7
by: Steven | last post by:
Hi, Thanks all for you help with my "socket and buffer size" question. I have decided to use async ones, and my code is working pretty well. But i still have a non-answered question: Is it...
7
by: D. Patrick | last post by:
I need to duplicate the functionality of a java applet, and how it connects to a remote server. But, I don't have the protocol information or the java source code which was written years ago. ...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

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.