SMTP協議初探(二)----linux下c程式設計實現發郵件

pengfoo發表於2012-03-10

仿照,SMTP協議初探(一)--我的前一篇部落格,結合網上的材料,利用socket,寫了個Linux下c程式設計實現的發郵件程式。

大家可以拿自己的郵箱去做實驗啦,base64是一種編碼方式,網上可以找到一個網頁,可以查詢密碼的base64編碼是多少。當然也可以自己手工計算(我沒試過)。

具體原始碼如下:

 

//#include<iostream>
#include <sys/types.h>      
#include <sys/socket.h>      
#include <netinet/in.h>      
#include <netdb.h>      
#include <stdio.h> 
#include <string.h>     
//using namespace std;      
#define EHLO "EHLO ***\r\n" //***為郵箱使用者名稱     
#define DATA "data\r\n"      
#define QUIT "QUIT\r\n" 

//#define h_addr h_addr_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 = socket(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 25 on remote host=====*/
	memcpy((char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length);     	 				
	server.sin_port=htons(25); /* SMTP PORT */
	if (connect(sock, (struct sockaddr *) &server, sizeof server)==-1)
	{
		perror("connecting stream socket");
		//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("*******");//使用者名稱的base64編碼
	send_socket("\r\n");
	read_socket();
	send_socket("******");//密碼的base64編碼
	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;
} 

具體結果如下圖片所示:


 

相關文章