Experience with using C # for SMTP protocol client development--reading server answer posts

Source: Internet
Author: User
Tags count readline response code
smtp| Server | client | Experience with the TcpClient connection, for example, first get the data stream sent back by the server.

NetworkStream Streamaccount=tcpclient.getstream ();

When we send a request to an SMTP server, such as a connection, a user name, a password, the server returns an answer stream.

We have to read the server back to the data stream, which I have undergone 3 changes.

The first program is in accordance with the Visaul C #. NET network core programming, the example in this book is written:

private string Readfromnetstream (ref NetworkStream NetStream)
{
Byte[] By=new byte[512];
Netstream.read (By,0,by. Length);
String read=system.text.encoding.ascii.getstring (by);
return read;
}

In fact, this way is to read the data stream all into a string form, but the actual network transmission, the SMTP server is not all the return of the valid command, the command is in <CRLF> (carriage return add line) end. Therefore, there is a problem with this way of reading.

The following code is modified:

private string Readfromnetstream (ref NetworkStream netstream,string Strendflag)
{
String resultdata = "";
Byte[] Ubbuff=new byte[1024];
Try
{
while (true)
{
int ncount = Netstream.read (ubbuff,0,ubbuff.length);
if (ncount > 0)
{
Resultdata + + Encoding.ASCII.GetString (ubbuff, 0, ncount);
}

if (Resultdata.endswith (Strendflag))
{
Break
}

if (ncount = 0)
{
throw new System.Exception ("timeout");
}
}
}
catch (System.Exception se)
{
Throw SE;
MessageBox.Show (SE. ToString ());
Return "";
}
return resultdata;
}

This allows you to intercept a command that ends with a carriage return. However, this is not correct, because the SMTP server in some cases will send back some of the welcome information and so on, they are also in the <CRLF> (carriage return add line) ended, we use the above program is probably not what we actually want to get the correct command.

So I only have to revise the procedure as follows:

/**
*
* Read the server's return stream
*
*/
public string Readfromnetstream (ref NetworkStream NetStream)
{

if (NetStream = null)
Return "";

String resultdata = "";
Try
{
while (true)
{
String linedata = Readlinestr (NetStream);
if (null!= linedata
&& linedata.length > 4)
{
if (linedata.substring (3,1). CompareTo ("")!= 0)
{
Resultdata + = Linedata + "\ r \ n";
Continue
}
Else
{
Resultdata + = Linedata;
Break
}
}
Else
{
Resultdata + = Linedata;
Break
}
}
}
catch (Exception e)
{
Throw e;
}

return resultdata;
}

/**
*
* Read the server's return information on a line-by-row basis
*
*/
Public byte[] ReadLine (NetworkStream m_strmsource)
{
ArrayList linebuf = new ArrayList ();
byte prevbyte = 0;

int currbyteint = M_strmsource.readbyte ();
while (Currbyteint >-1)
{
Linebuf.add ((byte) currbyteint);

if ((Prevbyte = (byte) ' \ R ' && (byte) Currbyteint = = (byte) ')
{
byte[] RetVal = new Byte[linebuf.count-2];
Linebuf.copyto (0,retval,0,linebuf.count-2);

return retVal;
}

Prevbyte = (byte) currbyteint;

Currbyteint = M_strmsource.readbyte ();
}


if (Linebuf.count > 0)
{
byte[] RetVal = new Byte[linebuf.count];
Linebuf.copyto (0,retval,0,linebuf.count);

return retVal;
}

return null;
}

/**
*
* Convert server's return information line to string
*
*/
public string Readlinestr (NetworkStream mystream)
{
Byte[] B = readLine (MyStream);
if (null = = b)
{
return null;
}
Else
{
return System.Text.Encoding.ASCII.GetString (b);
}
}

As a result, we can read those data streams that start with a 3-bit response code, add a space, and then some send back, ending in the correct command format for carriage return plus line wrapping.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.