Send an email using SMTP (source code)

zhaozj2021-02-08  223

Source File:

#include #pragma HDRSTOP

#include "sendmail.h" #include "mmsystem.h"

#pragma package (smart_init)

Struct recvemubject, strfrom, strs, strcontent, strcmddate, strcmdnum; int nrecnum, nsendcount; tlist * attachlist;

struct AttachInfo {AnsiString FileName; int Length; BYTE * Content;}; __ fastcall TSendMailThread :: TSendMailThread (bool CreateSuspended, int sleeptime): TThread (CreateSuspended) {Ftime = sleeptime; FreeOnTerminate = true; nSendFlag = -1; nCommandKind = 0; Strboundary = "---- mailor_of_qurqur.china ----";

Recvemaillist = new tlist;

/ * TMEMORYSTREAM * MS = new tmemorystream (); ms-> loadFromfile ("d: //test.txt"); recvEmailinfo * prei; attachinfo * pai; for (int i = 0; i <5; i ) {PAI = new attctachinfo; prei = new recvEmailinfo; pai-> filename = "test.txt"; pai-> length = ms-> size; pai-> content = new byte [ms-> size]; ms-> position = 0; ms-> readbuffer (pai-> content, ms-> size); prei-> attachlist = new tlist; prei-> attachlist-> add (pai); prei-> strsubject = "humor"; prei-> Strfrom = "qurQur@163.net"; prei-> strto = "qurqur@163.net; prei-> strcontent =" test "; prei-> nsendcount = 5; recvErets-> add (prei);} delete ms; * /} void __fastcall tsendmailthread :: execute () {Word WVersionRequested; Wsadata Wsadata; int Err

WVersionRequested = MakeWord (2, 2); Err = WSAStartup (WVersionRequested, & WSADATA); if (Err! = 0) {// Tell The User That We Could Not Find A Usable Winsock DLL. Return;} IF (Lobyte (WSADATA. WVersion)! = 2 || Hibyte (Wsadata.WVersion)! = 2) {/ Tell The User That We Could Not Find A Usable Winsock DLL. wsacleanup (); Return;} While (! Terminated) {if (! Bcansendemail ) {Sleep (Ftime); continue;} SendDeal (); Sleep (10);}} int __fastcall TSendMailThread :: InitSocket () {smtpclient = socket (AF_INET, SOCK_STREAM, 0); if (smtpclient == INVALID_SOCKET) {return -1;} Hostent * Hostinfo = gethostByName (strsmtpserver.c_str ()); if (HostInfo == Null) Return -1;

struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons (nSMTPPort); memcpy (& server.sin_addr, * hostinfo-> h_addr_list, sizeof (server.sin_addr)); int rval = connect (smtpclient, (struct sockaddr * ) & server, sizeof (server); if (rval == socket_error) {// rval = wsagetlasterror (); return -1;} return 0;} void __fastcall tsendmailthread :: seunddeal () {unsigned int new_time; char AA [ INT ERR, LEN; SWITCH (Nsendflag) {case -1: if (Haveemail ()) {Err = INITSOCKET (); if (err == -1) {nsendflag = 1;

STRCPY (BUF, "Error");

NCommandkind = 9;

} Else {strbegintime = now (). FormatString ("YYYY-MM-DD HH: MM: SS"); nsendflag = 0; ncommandkind = 0;}}} Break; Case 0: Len = Recv (SMTPCLIENT, BUF, 8192, 0); if (len == socket_error) // err {nsendflag = 1; ncommandkind = 9; Break;} buf [len] = 0; Nsendflag = 1; Break; case 1: switch (ncommandk) {casse 0: IF (ANSISTRING (BUF). Gunsipos ("220")! = 0) {// Connection success, send Helo Strcpy (BUF, "Helo"); strcat (buf, strsmtpserver.c_str ()); strcat (buf, "/ R / N "); if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {nCommandkind = 9;

STRCPY (BUF, "Error");

Break;} nsendflag = 0; nCommandkind = 2;} else {ncommandkind = 9;

STRCPY (BUF, "Error");} Break; Case 2: // is affected by HELO, authentication if (ANSISTRING (BUF) .ansipos ("250")! = 0) {STRCPY (BUF, "Auth Login" ); Strcat (buf, "/ r / n"); if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {nCommandkind = 9;

