⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smtp.c

📁 W3100是WIZnet公司专门为以太网互联和嵌入式设备推出的硬件TCP/IP协议栈芯片
💻 C
字号:
/*
********************************************************************************
* Wiznet.
* 5F Simmtech Bldg., 228-3, Nonhyun-dong, Kangnam-gu,
* Seoul, Korea
*
* (c) Copyright 2002, Wiznet, Seoul, Korea
*
* Filename : smtp.c
* Version  : 1.5
* Programmer(s): Kim Wooyoul
* Created  : 2002/05/28
* Modified :	Date - 2002/10/28
*		Description - Version Up (1.0 -> 1.5)
*			    - Use getSocket() function in sockutil.h
*			    - Remove goto statements in SendMail() function()
*			    - Type Converion of ServerAddr Variable (u_char --> u_long)
*			    - Increase Global Memory size(ACCNT, RMAIL,SMAIL, TITLE,Buffer)
*			    - Add to debug-code
*			    - Apply new sockutil.h and refined util.h
* Description : SMTP server related functions
********************************************************************************
*/

/*
###############################################################################
Include Part
###############################################################################
*/
#include "util.h"
#include "socket.h"
#include "serial.h"
#include "smtp.h"
#include "sockutil.h"

/*
###############################################################################
Define Part
###############################################################################
*/
#define SOCKET_ERROR	-1
#define MAX_LENGTH	1024		// Maximum length of Send & Receive Buffer

/*
###############################################################################
Grobal Variable 急攫 Part
###############################################################################
*/

u_long xdata ServerAddr;		// Mail Server IP Address          
UCHAR xdata ACCNT[20];	     		// User Account
UCHAR xdata SMAIL[50];			// Sender Mail Address 
UCHAR xdata RMAIL[50];			// Recipient Mail Address
UCHAR xdata TITLE[256];			// Mail title

UCHAR Inc = 0;				// Incrementing variable for source port to connect to the Mail Server

UCHAR xdata Buffer[MAX_LENGTH];		// Send & Receive Buffer

/*
###############################################################################
Function Prototype 急攫 Part
###############################################################################
*/

/*
********************************************************************************
*              Function to store Mail server IP address to global variable
*
* Description: Receive and convert character string to ServerAddr
* Arguments  : IP : Mail Server IP Address which is character number typed, transferred byHTTP Request
* Returns    : none.
* Note       : 
********************************************************************************
*/
void SetMailServerIP(UCHAR * ip)
{
	ServerAddr = inet_addr(ip);
	PutString("Mail Server IP : "); PutStringLn(ip);
}

/*
********************************************************************************
*              Function to store User Account to global variable
*
* Description: Store User Account to Global variable
* Arguments  : acc : user account which is transferred by HTTP Request
* Returns    : none.
* Note       : 
********************************************************************************
*/
void SetAccount(UCHAR * acc)
{
	MemCopy(ACCNT,acc);
	PutString("ACCOUNT = ");PutStringLn(ACCNT);
}

/*
********************************************************************************
*              Function to store Sender Mail Address to global variable
*
* Description: Store Sender Mail Address to global variable
* Arguments  : mail : Sender Mail Address which is transferred by HTTP Request
* Returns    : none.
* Note       : 
********************************************************************************
*/
void SetSMailAddr(UCHAR* mail)
{
	MemCopy(SMAIL,mail);
	PutString("SMAIL = ");PutStringLn(SMAIL);
}

/*
********************************************************************************
*              Function to store Recipient Mail Address to Global variable
*
* Description: store Recipient Mail Address to Global variable
* Arguments  : mail : Recipient Mail Address which is transferred by HTTP Request
* Returns    : none.
* Note       : 
********************************************************************************
*/
void SetRMailAddr(UCHAR* mail)
{
	MemCopy(RMAIL,mail);
	PutString("RMAIL = ");PutStringLn(RMAIL);
}

/*
********************************************************************************
*              Function to store Mail Title to Global variable
*
* Description: store Mail Title to Global variable
* Arguments  : title : Mail Title which is transferred by HTTP Request
* Returns    : none.
* Note       : 
********************************************************************************
*/
void SetTitle(UCHAR* title)
{
	MemCopy(TITLE,title);
	PutString("Title = ");PutStringLn(TITLE);
}

