Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

PopMail! -- A POP3 Mailer for Windows


May 1996/PopMail! -- A POP3 Mailer for Windows/Listing 1

Listing 1: POPMAIL.C A Winsock 1.1 complaint POP3 agent


/**********************************************************/
/* POPMAIL.C - A Winsock 1.1 compliant POP3 agent         */
/**********************************************************/
#include <stdlib.h>
#include <string.h>
#include "popmail.rh"   /* resource id file */
#include "popmail.h"    /* variables and structs global to this module */
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  {
	  WNDCLASS wndClass;
	  MSG message;
	  hInst = hInstance;
	  if (!hPrevInstance)
		 {
			 /* create and register the main window class */
			 wndClass.style = CS_HREDRAW | CS_VREDRAW;
			 wndClass.lpfnWndProc = WinProc;
			 wndClass.cbClsExtra = 0;
			 wndClass.cbWndExtra = 0;
			 wndClass.hInstance = hInstance;
			 wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
			 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
			 wndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
			 wndClass.lpszMenuName = "Main";
			 wndClass.lpszClassName = "Popmail";
			 RegisterClass(&wndClass);
			 /* create and register the invisible POP3 event handling window class */
			 wndClass.style = CS_HREDRAW | CS_VREDRAW;
			 wndClass.lpfnWndProc = Pop3Proc;
			 wndClass.cbClsExtra = 0;
			 wndClass.cbWndExtra = 0;
			 wndClass.hInstance = hInstance;
			 wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
			 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
			 wndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
			 wndClass.lpszMenuName = "";
			 wndClass.lpszClassName = "Pop3";
			 RegisterClass(&wndClass);
		 }
	  /* create the main window */
	  hwndMain = CreateWindow
		 (
			 "Popmail",
			 "Simple POP3 Mail Client",
			 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW,
			 CW_USEDEFAULT,
			 CW_USEDEFAULT,			 CW_USEDEFAULT,			 CW_USEDEFAULT,
			 NULL,
			 NULL,
			 hInstance,
			 NULL
		 );
	  ShowWindow(hwndMain, SW_SHOWNORMAL);
	  hwndConfigDlg = 0;
	  /* the main program message loop */
	  while (GetMessage(&message, NULL, 0, 0))
		 {
			 if (hwndConfigDlg == NULL || !IsDialogMessage(hwndConfigDlg, &message))
				{
					TranslateMessage(&message);
					DispatchMessage(&message);
				}
		 }
	  return message.wParam;
  }
long far PASCAL _export WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  {
	  FARPROC lpfnConfigDlgProc;
	  switch (message)
		 {
			 case WM_CREATE:
				strcpy(ConfigFile, "PopMail.Ini");
				/* insert an edit control into the window */
				hwndClient = CreateWindow
				  (
					  "Edit",
					  "",
					  WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | WS_BORDER |
						 ES_LEFT | ES_WANTRETURN | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
					  0,
					  0,					  0,					  0,
					  hwnd,
					  NULL,
					  hInst,
					  NULL
				  );
				hwndPopWindow = CreateWindow
				  (
					  "Pop3",
					  "",
					  WS_CHILD,
					  0,
					  0,					  0,					  0,
					  hwnd,
					  NULL,
					  hInst,
					  NULL
				  );
				SendMessage(hwndClient, WM_SETFONT, GetStockObject(ANSI_FIXED_FONT), 0);
				if (!LoadParms())
				  {
					  MessageBox(hwnd, "Error reading POPMAIL.INI.  You must "
						 "enter your configuration data.", "Program Information",
							MB_OK | MB_ICONINFORMATION);
					  SendMessage(hwnd, WM_COMMAND, CM_CONFIGURE, 0L);
				  }
				SetTimer(hwnd, MAILCHECKTIMER, (Config.Timer * 60000), NULL);
				NetworkReady = InitNetwork();
				if (!NetworkReady)
					MessageBox(hwnd, "The network layer couldn't be initialized.  "
					  "Subsequent attempts will be made at each scheduled mail "
						  "check.", "Program Error", MB_OK | MB_ICONINFORMATION);
				break;
			 case WM_SIZE:
				MoveWindow(hwndClient, 0, 0, LOWORD(lParam), HIWORD(lParam),	TRUE);
				break;
			 case WM_CLOSE:
				SendMessage(hwnd, WM_COMMAND, CM_EXIT, 0L);
				break;
			 case WM_TIMER:
				if (!NetworkReady)
				  NetworkReady = InitNetwork();
				else
				  if (wParam == MAILCHECKTIMER)
					 {
						 if (strlen(Config.ServerIp) == 0) /* must have the IP */
							LookupServer(hwndPopWindow);
						 else
							ConnectToServer(hwndPopWindow);
					 }
				break;
			 case WM_COMMAND:
				switch (wParam)
				  {
					  case CM_CONFIGURE:
						 lpfnConfigDlgProc = MakeProcInstance((FARPROC) ConfigProc, hInst);
						 DialogBox(hInst, "CONFIGURE", hwnd, lpfnConfigDlgProc);
						 FreeProcInstance(lpfnConfigDlgProc);
						 break;
					  case CM_EXIT:
						 PostQuitMessage(0);
						 ShutDownNetwork();
						 break;
				  }
				break;
		 }
	  return DefWindowProc(hwnd, message, wParam, lParam);
  }