STRCPY (BUF, "Error"); Break;} nsendflag = 0; ncommandkind = 3;} else {nCommandkind = 9; strcpy (buf, "error");} Break; case 3: // Receive a responding user name IF ("334")! = 0) {structure (buf, eNCode_name.c_str ()); strcat (buf, "/ r / n"); if (Socket_ERROR == Send (SMTPClient, BUF) , strlen (buf), 0)) {nCommandkind = 9;

STRCPY (BUF, "Error"); Break;} nsendflag = 0; nCommandkind = 4;} else {structure (buf, "mail from:"); strcat (buf, fromaddress.c_str ()); strcat (buf, " / r / n "); if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {nCommandkind = 9;

STRCPY (BUF, "Error");

Break;} nsendflag = 0; ncommandkind = 6; // no auth} Break; case 4: // is received by the answer password if (ANSISTRING (BUF). GuniPOS ("334")! = 0) {struct (buf, eNCode_key. c_str ()); strcat (buf, "/ r / n"); if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {nCommandkind = 9;

STRCPY (BUF, "Error"); Break;} nsendflag = 0; ncommandkind = 5;} else {nCommandkind = 9; strcpy (buf, "error");} Break; case 5: // Answer Mail from if (Ansistring (BUF). Gunsipos ("235")! = 0) {STRCPY (BUF, "Mail from:"); strcat (buf, fromaddress.c_str ()); strcat (buf, "/ r / n"); if (Socket_error == Send (SMTPClient, BUF, Strlen (BUF), 0)) {ncommandkind = 9;

STRCPY (BUF, "Error"); Break;} nsendflag = 0; ncommandkind = 6;} else {nCommandkind = 9;

STRCPY (BUF, "Error");} Break; Case 6: // Answer RCPT to if (ANSISTRING (BUF). GunSipos ("250")! = 0) {STRCPY (BUF, "RCPT TO:"); strcat (BUF, RECV_TEMP-> STRTO.C_STR ()); strcat (buf, "/ r / n"); if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {nCommandkind = 9;

STRCPY (BUF, "Error"); Break;} nsendflag = 0; ncommandkind = 7;} else {ncommandkind = 9;

STRCPY (BUF, "Error");} Break; Case 7: // Answer Data IF (BUF) .ansipos ("250")! = 0) {struct (buf, "data / r / n"); STRCAT (BUF, "/ R / N"); if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {nCommandkind = 9; strcpy (buf, "error"); Break;} nsendflag = 0; ncommandkind = 8;} else {nCommandkind = 9;

STRCPY (BUF, "Error");} Break; Case 8: // Answer Content IF (BUF). Gunsipos ("354")! = 0) {STRCPY (BUF, "DATE:"); strcat (buf , Formatdatetime ("YYY-MM-DD HH: NN: SS", NOW ()) .c_str ()); strcat (buf, "/ r / nsubject:"); strcat (buf, recv_temp-> strsubject.c_str ))); Strcat (buf, "/ r / nfrom:"); strcat (buf, recv_temp-> strfrom.c_str ()); strcat (buf, "/ r / nto:"); strcat (buf, rv_temp-> STRTO.C_STR ()); STRCAT (BUF, "/ R / N"); if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {nCommandkind = 9;

