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

📄 client.c

📁 C语言实现的一个简易的SMPT服务器代码
💻 C
字号:
/* *  Embedded SMTP Client * *  ./software/ch5/emsmtpd/client.c * *  Copyright 2001,2002 M. Tim Jones <mtj@cogitollc.com> * *  mtj@cogitollc.com * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * General Public License for more details. * */#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <string.h>#include <stdio.h>#include "client.h"static char *hello_msg={"HELO my_embedded_board.com\n"};static char *mailServer={"mail.mtjones.com"};/* *  strscan() * *  Extract the return code and check it against the passed *  reference code (str). * *  Returns 0 on success, -1 on error. * */int strscan(char *line, char *str, int len){  int i;  for (i = 0 ; i < strlen(line) - len ; i++) {    if (line[i] == str[0]) {      if (!strncmp(line, str, 3)) return(0);    }  }  return(-1);}/* *  dialog() * *  Perform a dialog with the connected mail server.  If the command is  *  present (non-NULL), send it through the socket to the server.  If  *  the response is present, check it against the return code from the  *  mail server. * *  Returns 0 on success, -1 on error. * */int dialog(int sd, char *command, char *resp){  int ret, len;  char line[128];  if (command != NULL) {    len = strlen(command);    if (write(sd, command, len) != len) return -1;  }  if (resp != NULL) {    ret = read(sd, line, sizeof(line)-1); line[ret] = 0;    if (strscan(line, resp, 3) == -1) return -1;  }  return 0;}/* *  sendMail() * *  Given a passed mailHeader structure, send the email to the mail *  server defined by the mailServer variable. * *  Returns 0 on success, -1 on error. * */int sendMail(struct mailHeader *mail){  int connfd, result, ret, goodMsg = 0;  struct sockaddr_in servaddr;  char mailRcpt[129];  char line[256];  connfd = socket(AF_INET, SOCK_STREAM, 0);  bzero((void *)&servaddr, sizeof(servaddr));  servaddr.sin_family = AF_INET;  servaddr.sin_port = htons(25);  servaddr.sin_addr.s_addr = inet_addr(mailServer);  /* if the prior inet_addr results in a '-1' (or error), then we   * assume that the gateway symbolic is not an dotted-notation   * IP address.  It must therefore be a fully-qualified domain name   * and we use gethostbyname to resolve it.   */  if (servaddr.sin_addr.s_addr == 0xffffffff) {    struct hostent *hptr = (struct hostent *)gethostbyname(mailServer);    if (hptr == NULL) {      /* Don't know what the mailServer represents... */      return(-1);    } else {      struct in_addr **addrs;      addrs = (struct in_addr **)hptr->h_addr_list;      memcpy(&servaddr.sin_addr,       *addrs, sizeof(struct in_addr));    }  }  result = connect(connfd,                     (struct sockaddr *)&servaddr, sizeof(servaddr));  do {    /* Look for initial salutation */    if ( dialog( connfd, NULL, "220" ) ) break;    /* Send HELO and await response */    if ( dialog( connfd, hello_msg, "250" ) ) break;    /* Send MAIL FROM and await response */    sprintf(line, "MAIL FROM:<%s>\n", mail->sender);    if ( dialog( connfd, line, "250" ) ) break;    /* Send RCPT TO and await response */    sprintf(line, "RCPT TO:<%s>\n", mail->recipient);    if ( dialog( connfd, line, "250" ) ) break;    /* Send DATA and await response */    if ( dialog( connfd, "DATA\n", "354" ) ) break;    /* Send out the header first */    sprintf(line, "From: %s\n", mail->sender);    if ( dialog( connfd, line, NULL ) ) break;    sprintf(line, "To: %s\n", mail->recipient);    if ( dialog( connfd, line, NULL ) ) break;    sprintf(line, "Subject: %s\n", mail->subject);    if ( dialog( connfd, line, NULL ) ) break;    if (mail->contentType[0] != 0) {      sprintf(line, "Content-Type: %s\n", mail->contentType);      if ( dialog( connfd, line, NULL ) ) break;    }    if (mail->specialHeaders[0] != 0) {      if ( dialog( connfd, mail->specialHeaders, NULL ) ) break;    }    if ( dialog( connfd, mail->contents, NULL ) ) break;    /* Send mail-end and await response */    if ( dialog( connfd, "\n.\n", "250" ) ) break;    if ( dialog( connfd, "QUIT\n", "221" ) ) break;    goodMsg = 1;  } while (0);  close(connfd);  return(goodMsg);}

⌨️ 快捷键说明

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