SMTPClient.cpp
资源名称:FindMail.zip [点击查看]
上传用户:feituo2008
上传日期:2013-02-02
资源大小:493k
文件大小:3k
源码类别:
Email客户端
开发平台:
Visual C++
- #include "StdAfx.h"
- #include <stdio.h>
- #include "smtpclient.h"
- #include "tcp.h"
- #include "udp.h"
- #include "util.h"
- CSMTPClient::CSMTPClient(int num)
- : m_sd(-1)
- , m_port(25)
- {
- m_client_num =num;
- }
- CSMTPClient::~CSMTPClient(void)
- {
- Disconnect();
- }
- int CSMTPClient::ConnectHost(char * host, int port)
- {
- int ret =0;
- if(port !=25) m_port =port; if(host !=NULL) strcpy(m_host, host); Sleep(100*((m_client_num+1)%10)); if((m_sd =tcp_connect(m_host, m_port, 15, false)) <0) return -1; if(GetReply() !=SMTP_WELCOME) { tcp_close(m_sd); m_sd =-1; return -10; } int r; srand((unsigned int)time(NULL)+m_client_num); for(int i =0; i<9; i++) { r =rand()%(int)('z'-'a'); m_random_name[i] ='a' +r; } m_random_name[i] =0; for(i=0; i<4; i++) { r =rand()%(int)('z'-'d'); m_random_domain[i] ='d' +r; } m_random_domain[i] =0; strcat(m_random_domain, ".com"); if((ret =command("HELO %s", m_random_name)) != SMTP_OK)
- {
- tcp_close(m_sd);
- m_sd =-1;
- return -10;
- }
- if(command("RSET") !=SMTP_OK)
- {
- tcp_close(m_sd);
- m_sd =-1;
- return -1;
- }
- return m_sd;
- }
- int CSMTPClient::Login(char * user, char * password)
- {
- return 0;
- }
- int CSMTPClient::command(char * fmt, ...)
- {
- if(m_sd <0) return -1;
- char cmd[256];
- va_list args;
- va_start(args,fmt);
- vsprintf(cmd, fmt, args);
- va_end(args);
- if(cmd[strlen(cmd)-1] !='n')
- strcat(cmd, "rn");
- WriteStat("command:%s", cmd);
- if(tcp_send(m_sd, cmd, (int)strlen(cmd), 5) !=(int)strlen(cmd)) return -1; return GetReply();
- }
- int CSMTPClient::GetReply(void)
- {
- int len=0, len_recved =0; time_t t1, t2; int able =0, code=-1;
- m_reply[0] =0; time(&t1); while(1) { time(&t2); if(difftime(t2, t1) >60) { WriteStat("timeout ..."); break; // max wait time: 15 seconds } len =tcp_recv(m_sd, &m_reply[len_recved], 1, 1); //len =recv(m_sd, &m_reply[len_recved], 1, 0); if (len ==SOCKET_ERROR) { if(WSAGetLastError() ==WSAEWOULDBLOCK) { Sleep(100); continue; } WriteStat("error=%d", WSAGetLastError()); m_reply[len_recved] = 0; return -1; } if(len ==0) { WriteStat("len ==0, err=%d", WSAGetLastError()); break; } len_recved ++; m_reply[len_recved] =0; if(strstr(m_reply, "rn") != NULL) break; } WriteStat("reply:%s", m_reply); if(sscanf(m_reply, "%d", &code) !=1) return -1; return code; }
- int CSMTPClient::Disconnect(void)
- { if(m_sd >0) { //command("QUIT"); tcp_close(m_sd); m_sd =-1; } return 0; }
- int CSMTPClient::Rcpt(char * email_addr)
- {
- int ret;
- if((ret=command("RCPT TO: <%s>", email_addr)) !=SMTP_OK)
- return ret;
- return 0;
- }
- int CSMTPClient::From(char * email_addr)
- {
- int ret;
- if(email_addr ==NULL || email_addr[0] ==0) // if email_addr is null, then mail from random user
- {
- if((ret =command("MAIL FROM: <%s@%s>", m_random_name, m_random_domain)) !=SMTP_OK)
- return ret;
- }
- else if((ret =command("MAIL FROM: <%s>", email_addr)) !=SMTP_OK)
- return ret;
- return 0;
- }