STRCPY (BUF, "Error"); Break;} if (Recv_temp-> attachlist! = null && rec_temp-> attachlist-> count> 0) {struct (buf, "mime-version: 1.0 / r / n"); STRCAT (BUF, "Content-Type: Multipart / Mixed; / R / N"); STRCAT (BUF, "/ TBoundary = /"); strcat (buf, strboundary.c_str ()); strcat (buf, "/ "/ r / n / r / n"); // Space line strcat (buf, "this is a multi-part message in mime format./r/n/r/N"); // Space Line Strcat (BUF ("-" strboundary "/ r / n") .c_str ()); strcat (buf, "content-type: text / plain; / r / n / tcharset = /" gb2312 / "/ r / N "); strcat (buf," content-transfer-encoding: 7bit / r / n / r / n "); // Space line if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {NCommandkind = 9; strcpy (buf, "error"); Break;} if (SendMailText () == -1) // Send the message body failed {nCommandkind = 9;

STRCPY (BUF, "Error"); Break;} IF (sendattach () == -1) // Send mail attachment failed {nCommandkind = 9;

STRCPY (BUF, "Error"); Break;} STRCPY (BUF, ("/ R / N--" Strboundary "- / R / N") .c_str ()); if (socket_error == Send (Socket_ERROR == Send) SMTPCLIENT, BUF, STRLEN (BUF), 0)) {nCommandkind = 9; STRCPY (BUF, "ERROR"); Break;}} else {IF (sendmailText () == -1) // Send mail body failed {ncommandkind = 9;

STRCPY (BUF, "Error"); Break;}} STRCPY (BUF, "/R/N/R/N/r/N./R/N"); if (Socket_ERROR == Send (SMTPCLIENT, BUF, STRLEN (buf), 0)) {nCommandkind = 9;

STRCPY (BUF, "Error"); Break;} nsendflag = 0; ncommandkind = 9;} else {ncommandkind = 9;

STRCPY (BUF, "Error");} Break; Case 9: // Answer quit if (Ansistring (BUF) "! = 0) {fsuccess = 0; strENDTIME = now (). FormatString (" YYYY-MM-DD HH: MM: SS "); recvresult (); if (Haveemail ()) {strbegintime = now (). FormatString (" YYYY-MM-DD HH: MM: SS "); strcpy (buf, "Mail from:"); strcat (buf, fromaddress.c_str ()); strcat (buf, "/ r / n"); if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) { CloseSocket (SMTPCLIENT); NSENDFLAG = -1; Break;} nsendflag = 0; ncommandkind = 6;} else {structure (buf, "quit / r / n"); if (socket_err OR == Send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {CloseSocket (SMTPCLIENT); NsendFlag = -1; Break;} nsendflag = 0; ncommandkind = 10;}} else {fsuccess = 1; recvResult () CloseSocket (SMTPCLIENT); NSENDFLAG = -1; Sleep (ftime);} Break; Case 10: // Answer IF (ANSISTRING (BUF). GuiPOS ("221")! =

