A preliminary study of SMTP protocol (ii)----Linux under C Programming implementation of email __ block chain

Source: Internet
Author: User
Tags base64

Modeled on, SMTP protocol (i)--My previous blog, combined with the material on the web, using sockets, wrote a Linux under the C programming implementation of the email program.

You can take their own mailbox to do experiments, Base64 is a way of encoding, online can find a Web page, you can query the password of the Base64 code is how much. Of course, I can also manually calculate (I have not tried).

The specific source code is as follows:

#include <iostream> #include <sys/types.h> #include <sys/socket.h> #include <netinet/i n.h> #include <netdb.h> #include <stdio.h> #include <string.h>//using namespace St      
D #define EHLO "EHLO ***\r\n"//*** for the mailbox username #define DATA "data\r\n" #define QUIT "quit\r\n"//#define H_ADDR H_ad     
Dr_list[0]//file *fin;     
int sock;     
struct SOCKADDR_IN server;     
struct Hostent *hp, *gethostbyname ();     
Char buf[bufsiz+1];     
int Len;     
Char *host_id= "smtp.126.com";     
Char *from_id= "***@126.com";     
Char *to_id= "60462**** @qq. com";     
Char *sub= "testmail\r\n";      

Char wkstr[100]= "Hello how r u\r\n";         
	/*=====send a string to the socket=====*/void Send_socket (char *s) {write (Sock,s,strlen (s));         
	Write (1,s,strlen (s));     
printf ("client:%s\n", s); }//=====read a string from the socket=====*/void Read_socket () {len = Read (Sock,buf,bufsiz);
	Write (1,buf,len);     
printf ("server:%s\n", buf); /*=====main=====*/int MAIN (int argc, char* argv[]) {/*=====create socket=====*/sock = Socke     
	T (af_inet, SOCK_STREAM, 0);
		if (sock==-1) {perror ("opening stream socket");  
		Exit (1);   
	return 1;
		else//cout << "Socket created\n";

	printf ("Socket created\n");
	/*=====verify host=====*/server.sin_family = af_inet;
	HP = gethostbyname (host_id);
		if (hp== (struct hostent *) 0) {fprintf (stderr, "%s:unknown host\n", host_id);
		Exit (2);
	return 2; }/*=====connect to port on remote host=====*/memcpy ((char *) &server.sin_addr, (char *) HP-&GT;H_ADDR, hp->     	 				
	H_length); Server.sin_port=htons (25); /* SMTP PORT/if (Connect (sock, (struct sockaddr *) &server, sizeof server) ==-1) {perror ("Connecting Stream SOC
		Ket ");
		Exit (1);
	return 1;
		else//cout << "connected\n"; printf ("Connected\n "); /*=====write Some data then read some =====*/read_socket (); * * SMTP Server logon string/Send_socket (EHLO); /* Introduce ourselves * * Read_socket ();
	/*read Reply * * * **added by Fupeng * * Send_socket ("AUTH LOGIN");
	Send_socket ("\ r \ n");
	Read_socket ();
	Send_socket ("*******");//user name Base64 encoding send_socket ("\ r \ n");
	Read_socket ();
	Send_socket ("Hu Jintao");//Password base64 encoding Send_socket ("\ r \ n");

	Read_socket ();
	Send_socket ("Mail from <");
	Send_socket (from_id);
	Send_socket (">");
	Send_socket ("\ r \ n"); Read_socket ();
	/* Sender OK * *//send_socket ("VRFY");
	Send_socket (from_id);     
	Send_socket ("\ r \ n"); Read_socket (); Sender OK * * Send_socket ("RCPT to <");
	/*mail to*/Send_socket (to_id);
	Send_socket (">");
	Send_socket ("\ r \ n"); Read_socket (); 
	Recipient ok*/Send_socket (DATA);//Body to follow*/read_socket ();
	Send_socket ("from:***@126.com");
	Send_socket ("Subject:");
	Send_socket (sub); Read_socket (); Recipient ok*/Send_socket ("\r\n\r\n");
	Send_socket (WKSTR);
	Send_socket (". \ r \ n");
	Read_socket (); Send_socket (QUIT); /* Quit * * Read_socket ();
	Log off *///=====close socket and finish=====*/close (sock);
	Exit (0);
return 0;  }

The results are shown in the following picture:


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.