/*************************************************************
* smtp.cpp - Use SMTP to send an eMail with an Attachment and verify *
* Copyright (C) 2001-2002 by ShadowStar. *
* Use and modify freely. *
* http://shadowstar.126.com/ *
*************************************************************
*/
//---------------------------------------------------------------------------
#include <winsock2.h>
#include <string.h>
#include <stdio.h>
const int BASE64_MAXLINE = 76;
const char EOL[] = " ";
const char BASE64_TAB[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz0123456789+/";
const char HEADER[] =
"HELO support.com "
//"AUTH LOGIN " //+ BASE64 USER + BASE64 PASS
"MAIL FROM: chinansl@support.com "
"RCPT TO: shadowstar@support.com "
"DATA "
"FROM: chinansl@support.com "
"TO: shadowstar@support.com "
"SUBJECT: this is a test "
"Date: 2002-5-14 "
"X-Mailer: shadowstar's mailer "
"MIME-Version: 1.0 "
"Content-type: multipart/mixed; boundary="#BOUNDARY#" "
//"Content-Type: text/plain; charset=gb2312 "
" ";
const char CONTENT[] =
" --#BOUNDARY# "
"Content-Type: text/plain; charset=gb2312 "
"Content-Transfer-Encoding: quoted-printable "
" "
"/*************************************************************"
" * smtp.cpp - Use SMTP to send an eMail with an Attachment and verify *"
" * Copyright (C) 2001-2002 by ShadowStar. *"
" * Use and modify freely. *"
" * http://shadowstar.126.com/ *"
" *************************************************************"
" */ "
" ";
const char ATT_HEADER[] =
" --#BOUNDARY# "
"Content-Type: application/octet-stream; name=smtp.exe "
"Content-Disposition: attachment; filename=smtp.exe "
"Content-Transfer-Encoding: base64 "
" ";
//---------------------------------------------------------------------------
int ANSIToBase64(const char *szInANSI, int nInLen, char *szOutBase64, int nOutLen);
int main(int argc, char* argv[])
{
WSADATA wsaData;
int SockFD;
struct sockaddr_in ServAddr;
char buf[0x100];
int x;
FILE *fp;
char *aatt = new char[0x400000];
char *batt = new char[0x555556];
WSAStartup(MAKEWORD(2,2), &wsaData);
LPHOSTENT pHost = gethostbyname("172.16.234.111");
SockFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ServAddr.sin_family = AF_INET;
ServAddr.sin_addr.s_addr = *(ULONG *)pHost->h_addr_list[0];
ServAddr.sin_port = htons(25);
connect(SockFD, (struct sockaddr *)&ServAddr, sizeof(ServAddr));
//send HEADER
send(SockFD, HEADER, strlen(HEADER), 0);
//send CONTENT
send(SockFD, CONTENT, strlen(CONTENT), 0);
//send ATT_HEADER
send(SockFD, ATT_HEADER, strlen(ATT_HEADER), 0);
//read attachment
fp = fopen(argv[0], "rb");
fseek(fp, 0, 2);
x = ftell(fp);
if (x > 0x400000)
x = 0;
rewind(fp);
fread(aatt, x, 1, fp);
fclose(fp);
x = ANSIToBase64(aatt, x, batt, 0x555556);
//send base64 attachment
send(SockFD, batt, x, 0);
send(SockFD, ". ", strlen(". "), 0); //end
send(SockFD, "QUIT ", strlen("QUIT "), 0); //quit
closesocket(SockFD);
WSACleanup();
delete []aatt;
delete []batt;
return 0;
}
//---------------------------------------------------------------------------
int ANSIToBase64(const char *szInANSI, int nInLen, char *szOutBase64, int nOutLen)
{
//Input Parameter validation
if ((szInANSI == NULL) || (nInLen == 0) || (szOutBase64 == NULL) || (nOutLen == 0))
return 0;
if (nOutLen < (nInLen*4/3 + 1 + nInLen*4/3/BASE64_MAXLINE*2 + 1 + 4))
return 0;
//Set up the parameters prior to the main encoding loop
int nInPos = 0;
int nOutPos = 0;
int nLineLen = 0;
int c1, c2, c3;
int i;
// Get three characters at a time from the input buffer and encode them
for (i=0; i<nInLen/3; ++i)
{
//Get the next 2 characters
c1 = szInANSI[nInPos++] & 0xFF;
c2 = szInANSI[nInPos++] & 0xFF;
c3 = szInANSI[nInPos++] & 0xFF;
//Encode into the 4 6 bit characters
szOutBase64[nOutPos++] = BASE64_TAB[c1 >> 2];
szOutBase64[nOutPos++] = BASE64_TAB[((c1 << 4) | (c2 >> 4)) & 0x3F];
szOutBase64[nOutPos++] = BASE64_TAB[((c2 << 2) | (c3 >> 6)) & 0x3F];
szOutBase64[nOutPos++] = BASE64_TAB[c3 & 0x3F];
nLineLen += 4;
//Handle the case where we have gone over the max line boundary
if (nLineLen > BASE64_MAXLINE - 4)
{
szOutBase64[nOutPos++] = EOL[0];
szOutBase64[nOutPos++] = EOL[1];
nLineLen = 0;
}
}
// Encode the remaining one or two characters in the input buffer
switch (nInLen % 3)
{
case 0:
{
szOutBase64[nOutPos++] = EOL[0];
szOutBase64[nOutPos++] = EOL[1];
break;
}
case 1:
{
c1 = szInANSI[nInPos] & 0xFF;
szOutBase64[nOutPos++] = BASE64_TAB[(c1 & 0xFC) >> 2];
szOutBase64[nOutPos++] = BASE64_TAB[((c1 & 0x03) << 4)];
szOutBase64[nOutPos++] = '=';
szOutBase64[nOutPos++] = '=';
szOutBase64[nOutPos++] = EOL[0];
szOutBase64[nOutPos++] = EOL[1];
break;
}
case 2:
{
c1 = szInANSI[nInPos++] & 0xFF;
c2 = szInANSI[nInPos] & 0xFF;
szOutBase64[nOutPos++] = BASE64_TAB[(c1 & 0xFC) >> 2];
szOutBase64[nOutPos++] = BASE64_TAB[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
szOutBase64[nOutPos++] = BASE64_TAB[((c2 & 0x0F) << 2)];
szOutBase64[nOutPos++] = '=';
szOutBase64[nOutPos++] = EOL[0];
szOutBase64[nOutPos++] = EOL[1];
break;
}
default:
{
return 0;
}
}
szOutBase64[nOutPos] = 0;
return nOutPos;
}
自由广告区 |
分类导航 |
邮件新闻资讯: IT业界 | 邮件服务器 | 邮件趣闻 | 移动电邮 电子邮箱 | 反垃圾邮件|邮件客户端|网络安全 行业数据 | 邮件人物 | 网站公告 | 行业法规 网络技术: 邮件原理 | 网络协议 | 网络管理 | 传输介质 线路接入 | 路由接口 | 邮件存储 | 华为3Com CISCO技术 | 网络与服务器硬件 操作系统: Windows 9X | Linux&Uinx | Windows NT Windows Vista | FreeBSD | 其它操作系统 邮件服务器: 程序与开发 | Exchange | Qmail | Postfix Sendmail | MDaemon | Domino | Foxmail KerioMail | JavaMail | Winwebmail |James Merak&VisNetic | CMailServer | WinMail 金笛邮件系统 | 其它 | 反垃圾邮件: 综述| 客户端反垃圾邮件|服务器端反垃圾邮件 邮件客户端软件: Outlook | Foxmail | DreamMail| KooMail The bat | 雷鸟 | Eudora |Becky! |Pegasus IncrediMail |其它 电子邮箱: 个人邮箱 | 企业邮箱 |Gmail 移动电子邮件:服务器 | 客户端 | 技术前沿 邮件网络安全: 软件漏洞 | 安全知识 | 病毒公告 |防火墙 攻防技术 | 病毒查杀| ISA | 数字签名 邮件营销: Email营销 | 网络营销 | 营销技巧 |营销案例 邮件人才:招聘 | 职场 | 培训 | 指南 | 职场 解决方案: 邮件系统|反垃圾邮件 |安全 |移动电邮 |招标 产品评测: 邮件系统 |反垃圾邮件 |邮箱 |安全 |客户端 |