0) {SLEEP (ftime);} nsendflag = -1; break;} Break;}} BOOL __FASTCALL Tsendmailthread :: Haveemail () {if (recvEtrist-> count == 0) {RETURN FALSE;} else {rec_Temp = RecvEmailInfo *) RecvEmailList-> Items [0]; RecvEmailList-> Delete (0); return true;}} return false;} int __fastcall TSendMailThread :: SendMailText () {int pos, len; AnsiString strContent; while ((pos = Recv_temp-> strcontent.ansipos ("/ r")))! = 0) {len = Recv_temp-> strcontent.length (); strcontent = recv_temp-> strcontent.substring (1, pos); POS ; IF RECV_TEMP-> STRCONTENT [POS]! = '/ n') strcontent = "/ n"; recv_temp-> strcontent = recv_temp-> strcontent.substring (pOS, LEN - POS 1);} strcontent = recv_temp-> StrContent "/ r / n"; while ((pOS = strcontent.ansipos ("/ r/n./n"))))! = 0) {strcontent = strcontent.insert (".", POS 2);} len = 8192; Ansistring strsnd; while (len <= strcontent.length ()) {strsnd = strcontent.substring (1, len); StrContent = strcontent.substring (LEN 1, StrContent.Length () - POS); if (Socket_ERROR == Send (SMTPCLIENT, STRSEND.C_STR (), strsnd.length (), 0)) {Return -1;}} Len = strcontent.length (); if (len> 0) {if (socket_error ==

Send (SMTPCLIENT, STRCONTENT.C_STR (), LEN, 0)) {RETURN-1;}} return 0;} int __fastcall tsendmailthread :: sendattach () {if (Recv_temp-> attachlist == null) Return 0; tlist * List = Recv_temp-> attachlist; attachinfo * pai; int LEN, Last; char * chbuf; for (int i = 0; i count; i ) {PAI = (attachinfo *) list-> items [i] ; If (pai! = Null) {stracpy ("/ r / n--" strboundary "/ r / n") .c_str ()); strcat (buf, "content-type: Application / OcTet -stream / r / n "); STRCAT (BUF," Content-Transfer-Encoding: Base64 / R / N "); STRCAT (BUF," Content-Disposition: attachment; filename = / "); if (PAI- > Filename == "" "Pai-> filename =" attach " Ansistring (i); strcat (buf, (pai-> filename " / "/ r / n / r / n") .c_str ()); IF (Socket_ERROR == Send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {RETUR N -1;} len = pai-> length * 4/3 10; chbuf = new char [len]; encode_base64 (pai-> content, pai-> length, chbuf, len); Last = 0; While (LEN - Last> = 76) // 78Bytes per line {memcpy (buf, chbuf last, 76); Last = 76; BUF [76] = '/ r'; buf [77] = '/ n'; buf [ 78] = 0; if (socket_error ==

Send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {RETURN-1;}} Memcpy (BUF, CHBUF LAST, LEN - Last); BUF [LEN - LAST] = '/ R'; buf [len - Last 1] = '/ n'; buf [len - last 2] = 0; if (socket_error == send (SMTPCLIENT, BUF, STRLEN (BUF), 0)) {return -1;} delete [] Chbuf }}} Return 0;} void __fastcall tsendmailthread :: recvresult () {int num = Recv_temp-> nsendcount - 1; if (fsuccess == 0 | Num == 0) {synchronize (SAVESEND); if (Recv_temp-> Attachlist! = Null) {attachinfo * pai; while (recv_temp-> attachlist-> count> 0) {PAI = (attachinfo *) RECV_TEMP-> attachlist-> items [0]; if (pai! = Null) {delete [ ] pai-> content; delete pai;}} Delete rec_temp-> attachlist;} delete rec_temp; recv_temp = null; synchronize (refresh);} else {recv_temp-> nsendCount = Num; recvEmailist-> add (remote);}} header file:

#ifndef sendmailh # Define sendmailh # include

Struct recvEmailinfo; structure attachinfo;

class TSendMailThread: public TThread {private: int Ftime; // sleep time int nRecNum; TMemoryStream * SendBuf; AnsiString strBeginTime, strEndTime, strBoundary; AnsiString strSubject, strFrom, strTo, strContent, strCmdDate, strCmdNum; RecvEmailInfo * recv_temp; BYTE Fsuccess; / / = 0 success; = 1 failed; = 2 No socket SMTPClient; char buf [8200]; int nchamndkind; int nsendflag; tlist * recvEmaillist;

int __fastcall InitSocket (); void __fastcall SaveSend (); void __fastcall SendDeal (); void __fastcall DealWait (); void __fastcall RecvResult (); void __fastcall Refresh (); bool __fastcall HaveEmail (); int __fastcall SendMailText (); int __fastcall Sendattach (); protected: void __fastcall execute (); public: __fastcall tsendmailthread (Bool createSuspended, int Sleeptime);};

转载请注明原文地址:https://www.9cbs.com/read-1762.html

New Post(0)