/*
********************************************************************************
*              Function to tranfer Mail Content to designated Mail Server
*
* Description: Send Mail contect to designated mail server by SMTP Protocol
* Arguments  : contents : Mail content which is transferred by HTTP Request
* Returns    : 1(success), -1(fail)
* Note       : 
********************************************************************************
*/
char SendMail(UCHAR* contents)
{
	UINT i;
	UINT len;
	SOCKET MailSock;  		
	
	MailSock = getSocket(SOCK_CLOSED,0);
	if(MailSock == -1) return -1;					// Fail to find free socket
	MailSock = socket(MailSock, SOCK_STREAM,7000+Inc++,0);		// Create a socket to connect to the Mail Server with port number 7000 ~ 7255
	if(MailSock == -1)	return -1;					// Fail to create socket
	PutString("MailSock : ");PutHTOA(MailSock);PutStringLn("");

	contents[MAX_LENGTH-1] = '\0';					// Truncate mail content not to exceed send buffer. Protect Memory Access Vialoation

	PutString("Contents = "); PutStringLn(contents);

	if (SOCKET_ERROR == connect(MailSock,(char*)&ServerAddr, 25))		// Try to connect to the mail server (SMTP Port = 25)
	{
		close(MailSock); return -1;
	}
	PutStringLn("Connect OK");

	
	while( select(MailSock,SEL_CONTROL) == SOCK_ESTABLISHED) 	// If succeeded to connect to the mail server, follow the procedure SMTP Protocol
	{
		if( (len = select(MailSock,SEL_RECV)) > 0)
		{
			/* Analyze received message from the server */
			i = 0;
		    	len = recv(MailSock, Buffer,len);
			if(len == SOCKET_ERROR) break;
			*(Buffer+len) = '\0';
			MakeUpper(Buffer);
                        			
			if( FindFirstStr(Buffer,"ESMTP" /*SENDMAIL"*/) != 0)	// Connect & Greeting 
			{       			 	
				MemCopy(Buffer,"helo ");			// Send Greeting Message of the client
				MemCopy(Buffer+5, ACCNT);
			}
			else  if ( FindFirstStr(Buffer,"HELLO") != 0)		// Mail Server Greeting
			{               				 
				MemCopy(Buffer,"MAIL FROM:<");			// Inform send address to the server
				MemCopy(Buffer+11,SMAIL);
				MemCat(Buffer,">");
			}  			
			else if ( FindFirstStr(Buffer,"SENDER" /*OK"*/) !=0)	// Mail Server狼  Sender Mail Address Accept OK
			{             			
				MemCopy(Buffer,"RCPT TO:<");  			// Send Recipient Mail Address to the server
				MemCopy(Buffer+9,RMAIL);
				MemCat(Buffer,">");
			}          			
			else if ( FindFirstStr(Buffer,"RECIPIENT" /*OK"*/) !=0)	// Recipient Mail Address Accept OK
			{  				
				MemCopy(Buffer,"DATA\r\nSubject: ");		// Inform to the server when ready to send mail
				MemCopy(Buffer+15,TITLE);
			}      			
			else if ( FindFirstStr(Buffer,"ENTER" /*MAIL"*/) !=0)	// Allow server mail tranfer
			{   	
				MemCat(contents,"\r\n.");			// Send mail content
				MemCopy(Buffer,contents);
   			}  			
			else if ( FindFirstStr(Buffer,"MESSAGE" /*ACCEPTED"*/) !=0)	// Recieved mail successfully
			{   				
				MemCopy(Buffer,"quit");					// Request of connection close from the server
			} 		
			else if ( FindFirstStr(Buffer,"CLOSING" /*CONNECT"*/) !=0) 	// Connection close of server is OK
			{
				PutStringLn("Mail OK");
				close(MailSock); return 1;				// Success to send mail
			}			
			else								// Otherwise, error
			{
				PutStringLn(Buffer);
				break;
			}
						
			MemCat(Buffer,"\r\n\0");					// Send appropriate Procedure to the mail server
			if(SOCKET_ERROR == send(MailSock,Buffer,StrLen(Buffer)))
			{
				PutStringLn("Fail to send mail");
				break;
			}
		}

		/* In case that hard to receive mail from the mail server    */
	    	/* Error will be returned after some time goes by            */
        	/* because of endless while loop  */
		if( i++ == 0xFFFF) break;
	}
	close(MailSock);
	return -1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -