这个例子可以用,不过你要自己读文件的内容先: // QSMTP // // Customize the following variables and constants for your use: // HOST_NAME // The HELO parameter in MailMessage[] // The MAIL parameter in MailMessage[] // The RCPT parameter in MailMessage[]#include <string.h> #include "..\winsock.h"#define PROG_NAME "Quick SMTP Demo"#define HOST_NAME "your.host" // Define the mail server for your account #define WINSOCK_VERSION 0x0101 // Program requires Winsock version 1.1 #define DEFAULT_PROTOCOL 0 // No protocol specified, use default #define NO_FLAGS 0 // No special flags specifiedchar sScratchBuffer[100]; // General purpose buffer for messagesVOID SendMail(SOCKET hSocket) { BYTE sReceiveBuffer[4096]; // Buffer for mail server replies int iLength; // Length of mail server reply int iMsg = 0; // Index into SMTP command array int iEnd; // Pointer to the end of the reply buffer
// Define SMTP command strings as an array. // Make sure last element remains NULL. // Command strings are sent in sequence. char *MailMessage[] = { "HELO HOST.COM\r\n", "MAIL FROM:<[email protected]>\r\n", "RCPT TO:<[email protected]>\r\n", "DATA\r\n", "This is my message to myself.\r\n\r\n.\r\n", "QUIT\r\n", 0 };
iEnd = 0; do { if (send(hSocket, (LPSTR)MailMessage[iMsg], strlen(MailMessage[iMsg]), NO_FLAGS) == SOCKET_ERROR) { MessageBox(NULL, "The send() function returned a socket error.", PROG_NAME, MB_OK|MB_ICONSTOP); break; } iLength = recv(hSocket, (LPSTR)sReceiveBuffer+iEnd, sizeof(sReceiveBuffer)-iEnd, NO_FLAGS);
if (iLength == 0 || iLength == SOCKET_ERROR) { MessageBox(NULL, "The recv() function returned a socket error.", PROG_NAME, MB_OK|MB_ICONSTOP); break; }
多谢两位仁兄,我忘了说了,我需要的是API的,不能用控件也不能用MFC!
还请指点12!!!!
#define Server "public1.ptt.js.cn"
#define COMMAND1 "HELO\r\n\r\n"
#define COMMAND2 "mail from:[email protected]\r\n\r\n"
#define COMMAND3 "rcpt to:[email protected]"
#define COMMAND4 "data\r\n"
#define MESSAGEBODY "hello from qingzl\r\n.\r\n"#define PORT 25
int SockSendAll(SOCKET s,LPCSTR buf,UINT length)
{
UINT nSent=0;
int c_send;
while(nSent<length) {
c_send=send(s,buf+nSent,length-nSent>1024? 1024:length-nSent,0);
if(c_send<0)
return -1;
nSent+=c_send;
}
return nSent;
}int main()
{
int rc;
WSADATA ws;
rc = WSAStartup(0x0101,&ws);if(rc!=0) {
#ifdef _DEBUG
printf("Error :WinSock initialize failed\n");
#endif
return -1;
}struct sockaddr_in sa;
HOSTENT *hp;
SOCKET sock=NULL;sock =socket(AF_INET,SOCK_STREAM,0);
if(sock==INVALID_SOCKET ) {
#ifdef _DEBUG
printf("Error :Can't initialize TCP/IP socket\n");
#endif
return INVALID_SOCKET;
}hp=gethostbyname(Server);
if(!hp) {
#ifdef _DEBUG
printf("Error :Can't find this server :%s\n",Server);
#endif
return INVALID_SOCKET;
}sa.sin_family = AF_INET;
sa.sin_addr.s_addr = *(unsigned long*)hp->h_addr; /* Server IP */
sa.sin_port = htons(PORT); /* Server Port number */if (connect(sock,(struct sockaddr*)&sa,sizeof(sa))!=0){
#ifdef _DEBUG
printf("Error :Connection failed\n");
#endif
return INVALID_SOCKET;
}SockSendAll(sock,COMMAND1,sizeof(COMMAND1)-1);
SockSendAll(sock,COMMAND2,sizeof(COMMAND2)-1);
SockSendAll(sock,COMMAND3,sizeof(COMMAND3)-1);
SockSendAll(sock,COMMAND4,sizeof(COMMAND4)-1);
SockSendALL(sock,MESSAGEBODY,sizeof(MESSAGEBODY)-1);
}
#define COMMAND3 "rcpt to:[email protected]"
改为
#define COMMAND3 "rcpt to:[email protected]\r\n\r\n"
偶的木马要求、、、mapi是借助oe、、、差老远了
// QSMTP
//
// Customize the following variables and constants for your use:
// HOST_NAME
// The HELO parameter in MailMessage[]
// The MAIL parameter in MailMessage[]
// The RCPT parameter in MailMessage[]#include <string.h>
#include "..\winsock.h"#define PROG_NAME "Quick SMTP Demo"#define HOST_NAME "your.host" // Define the mail server for your account
#define WINSOCK_VERSION 0x0101 // Program requires Winsock version 1.1
#define DEFAULT_PROTOCOL 0 // No protocol specified, use default
#define NO_FLAGS 0 // No special flags specifiedchar sScratchBuffer[100]; // General purpose buffer for messagesVOID SendMail(SOCKET hSocket)
{
BYTE sReceiveBuffer[4096]; // Buffer for mail server replies
int iLength; // Length of mail server reply
int iMsg = 0; // Index into SMTP command array
int iEnd; // Pointer to the end of the reply buffer
// Define SMTP command strings as an array.
// Make sure last element remains NULL.
// Command strings are sent in sequence.
char *MailMessage[] =
{
"HELO HOST.COM\r\n",
"MAIL FROM:<[email protected]>\r\n",
"RCPT TO:<[email protected]>\r\n",
"DATA\r\n",
"This is my message to myself.\r\n\r\n.\r\n",
"QUIT\r\n",
0
};
iEnd = 0;
do
{
if (send(hSocket, (LPSTR)MailMessage[iMsg],
strlen(MailMessage[iMsg]), NO_FLAGS) == SOCKET_ERROR)
{
MessageBox(NULL,
"The send() function returned a socket error.",
PROG_NAME, MB_OK|MB_ICONSTOP);
break;
}
iLength = recv(hSocket, (LPSTR)sReceiveBuffer+iEnd,
sizeof(sReceiveBuffer)-iEnd, NO_FLAGS);
if (iLength == 0 || iLength == SOCKET_ERROR)
{
MessageBox(NULL,
"The recv() function returned a socket error.",
PROG_NAME, MB_OK|MB_ICONSTOP);
break;
}
iEnd += iLength;
sReceiveBuffer[iEnd] = '\0';
MessageBox(NULL, (LPSTR)sReceiveBuffer, (LPSTR)MailMessage[iMsg],
MB_OK|MB_ICONSTOP);
iMsg++;
}
while (MailMessage[iMsg]);
return;
}SOCKET ConnectMailServerSocket(VOID)
{
WSADATA wsaData; // Winsock implementation details
LPHOSTENT lpHostEnt; // Internet host information structure
SOCKADDR_IN sockAddr; // Socket address structure
LPSERVENT lpServEnt; // Service information structure
short nProtocolPort; // Protocol port
int nConnect; // Socket connection results SOCKET hServerSocket = INVALID_SOCKET; // Default socket number if (WSAStartup(WINSOCK_VERSION, &wsaData))
MessageBox(NULL, "Could not load Windows Sockets DLL.",
PROG_NAME, MB_OK|MB_ICONSTOP);
else // Resolve the host name
{
lpHostEnt = gethostbyname(HOST_NAME);
if (!lpHostEnt)
MessageBox(NULL, "Could not get IP address!", HOST_NAME,
MB_OK|MB_ICONSTOP);
else // Create the socket
{
hServerSocket = socket(PF_INET, SOCK_STREAM,
DEFAULT_PROTOCOL);
if (hServerSocket == INVALID_SOCKET)
MessageBox(NULL, "Invalid socket!!", PROG_NAME,
MB_OK|MB_ICONSTOP);
else // Configure the socket
{
// Get the time service information
lpServEnt = getservbyname("mail", DEFAULT_PROTOCOL);
if (lpServEnt == NULL)
nProtocolPort = htons(IPPORT_SMTP);
else
nProtocolPort = lpServEnt->s_port; // Define the socket address
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = nProtocolPort;
sockAddr.sin_addr = *((LPIN_ADDR)*lpHostEnt->h_addr_list);
// Connect the socket
nConnect = connect(hServerSocket, (PSOCKADDR)&sockAddr,
sizeof(sockAddr));
if( nConnect)
{
MessageBox(NULL, "Error connecting socket!!",
PROG_NAME, MB_OK|MB_ICONSTOP);
hServerSocket = INVALID_SOCKET;
}
}
}
}
return(hServerSocket);
}int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
SOCKET hSocket; // Socket handle for the SMTP connection hSocket = ConnectMailServerSocket(); if (hSocket != INVALID_SOCKET)
{
SendMail(hSocket);
closesocket(hSocket);
}
WSACleanup();
return(NULL);
}