BOOL FAR PASCAL _export ConfigProc(HWND hwnd, UINT message, UINT wParam, LONG lParam)  {
	  char timer[5];
	  switch (message)
		 {
			 case WM_INITDIALOG:
				if ((Config.Timer <= 0) || (Config.Timer >= 61)) Config.Timer = 1;
				SendMessage(hwnd, WM_COMMAND, IMSG_PUTDATA, 0L);
				return TRUE;
			 case WM_COMMAND:
				switch (wParam)
				  {
					  case IMSG_PUTDATA:
						 SetWindowText(GetDlgItem(hwnd, CF_SERVERNAME), Config.ServerName);
						 SetWindowText(GetDlgItem(hwnd, CF_SERVERIP), Config.ServerIp);
						 SetWindowText(GetDlgItem(hwnd, CF_POP3NAME), Config.Pop3Name);
						 SetWindowText(GetDlgItem(hwnd, CF_POP3PASS), Config.Pop3Pass);
						 SetWindowText(GetDlgItem(hwnd, CF_MAILDIR), Config.MailDir);
						 itoa(Config.Timer, timer, 10);
						 SetWindowText(GetDlgItem(hwnd, CF_TIMER), timer);
						 break;
					  case IMSG_GETDATA:
						 GetWindowText(GetDlgItem(hwnd, CF_SERVERNAME), Config.ServerName, sizeof(Config.ServerName));
						 GetWindowText(GetDlgItem(hwnd, CF_SERVERIP), Config.ServerIp, sizeof(Config.ServerIp));
						 GetWindowText(GetDlgItem(hwnd, CF_POP3NAME), Config.Pop3Name, sizeof(Config.Pop3Name));
						 GetWindowText(GetDlgItem(hwnd, CF_POP3PASS), Config.Pop3Pass, sizeof(Config.Pop3Pass));
						 GetWindowText(GetDlgItem(hwnd, CF_MAILDIR), Config.MailDir, sizeof(Config.MailDir));
						 GetWindowText(GetDlgItem(hwnd, CF_TIMER), timer, sizeof(timer));
						 Config.Timer = atoi(timer);
						 break;
					  case IDCANCEL:
						 EndDialog(hwnd, 0);
						 return FALSE;
					  case IDOK:
						 SendMessage(hwnd, WM_COMMAND, IMSG_GETDATA, 0L);
						 if (VerifyParms())
							{
							  SaveParms();
							  EndDialog(hwnd, 0);
							}
						 else MessageBox(hwndMain, "Please enter either your mail server's domain name "
							"or it's IP address (or both)", "Program Information", MB_OK | MB_ICONINFORMATION);
						 return TRUE;
				  }
		 }
	  return FALSE;
  }
BOOL LoadParms(void)  {	  GetPrivateProfileString("PopMail", "ServerName", "", Config.ServerName, sizeof(Config.ServerName), ConfigFile);	  GetPrivateProfileString("PopMail", "ServerIp", "", Config.ServerIp, sizeof(Config.ServerIp), ConfigFile);	  GetPrivateProfileString("PopMail", "Pop3Name", "", Config.Pop3Name, sizeof(Config.Pop3Name), ConfigFile);	  GetPrivateProfileString("PopMail", "Pop3Pass", "", Config.Pop3Pass, sizeof(Config.Pop3Pass), ConfigFile);	  GetPrivateProfileString("PopMail", "MailDir", "", Config.MailDir, sizeof(Config.MailDir), ConfigFile);	  Config.Timer = GetPrivateProfileInt("PopMail", "Timer", 0, ConfigFile);	  return (VerifyParms());  }BOOL VerifyParms(void)  {	  if (Config.Timer == 0) Config.Timer = 1;	  if (strlen(Config.MailDir) == 0) strcpy(Config.MailDir, "\\");	  if (Config.MailDir[strlen(Config.MailDir) - 1] != '\\') strcat(Config.MailDir, "\\");	  if ((strlen(Config.ServerName) == 0) && (strlen(Config.ServerIp) == 0)) return FALSE;	  if ((strlen(Config.Pop3Name) == 0) || (strlen(Config.Pop3Pass) == 0)) return FALSE;	  return TRUE;  }void SaveParms(void)  {	  char timer[5];	  itoa(Config.Timer, timer, 10);	  WritePrivateProfileString("PopMail", "ServerName", Config.ServerName, ConfigFile);	  WritePrivateProfileString("PopMail", "ServerIp", Config.ServerIp, ConfigFile);	  WritePrivateProfileString("PopMail", "Pop3Name", Config.Pop3Name, ConfigFile);	  WritePrivateProfileString("PopMail", "Pop3Pass", Config.Pop3Pass, ConfigFile);	  WritePrivateProfileString("PopMail", "MailDir", Config.MailDir, ConfigFile);	  WritePrivateProfileString("PopMail", "Timer", timer, ConfigFile);  }void Display(char* strText)  {	  int iSize;	  HLOCAL hBuffer;     char NEAR* npBuffer;	  hBuffer = (HLOCAL) SendMessage(hwndClient, EM_GETHANDLE, 0, 0L);	  LocalUnlock(hBuffer);	  iSize = LocalSize(hBuffer);
	  LocalReAlloc(hBuffer, iSize + strlen(strText), 0);
	  npBuffer = (char NEAR*) LocalLock(hBuffer);
	  strcat(npBuffer, strText);
	  LocalUnlock(hBuffer);
	  SendMessage(hwndClient, EM_SETHANDLE, (WPARAM) (HLOCAL) hBuffer, 0);
  }

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.