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

📄 smtp.c

📁 DOS下开发网络程序的SOCK库,有示例程序,包括FTP
💻 C
字号:
/*

DM&P Mity-Mite Demo Program.
Copyright (C) 2002 by DM&P.

This file is from example code of DSock.

*/

#include "smtp.h"
#include "gpio.h"
#include "grlcd.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

int  _nMailLine = 0;
char _ppcMailBuf[MAX_MAIL_LINE][MAIL_LINE_SIZE];

int GetReply(SOCKET s);

BOOL SendMail(char *szMailServer,char *szFrom,char *szTo,char *szSubject,char *szContent)
{
  SOCKET s;
  DWORD dwIp;
  dwIp = DSock_Resolve(szMailServer);
  printf("Send mail, connecting to mail server %s...",szMailServer);
  s = SocketCreate(TCP_SOCKET);
  if(s==INVALID_SOCKET)
  {
    printf("Unable to create socket.\n");
    return FALSE;
  }
  if(SocketConnect(s,dwIp,SMTP_PORT)==FALSE)
  {
    printf("Unable to connect to mail server.\n");
    SocketDestory(s);
    return FALSE;
  }
  printf("OK\n");

  SocketPutString(s,"HELO Mity-Mite Module\r\n");
  if(GetReply(s)<0) return FALSE;

  SocketPutString(s,"MAIL FROM: %s\r\n",szFrom);
  if(GetReply(s)<0) return FALSE;

  SocketPutString(s,"RCPT TO: %s\r\n",szTo);
  if(GetReply(s)<0) return FALSE;

  SocketPutString(s,"DATA\r\n");
  while(TRUE)
  {
    int n = GetReply(s);
    if(n<0)
      return FALSE;
    if(n==354)
      break;
  }

  SocketPutString(s,"From: %s\r\n",szFrom);
  SocketPutString(s,"To: %s\r\n",szTo);
  SocketPutString(s,"Subject: %s\r\n",szSubject);
  SocketPutString(s,"\r\n");
  SocketPutString(s,szContent);
  SocketPutString(s,"\r\n");
  SocketPutString(s,"--\r\n");
  SocketPutString(s,"This mail is sent by Mity-Mite Module.\r\n");
  SocketPutString(s,"\r\n.\r\n");

  SocketPutString(s,"QUIT");
  SocketClose(s);
  SocketDestory(s);

  printf("Mail is sent.\n");
  return TRUE;
}

int GetReply(SOCKET s)
{
  char szTemp[1024];
  while(SocketDataReady(s)==0);
  SocketGetString(s,szTemp,1024);
  return atoi(szTemp);
}

BOOL SmtpServer(SOCKET s)
{
  char   szBuf[1024];
  DWORD  dwIp;

  if(SocketAccept(s,&dwIp)==FALSE)
    return FALSE;

  inet_ntoa(szBuf,dwIp);
  printf("SMTP: Accepting mail from %s\n",szBuf);

  SocketPutString(s,"220 mmm.dmp.com.tw DSockMail ready\r\n");

  while(TRUE)
  {
    if(kbhit()) break;
    if(SocketDataReady(s)==FALSE)
      continue;
    SocketGetString(s,szBuf,1024);
    printf("SMTP: %s\n",szBuf);
    if(strnicmp(szBuf,"HELO",4)==0)
    {
      SocketPutString(s,"250 Mity-Mite Module\r\n");
    }
    else if(strnicmp(szBuf,"MAIL",4)==0)
    {
      SocketPutString(s,"250 OK\r\n");
    }
    else if(strnicmp(szBuf,"RCPT",4)==0)
    {
      SocketPutString(s,"250 OK\r\n");
    }
    else if(strnicmp(szBuf,"QUIT",4)==0)
    {
      SocketPutString(s,"221 Bye\r\n");
      break;
    }
    else if(strnicmp(szBuf,"DATA",4)==0)
    {
      _nMailLine = 0;
      SocketPutString(s,"354 Enter mail, end with \".\" on a line by itself\r\n");
      while(TRUE)
      {
        if(kbhit()) break;
        if(SocketDataReady(s)==FALSE)
          continue;
        SocketGetString(s,szBuf,1024);
        printf("      %s\n",szBuf);
        if(stricmp(szBuf,".")==0)
          break;
        if((_nMailLine+1)<MAX_MAIL_LINE)
        {
          strncpy(_ppcMailBuf[_nMailLine],szBuf,MAIL_LINE_SIZE-1);
          _ppcMailBuf[_nMailLine][MAIL_LINE_SIZE] = 0;
          _nMailLine++;
        }
      }
      SocketPutString(s,"250 Okay\r\n");
    }
    else if(strnicmp(szBuf,"NOOP",4)==0)
    {
      SocketPutString(s,"250 NOOP\r\n");
    }
    else
    {
      SocketPutString(s,"500 Error\r\n");
    }
  }

  SocketClose(s);
  SocketBind(s,0L,SMTP_PORT);
  SocketListen(s);
  return TRUE;
}

void DumpMail()
{
  int i;
  int nIdx = 0;
  int k;
  int nEnd;
  GrLcd_ClearScreen();
  if(_nMailLine==0)
  {
    GrLcd_printf(0,8*0,"<No Mail>");
    while(GetKey()==KEY_NULL);
    return;
  }
  DumpBuf(_ppcMailBuf,_nMailLine);
  return;
  while(TRUE)
  {
    nEnd = (nIdx+4)>=_nMailLine ? _nMailLine : nIdx+4;
    if(nIdx!=nEnd)
      GrLcd_ClearScreen();
    for(i=nIdx;i<nEnd;i++)
      GrLcd_printf(0,8*(i-nIdx),"%s",_ppcMailBuf[i]);
    while((k=GetKey())==KEY_NULL);
    if(k==KEY_ENTER)
      break;
    if(k==KEY_LEFT)
      if(nIdx>=1)
        nIdx--;
    if(k==KEY_RIGHT)
      if((nIdx+4)<_nMailLine)
        nIdx++;
  }
  GrLcd_ClearScreen();
}

⌨️ 快捷键说明

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