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

📄 receive_email.c

📁 embedded_ethernet_complete_code_rabbit
💻 C
字号:
// Retrieves e-mail messages from a POP3 server.

// This file and other embedded Ethernet and Internet code and resources are
// available from www.Lvr.com.

// Select a network configuration from \lib\tcpip\tcp_config.lib
// in the Dynamic C distribution.
#define TCPCONFIG 1

// The name or IP address of the Rabbit's POP3 server.
// YOU MUST CHANGE THIS VALUE to match the name or IP address of a POP3 server
// your Rabbit module can access.
// If you specify the server by name, you must define MY_NAMESERVER in
// tcpconfig.lib or in this application.
#define POP_HOST	"mail.example.com"

// The user name and password for the Rabbit's e-mail account.
#define POP_USER	"rabbit1"
#define POP_PASS	"embedded"

// For debugging, display communications with the POP3 server in Dynamic C's stdio window.
#define POP_DEBUG

// Don't delete the messages on the server after downloading.
// To delete messages, remove or comment out this line
#define POP_NODELETE

// Store the to, from, subject and body fields of received messages
// in the locations specified in the call to the store_message function.
#define POP_PARSE_EXTRA

// Set the size of the DHCP socket buffer.
// Not required for Dynamic C v8 and higher.
//#define DHCP_SOCK_BUF_SIZE 900

// All C functions not declared as root go to extended memory.
#memmap xmem

// The dcrtcp library supports IP and TCP; the smtp library supports SMTP communications.
#use "dcrtcp.lib"
#use "pop3.lib"

int current_message;

int store_message(int message_number, char *to, char *from, char *subject, char *body_line, int body_length) {

	// This callback function receives and processes downloaded messages.
	// message_number is the number of the message.
	// to, from, and subject contain the contents of the corresponding lines in the e-mail's header.
	// body_line is a line of text in the message body.
	// body_length is the length of body_line.
   // The function is called for each line in a message.

	// The #GLOBAL_INIT section executes only once.
	#GLOBAL_INIT {

		// Initialize the current_message value.
		current_message = -1;
	}

	// If the current message is a new message, set current_message equal to
   // message_number and display the contents of the header in the stdio window.
	if(current_message != message_number) {
		current_message = message_number;
		printf("MESSAGE <%d>\n", current_message);
		printf("FROM: %s\n", from);
		printf("TO: %s\n", to);
		printf("SUBJECT: %s\n", subject);
	}

	// Display a line of the message body in the stdio window.
	printf("%s\n", body_line);
	return 0;
} // end store_message

void main() {

	// Log onto the POP3 server and retrieve e-mail messages.

	static long mail_host_ip;
	static int response;

   // Initialize the TCP/IP stack.
	sock_init();

	// Name the callback function to pass received e-mail to.
	pop3_init(store_message);

   // Convert an IP address or host name string to the longword required by
   // the pop3_getmail function.
	printf("Resolving the mail host's name...\n");
	mail_host_ip = resolve(POP_HOST);

   // Initiate receiving e-mail using the provided user name, password, and
   // POP3 mail server.
	pop3_getmail(POP_USER, POP_PASS, mail_host_ip);
	printf("Receiving e-mail...\n\n");

   // The pop3_tick() function handles communications with the mail server.
	while((response = pop3_tick()) == POP_PENDING)
		continue;

  // When pop3_tick() returns something other than POP_PENDING,
  // detect the status and write a message to the console.
  switch(response) {
    case POP_SUCCESS:
		printf("\nThe messages have been retrieved.\n");
		break;
	 case POP_TIME:
		printf("Timout error.\n");
		break;
	 case POP_ERROR:
		printf("General error.\n");
		break;
	 default:
	   printf("Undefined error.\n");
  }
} // end main

⌨️ 快捷键说明

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