Use C # production "mail express delivery"

xiaoxiao2021-03-06  52

The download address of the source code (go to the page below:

Http://itboy.cn/product/index.asp

First, the preface of the Foxmail new version has a "mail express delivery" function. I don't know how to use it first, and I will know that I am going there.

Tools -> System Options Set the IP address of the local DNS server.

I feel that this new feature is quite easy to use. You don't need to pass the SMTP proxy, you can send mail directly through the mail switch to your mailbox. I want to implement this function in VC at the beginning of the summer vacation. After the iRIS is intercepted, the program is found.

MX8.263.NET sends a mailbox, I don't know what this is something. Later, I thought this is the 263.NET MX record host. The principle of expressive express delivery is to send data to this host.

Run the NSLookup program:

SET TYPE = MX 263.NET

Yes, yes, get the result:

Non-Authoritative Answer: 263.NET MX preference = 10, mail exchanger = mx06.263.net 263.net MX preference = 10, mail exchanger =

MX08.263.NET

263.NET MX preference = 10, mail exchanger = mx09.263.net 263.net mx preference = 10, mail exchanger = mx11.263.net 263.net mx preference = 10, mail exchanger = mx12.263.net 263. Net mx preference = 40, mail exchanger = mx03.263.net 263.net mx preference = 10, mail exchanger = mx01.263.net

Not wrong. This is this. Later, I didn't know how to achieve NSLOOKUP's function, I gave up and learned about half a month of C #. Later, I found some related documents online. A few experiments. Take my development process to share, I wrote the tutorial document. So, if you don't standardize, please bear it. The domain name, mailbox and IP involved in this article are true.

Second, the principle of DNS protocol I think, to be a good network software programmer, you must read the RFC documentation. Because this article is a fans for most broad program enthusiasts, I try to write from the details. If the master, you can skip this part.

Related RFC Documents of the DNS Agreement:

RFC1034- "

Domain names - Concepts and facilities

RFC1035- "

Domain names - Implementation and Specification

The computer is used online as 220.162.75.1, which is called an IP address, is identified by a computer. And if you visit a computer accessed by entering such a stuff, isn't it too terrible? To have a good stuff like DNS, use the IP address you bind it, when we enter inside the browser

When http://zssysy.com, the browser doesn't know where to get it, so you will query to the set DNS server.

Zzsy.com This domain name. The DNS server will look for your own record library, and if you don't find it, turn to the previous DNS server for query (forwarding request). Tell your browser to find the IP. The record type of the browser query is a record. RFC1035 Documentation Defined in page 11 has 16 record types, and

Common A (Address) Record, CNAME (alias) record, MX (Mail Switch) record. We must care about MX records. The process of queries is generally: the customer sends a UDP packet to the 53 port of the DNS server, and the DNS server is processed and processes the results records still returned in the form of UDP packets. The general format of this UDP message:

---------------------

| Packet head |

---------------------

| Question | Query part of the server

---------------------

| Answer | Resource Record of Server Reply

---------------------

| Authorized | Authoritative resource record

---------------------

| Extraordinary | Extraordinary Resource Record

---------------------

In addition to the packet head is fixed 12 bytes, the length of each part is unordered byout.

We care about the news head, the problem, and answer these three parts.

among them

The format of the header:

1 1 1 1 1 1

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5

- - - - - - -

| ID |

- - - - - - -

| Qr | opcode | AA | TC | RD | RA | Z | RCODE |

- - - - - - -

| QDCOUNT |

- - - - - - -

| Ancount |

- - - - - - -

| Nscount |

- - - - - - -

| Arcount |

- - - - - - - - Good Guy, what ghost paint! One of the top is the number identifier, 0-15 (note that the 10-15 behind the back is in the form of up and down, and I didn't understand it at the beginning. The next is: ID: 16 bits, 2 bytes. The number of this message is specified by the client. The DNS reply is taken on this identifier to request a request for the corresponding correspondence. QR: 1 bit, 1/8 bytes. 0 represents the query, 1 represents DNS reply opcode: 4 digits, 1/2 bytes. Indicates the type of query: 0: Standard query; 1: reverse query; 2: server status query; 3-15: Not used. AA: 1 bit, 1/8 bytes. Whether authority reply. Tc: 1 bit, 1/8 bytes. Because a UDP message is 512 bytes, the bit indicates whether the exceeded portion. RD: 1 bit, 1/8 bytes. This bit is specified in the query and the same time is the same. Set to 1 Indicate the server for recursive query. RA: 1 bit, 1/8 bytes. Returns the specified by DNS reply, indicating whether the DNS server supports recursive query. Z: 3 digits, 3/8 bytes. The preserved field must be set to 0. Rcode: 4 digits, 1/2 bytes. The return code specified by the reply: 0: No error; 1: Format error; 2: DNS error; 3: Domain name does not exist; 4: DNS does not support this type of query; 5: DNS refuse query; 6-15: Reserved field . QDCount: 16 bits, 2 bytes. An unsigned number indicating the number of query records. ANCOUNT: 16 bits, 2 bytes. An unsigned number indicates the number of reply records. Nscount: 16 bits, 2 bytes. An unsigned number indicates the number of authoritative records. Arcount: 16 bits, 2 bytes. An unsigned number indicates a number of exception records.

Each query resource record format:

1 1 1 1 1 1

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5

- - - - - - -

| | |

/ QNAME /

/ /

- - - - - - -

| Qtype |

- - - - - - -

| Qclass |

- - - - - - QNAME : Not long, indicating the domain name to query. (Bright boxes in both sides) Qtype: 2 bytes, according to RFC 1035 and NSLOOKUP's help document, I define the following enumeration type: Enum querytype // The resource record type of Query. {A = 0x01, // Specify the computer IP address. NS = 0x02, / / ​​Specifies the DNS name server for the naming area. MD = 0x03, // Specify the mail receiving station (this type is outdated, use MX instead) MF = 0x04, // Specify the mail transfer station (this type is outdated, use MX instead) CNAME = 0x05, // Specify The normative name of the alias. SOA = 0x06, / / ​​Specifies "starting authority" for the DNS area. MB = 0x07, // Specify the mailbox domain name. MG = 0x08, // Specify a member of the mail group. MR = 0x09, // Specify the mail rename domain name. NULL = 0x0a, // Specify an empty resource record WKS = 0x0b, // Describe known service. PTR = 0x0c, // If the query is an IP address, specify the computer name; otherwise specify a pointer to other information. Hinfo = 0x0d, // Specifies the computer CPU and the operating system type. Minfo = 0x0e, // Specify a mailbox or mailing list information. MX = 0x0f, // Specify a mail switch. TXT = 0x10, // Specify text information. UINFO = 0x64, // Specify user information. UID = 0x65, // Specify the user identifier. GID = 0x66, // Specify the group identifier of the group name. Any = 0xFF // Specify all data types. }; Qtype: 2 bytes. Depending on RFC1035 and NSlookup help documents, I define the following enumeration type: Enum queryclass // Specify the protocol group of information. {IN = 0x01, // Specify the Internet Category. Csnet = 0x02, // Specify the CSNet category. (Outtime) Chaos = 0x03, // Specify the chaos category. Hesiod = 0x04, // Specify Mit Athena HesioD category. Any = 0xFF // Specifies any previously listed wildcards. }; A, MX, CNAME in Qtype is common, and IN in QClass is common. Each recording format for each reply:

1 1 1 1 1 1

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5

- - - - - - -

| | |

/ /

/ Name /

| | |

- - - - - - - | TYPE |

- - - - - - -

| Class |

- - - - - - -

| TTL |

| | |

- - - - - - -

| RDLENGTH |

- - - - - - - - - |

/ RDATA /

/ /

- - - - - - -

Name: Reply to the domain name of the query, not long.

TYPE: The type of reply. 2 bytes, synonymous with queries. Indicates the type of resource record in RDATA.

Class: Reply to the class. 2 bytes, synonymous with queries. Indicates the resource record class in RDATA.

TTL: Survival time. 4 bytes, indicating that the resource recorded in RDATA is recorded in the cache.

Rdlength: Length. 2 bytes, indicating the length of the RDATA block.

RDATA: Resource Record. Undefined, depending on Type, this record is different, usually one MX record is composed of a 2-byte indicated a priority value of the mail switch and an unregistered mail exchanger name.

This describes the combination of names.

The name consists of a plurality of identity sequences, and the first byte of each identifier sequence illustrates the length of the identifier, and then uses a ASCII code to represent characters, and multiple sequences are later indicated by byte 0. If the length of the first character of one of the identification sequences is 0xc0, it means that the next byte indication is not an identifier sequence, but instead indicates the offset position in the next portion within the receiving package.

For example, BBS.ZZSY.com. Separate BBS, ZZSY, COM. The length of each part is 3, 4, 3

In the form of the DNS packet, like 3 B B S 4 Z z y 3 C O m 0

If there is a name of 4 Z Z Y 3 C O m 0 in the 12th byte location in the package.

It is possible to: 3 B B S 4 Z z y 0xc0 0x0c.

Third, DNS protocol instance explanation

Said so many theoretical fits, there may be two big heads. Or use an example method to explain it. I chose the famous webcape and protocol analysis tool Iris 4.05, you can download from my site:

Http://itboy.cn/data/iris405full.rar

Run IRIS, click on the Filters selection port tab of the menu to use the 53 port after determination.

Click on the green run icon on the IRIS toolbar for listening.

Run the NSlookUp program in Windows.

Enter the following command:

SET TYPE = MX

Then return to the NSLookUP program.

Enter the command:

Yahoo.com.cn

Get up

Yahoo.com.cn mx preference = 20, mail exchanger = mx5.mail.yahoo.com

Yahoo.com.cn mx preference = 10, mail exchanger = MTA-V1.MAIL.VIP.CNB.YAHOO.com

Such two MX resource records are recorded. At this time, IRIs is as follows:

The current picture shows the package is the second message, which is a message replying from DNS. The first message is a query message, which is much simpler than the reply packet. Therefore, if you analyze the reply packet, the query message believes that smart is as easy as you can make it easy.

The red portion of the figure shown in the figure is a DNS packet, and the data content on the upper side is the MAC Joint, PPP Protocol, IPv4 Protocol Pack and UDP Protocol Pack. These packages are not within the scope of this article, not paying attention.

(Insert some nonsense: Click on the entry displayed in the left tree of IRIS, not necessarily reflected in the packet on the right, I don't know, I don't know at the beginning, and analyze it according to the indication of it, analyze it. For a long time, I don't know what to say. The analysis of the package is to study this program in this program. In the past two, the online class did not listen to the online class of our college, and weird. , 咋 叫 叫 教 教 教 专 专 专 专 专 专 专 专 专 专: 地 地 专: 地Speech is correct

For the convenience of explanation, I handle some of the DNS protocols in the bag, as shown below:

The red portion is the header, blue for the query section, green for the first resource record of the reply, gold is a second resource record for the reply. Others don't have the authority record, extra record, don't analyze it.

Red section:

The 0th byte, the first byte 00 03 identifies an ID.

The second byte, the third word 81 80 is chemically binary form 1000 0001 1000 0000

QR (0) is 1 reply to reply packets.

OpCode (2-5) represents a standard query.

AA (6) indicates a non-authoritative query.

TC (7) is 0 indicates that no more than 512 bytes of packets do not truncate.

RD (8) is 1 indicating that the NSLookUP program indicates that the DNS is recursive.

RA (9) is 1 to represent DNS support recursive query.

Z (10-12) Reserved field

Rcode (13-16) is 0 indicates that the query is not enough.

The 4th byte, the 5th byte 00 01 indicates that the number of resource records in the query is 1

The 6th byte, the 7th byte 00 02 indicates that the number of resource records of the reply is 2

The 8th byte, the 9th byte 00 06 indicates that the number of authoritative resources records is 6

The 10th byte, the 11th byte 00 04 indicates that the number of additional resource records is 4

Then reached the blue part, the resource record section of the query:

The domain name is started at the beginning.

Start 05 The five bytes of the rear are the sequence character, turn the 79 61 68 6f 6f to the ASCII code to arrive to Yahoo arrive at 03 means the three bytes of the rear side are sequence characters, turn the 63 6F 6D to the ASCII code to COM

Arrived 02 indicates the two bytes of the back to the sequence character, turn the 63 6E to the ASCII code to CN

The arrival 00 indicates the end.

The whole strings, use. Connection, ie: yahoo.com.cn

The next two byte 00 0F indicates that the query type is 15, which is the MX record query.

Next, the two bytes 00 01 indicate that the query class is 1, which is connected to the Internet.

The DNS packets in the first package in the IRIS package contain only red lines and blue lines, and the analysis methods are the same. No longer.

Green part, reply the first resource record section:

Open the domain name of the query,

The first byte c0 represents compression, the next bit is the offset bypad.

Next, 0C represents the first byte of the blue of the entire package, that is, the first byte of the blue.

Then the same analysis is the same as the above, the domain name is yahoo.com.cn

Then return to the next byte

The 3-4 bytes 00 0f in the green package indicates that the query type is 15, which is the MX record query.

Then the next 5-6 bytes 00 01 indicate that the query class is 1, which is connected to the Internet.

Then the next 7-10 bytes 00 00 05 95 is evolved as evolution equal to 1429 seconds. That is, the cache time is 23 minutes and 49 seconds.

Then 11-12 byte 00 16 indicates that the data portion recorded in this resource is 22 bytes. That is, the number of bytes left.

22 bytes starting from 13 bytes to the data part of the MX record. This section is formatted to two bytes of mail exchanger priority, unregulated mail switch name.

Section 13-14 byte 00 14 indicates a priority value of 20

Then, from 15, it is a mail switch name, according to the blue domain name analysis method, get mx5.mail.yahoo.com

(The byte this paragraph is the relative position in the green block, not the absolute position of the entire DNS package)

The patronic method of the golden piece is similar to the green, but there is a difference that looks at the mail exclusion name of the golden bag.

From the absolute position of the package, the 78th (the figure in the figure is bidding the purple 78) begins to be the email switch name. We have analyzed, get MTA-V1.MAIL.VIP.CNB, then 0xc0 is then compressed, and then the next offset is 0x0c and jumps to the 12-byte of the package to get Yahoo.com.cn. The whole combination is the second resource record name: MTA-v1.mail.vip.cnb.yahoo.com

Such a result is running the results with NSlookup:

Yahoo.com.cn mx preference = 20, mail exchanger = mx5.mail.yahoo.com

Yahoo.com.cn mx preference = 10, mail exchanger = MTA-V1.MAIL.VIP.CNB.YAHOO.com

the same.

I hope this energy can help network enthusiasts learn the package analysis method. Analysis of programming points of MX record query in our current DNS.

Fourth, DNS Query MX Record Programming Key Point Analysis Foxmail 5.0 Mail Express A very rubbish place is unable to automatically get a local ISP DNS server, but also user manual input, I think it is because the limit of API or Foxmail development group I didn't think of the method or other things that I didn't know. No matter what he, use WMI in C #, it is easy:

String [] DNSES;

ManagementClass MC = New ManagementClass ("Win32_NetworkAdapterConfiguration);

ManagementObjectCollection moc = mc.getinstances ();

// Enumerate all the network cards on the current machine

Foreach (ManagementObject Mo in MoC)

{

IF (Bool) Mo ["iPenabled"])

{

DNSES = (String []) Mo ["DNSSERVERSEARCHORDER"];

IF (DNSES! = NULL)

{

DNSSERVER = DNSES [0]; // uses the first found DNS server.

}

}

}

Of course, you will not forget to add a reference to System.Management in the project -> add reference?

Put this in the static constructor of the DNSQuery class.

There is also a custom MXRecord structure that stores information for MX resource records.

Struct Mxrecord

{

Public String Domain; // Domain Name

Public QueryType QueryType; // Query Type

Public queryclass queryclass; // query class

Public timespan livetime; // Survival time

The length of the PUBLIC INT DATALENGTH; / / The length of the resource is indicative of the number of bytes of the resource of the priority and name of the mail server.

Public int preference; // Priority value, the smaller the value, the more preferred.

Public String Name; // Mail Exchanger Name

}

Make two bytes into an INT16 integer cannot be used by BitConverter class because it is different from the end of the DNS server. Therefore, it is shifted with the << method.

Others don't say, in the DNSQuery class of the program, I made a detailed code annotation. If you have some development experience, it should be easy to understand. If you have any questions, please contact me. I will help you with your energy and ability.

V. Analyze Foxmail's express delivery to send data.

Run IPConfig / ALL

Record the first IP address of the resulting DNS Servers.

2. Or run iris. Set Filters as SMTP, 25 ports. Run to listen.

3. Run Foxmail5.0, "Mail Express" tab within Tools -> System settings Set the domain name server 1 is the just IP address.

Click "Write" and write it with the format of the text file.

Recipient:

DreamChild@263.net

Cc:

theme:

Hello Dreamchild, take the liberty to fight

content:

Dear Mr. Dreamchild:

This is an email.

Attachment: (Add mm.gif and instructions. TXT two files. In order to ensure the integrity of this tutorial, these two additional stuff also put it in the package.)

4. Stop Iris after completion, point the decode of its menu bar to get the following:

220 Welcome To Coremail System (with Anti-spam) 2.1 for 263 (040326)

Helo DreamChild

250 mta6.x263.net

Mail from: <>

250 ok

RCPT TO:

250 ok

Data

354 End Data with

.

Date: Thu, 9 Sep 2004 01:00:35 0800

From: "=? GB2312? B? W87qobqi? =" <

>

TO: "Dreamchild"

? Subject: = gb2312 B xOO6w2RyZWFtY2hpbGSjrMOww8G08sjE = X-mailer: Foxmail 5.0 [cn] Mime-Version: 1.0 Content-Type:??? Multipart / mixed; boundary = "===== 001_Dragon788446150325 _ =====" This is a multi-part message in MIME format - ===== 001_Dragon788446150325 _ ===== Content-Type: text / plain; charset = "gb2312" Content-Transfer-Encoding:. base64 1 / C tLXEZHJlYW1jaGlsZM / Iyfqjug0KoaGhodXiysfSu7fi08q8 / Q Gjdqo = - ===== 001_dragon788446150325 _ ===== CONTENT-TYPE: image / gif; Name = "mm.gif" Content-Transfer-Encoding: Base64 Content-Disposition: attachment; filename = "mm.gif" R0lGODlheAB4AIddABwNCixIjJtjkLO8iZux0NK2xpVJT9XayJWNuDA0Vt5Xh Sw7dLc7p6Fh3mD (side to be omitted on a tourbillon MM.gif Base64 encoded file content) w75UTdHFHwD / 1Ex6cBSs0jHpKaySFXI4XCwAEhPkRsUSBQQAOw == - ===== 001_Dragon788446150325 _ ===== content-Type: application / octet-stream; name = "= gb2312 B y7XD9y50eHQ = =????" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename = "= gb2312 B y7XD9y50eHQ = =????" 1eK49k1Nv8mwrrK7v8mwrqGjDQo = - ===== 001_Dragon788446150325_ = ==== -. 250 ok: Queued As 7cda613a26e quit 221 BYE This is the SMTP protocol, where the version of the RFC821 document http://itboy.cn/data/rfc821.doc is a Chinese version, so everyone spends some time This is no longer described here. Only list status ID 211 system status or system help response 214 Help information 220

Service

221

Service Close Transport Channel

250 required mail operations

251 users are non-local, will forward

354 Start the mail input,

.

end

421

The service is not ready, turn off the transfer channel (this response can be used as a response to any command when it must be closed)

450 The required mail operation is not completed, the mailbox is unavailable (for example, mailbox busy) 451 to give up the requirements of the requirements;

452 system storage is insufficient, the required operation is not performed

500 Format error, command is not recognized (this error also includes command lines)

501 parameter format error

502 commands are not implemented

503 Error command sequence

504 Command parameters are not implemented

550 The required mail operation is not completed, the mailbox is unavailable (for example, the mailbox is not found, or if you are not accessible)

551 users are non-local, please try

552 excess storage allocation, the required operation is not performed

553 mailbox name is unavailable, the required operation is not executed (such as mailbox format errors)

554 operation failed

It can be seen from this point that the user name and password of the SMTP server are different from the general via SMTP proxy.

Describe the entire process:

First, a mail switch of 263.NET is obtained by the aforementioned method, and then connected to this switch. Then connect to the 25 port of this server,

The server returns 220.

Then indicate the username, send an email (person), receive the mailbox (person). Receive the data written to the mail.

The data is divided into two parts: the body of the mail header and the message.

The email header contains: time, send mailbox (people), receive mailbox (people), theme, sender, MIME version number, email content type and split.

There are some strings encoded with Base64, which are the original Chinese characters. In fact, we can write directly to Chinese when making a SMTP proxy mail sending program.

This is the type and split of the email content, and others are easy to understand.

The type of mail content here is

Multipart / Mixed; Description is made up of a variety of formats.

The separator is used to separate the content part of the message and each accessory. use

Boundary keywords and key values ​​are defined.

For example, this example

===== 001_dragon788446150325 _ ===== to indicate that there is a detail question, the key value is best to use, do not have spaces. For example, if you use

Boundary ====== 001_dragon7884446150325 _ ===== to indicate, the foxmail5.0 will not process the mail correctly, and the content part of the message is used as a whole base64 garbled text, but I log in to the 263.net website. Receiving can see that the message is transformed normally.

The content part of the message is through two minusks.

- Reconnect the partition to separate the part.

Mail main body from the first one

- ===== 001_dragon788446150325 _ ===== start, to the second

- ===== 001_dragon788446150325 _ ===== for the first part of the content

Content-type: text / plain; charset = "GB2312" Content-Transfer-Encoding: Base64

These two sentences explain the character sets and encodings of their types and content.

Here is the Base64, then a blank line, plus "Dear Dreamchild: / r / n This is an email." This string's base64 encodes the body part of the message. In fact, we can specify

Content-Transfer-Encoding: 8bit then can be used in the body part.

Next, it is partially spaced apart parts.

one extra

Content-disposition: attachment; to illustrate this part is an attachment, as well as the relevant file name

FileName = "mm.gif".

The attachment content part is to read the file into a byte array, then turn the byte array into the Base64 encoded string. Here is

Mm.gif This file content.

The third part is annex 2

Test .txt file,

Test .TXT is also processed into base64 format by Foxmail, which can be represented by the original text.

After the last finished, use "Enter a Row. Return to the Rank" indicates the end of the DATA part.

If you send it to the server correctly, then it will return one.

250 state.

Then

Quit Command Heel Server 3166

6. Analysis of Mail Send Program Programming

Let's define a mail structure to describe the individual properties of the message.

Public struct mailcontent // message content

{

Public string to; // recipient address

Public String Toname; // Recipient Name

Public string from; // sender address

Public string fromname; // send a name

Public String Title; // Theme

Public string body; // text content

Public Bool UseAttachment; // Whether to use attachments

Public String [] attachmentlist; // attachment list

}

Then, from each control, it is assigned an example of this structure.

The value of the value includes determining whether the mailbox format is correct, we use a regular expression to determine. as follows:

Regex.ismatch (mailbox string, @ "@ ([/ w - /.] ) @ (/ [[0-9] {1, 3} /. [0-9] {1,3} /. [0-9] {1,3} /.) | ([/ W -] /.))) ([A-ZA-Z] {2, 4} | [0-9] {1, 3}) (/]?) $ ")

If the returned value is fake, use the error prompt to prompt.

After the value is finished, create a work thread to send the message.

The thread starts the list of mailbox domain parts through local DNS. The switch is then taken for the exclusator of its minimum priority to be used for future transmission. For example

DreamChild@263.net This mailbox domain name

263.NET's priority value is 10 MX record

MX12.263.NET

Then it is even connected to the server to send commands and receive returns. Some of the following code is listed below:

About Creating a SOCK Socket:

TCPCLIENT SOCK = New TcpClient ();

NetWorkstream NetStream;

Sock.NodeLay = true; // Does not use the delay algorithm to speed up the transmission of the small data packet.

Sock.receivetimeout = 10000; // Receive timeout is 10 seconds.

Sock.Connect (Server, Port);

NetStream = Sock.getStream ();

The Nagle algorithm of the Socket will reduce the transmission speed of the small datagram, and the system defaults to use the Nagle algorithm. So set nodelay to turn off it to speed up the transmission of small data.

About Send method:

Byte [] Sendarray = Encoding.default.getbytes (SendString); NetStream.write (Sendarray, 0, Sendarray.Length);

First turn the string to byte arrays and send out

About Receive method:

Const int maxReceivesize = 1460;

....

Byte [] buffer = new byte [maxReceivesize];

Length = netstream.read (buffer, 0, maxreceivesize);

IF (Length == 0)

Return NULL;

....

ReceiveString = encoding.default.getstring (buffer, 0, length);

According to "In the Sock_Stream mode, if the single transmission data exceeds 1460, the system will be divided into multiple datagram, which will be a data stream in the other party, and the application needs to increase the determination of the broken frame. Of course, you can use a modified registry. The mode changes the size of 1460, but Micrcosoft believes that 1460 is the best efficiency parameter, not recommended. "

This is set once to receive 1460 bytes, but in fact, the return code can not be used so many bytes. This is before I write a part of the receiving function written based on the HTTP network program, keeping this method.

After receiving, turn it back to the string back.

Send = "Date: {$ time} / r / n"

"From: {$ fromname} <{$ from}> / r / n"

"To: {$ toname} <{$ to}> / r / n"

"Subject: {$ TITLE} / r / n"

"X-MAILER: NOSMTPSENDER [Cai Xiaohui Making] / R / N"

"MIME_VERSION: 1.0 / r / n"

"Content-Type: Multipart / Mixed; Boundary = /" {$ SPLITLINE} / "/ r / n"

"/ r / n" // The head ends, and the body is started.

"- {$ SPLITLINE} / r / n" // Content section.

"Content-Type: Text / Plain; Charset = GB2312 / R / N"

"Content-Transfer-Encoding: 8bit / R / N"

"/ r / n"

"{$ BODY} / R / N"

"/ r / n";

Attachment = "";

Attachment = "- {$ SPLITLINE} / R / N";

Attachment = "Content-Type: Application / OcTet-stream; Name = {$ FileName} / r / n";

Attachment = "Content-Disposition: attachment; filename = {$ filename} / r / n";

Attachment = "Content-Transfer-Encoding: Base64 / R / N";

Attachment = "/ r / n";

Attachment = attachment.replace ("{$ SPLITLINE}", splitline); // Advanced RREs to prevent the replacement of the accessory content. attachment = attachment.replace ("{$ filename}", file.name);

Attachment = Convert.TOBASE64STRING (Filebytes, 0, Length);

Attachment = "/ r / n / r / n";

Send = attachment;

According to the analysis of the previous section, the format is listed, and then replace the {$ variable name} is OK, where the TIME variable name is obtained:

DateTime.now.toString ("R"). Replace ("GMT", "China Standard Time")

I don't know if the VS2003 bug, with the "R" parameter output the current time RFC format Greenwich time, no localization time is automatically converted, such as China time 9 o'clock, it did not subtract 8 hours. And just add "GMT" directly after the current time. So we change the "GMT" to "China Standard Time" to school.

Press a question and answer "Communication", if there is a wrong, then save the wrong information, then

Return False;

If the entire transmission process is not wrong, you can

NetStream.close ();

Sock.close ();

end.

Interface screenshot of the program:

Seven, the last words

Thank you very much, I am very patiently finished. I spent four all night (actually sleeping in the morning) time to write software and tutorial's hard work. This software is a better finished product after learning for half a month. I started learning truly programming from the big two, using the nine months VB and wrote the C / MFC program for another year. Now studying C #. It feels that the foundation of C is very easy.

The method of sending the mail attachment of this document refers to Luo Seniors (

Http://www.luocong.com) C source code, otherwise it may be repeated to think about it. Thanks to Teacher Luo's selflessness.

After writing this stuff, after four days, my summer vacation is over, the next semester is a big four. A very fascinating school year, I want to find a good job in Shanghai after graduation. I want to invite my seniors in the industry to guide me, do you have to cross the threshold of Shanghai University software? I will strive to go beyond the rest of this year.

You can disseminate this software, source, and related documents arbitrarily, but keep integrity.

If it is used for commercial purposes, I need to agree.

my contact information:

Address: Zhangzhou Teachers College 082 Mailbox (363000)

Name: Cai Xiaomei

e-mail:

QQ: 22415

I hope we can make a friend.

If you need me to answer within a year, please send an inquiry to the [Information Technology] section on http://bbs.zzsy.com.

I will help you with your efforts and capabilities.

转载请注明原文地址:https://www.9cbs.com/read-117589.html

New Post(0)