Simple mail transfer protocol SMTP package class

zhaozj2021-02-08  292

Simple mail transfer protocol SMTP package class

Author: Asif Rasheed Translation: Liu Jianqiang on the Internet, Email is the most popular transmission media. This article consists of two protocols: POP3 protocol: POP3 protocol (Post Transport Protocol) means that the letter is received from the EMAIL server. I have submitted a class that encapsulates the POP3 protocol. The official description of this agreement You can consult RFC1225. SMTP protocol: SMTP (Simple Mail Transfer Protocol) refers to the destination of sending an email. Details of the SMTP protocol You can refer to RCF 821. I have the latest contribution to encapsulate SMTP protocols. I can't fully implement the SMTP protocol but you can use it to send emails in many applications. There are several ways, I choose the method name with the SMTP command, so you are easy to understand.

In this class, I once again used CSocket as a class member, so I can use it in the thread. Below is the encoding and comment of this class: // // SMTP.H: interface for the csmtp class. // / / #if! defined (AFX_SMTP_H__617F7E82_7F4D_11D1_88A0_00001C302581__INCLUDED_) #define AFX_SMTP_H__617F7E82_7F4D_11D1_88A0_00001C302581__INCLUDED_ #if _MSC_VER> = 1000 #pragma once #endif // _MSC_VER> = 1000 #define CONNECTION_CHECK 0 #define HELLO_CHECK 1 #define mAIL_CHECK 2 #define RCPT_CHECK 3 #define DATA_START_CHECK 4 #define DATA_END_CHECK 5 #define QUIT_CHECK 6 #define DATA_CHECK 7 class CSMTP {public: BOOL Mail (); // CString GetErrorMessage (); BOOL Data (CString Subject, CString Body); CString getTo (); BOOL SetTo (CString to); CString GetFrom (); void setfrom (cstring from); Bool Mail (CString from); Bool disconnect (); cstring gethost (); void setost (cstring host); Bool Connect (CString Host, CString Me); Bool Connect (); CSMTP (); Virtual ~ csmtp (); private: cstring getError (cstring response); cstring m_ERrorMessage; Bool CheckResponse (int type); int m_noofto; cstringArray m_to; cstring m _From; CSocket m_SMTPServer; CString m_Host;}; #endif // defined (AFX_SMTP_H__617F7E82_7F4D_11D1_88A0_00001C302581__INCLUDED_) // // SMTP.cpp:! Implementation of the CSMTP class // // #include "stdafx.h" #include "MailSend.h. "#include" SMTP.h "#ifdef _DEBUG #undef THIS_FILE static char THIS_FILE [] = __ FILE__; #define new DEBUG_NEW #endif // // Construction / Destruction // CSMTP :: CSMTP () {m_NoOfTo = 0; m_SMTPServer. Create ();} csmtp :: ~ csmtp () {m_smtpserver.close ();} // connect to the smtp server bool csmtp :: connect () {Return Connect (m_host, m_from);

} // Connect To the SMTP Server Bool Csmtp :: Connect (CString Host, CString from) {if (! M_smtpserver.connect (Host, 25)) // 25 for SMTP port {m_errorMessage = _t ("Server Cannot Be Connected" ); return false;} else {if (CHECKRESPONSE (CONNECTION_CHECK) == false) Return False; char buf [512]; WSPrintf (buf, "helo% s / r / n", (lpcstr) from); m_smtpserver.send (BUF, STRLEN (BUF)); IF (Hello_Check) == false) Return False; Else Return True; Return True;}} // setting the host string void csmtp :: sethost (cstract host) {m_host = Host } // Returing the host string cstring csmtp :: gethost () {return m_host;} // Send "Quit" command to SMTP server: BOOL CSMTP :: Disconnect () {char Buf [256]; WSPrintf (buf, " QUIT / R / N "); m_smtpserver.send (BUF, Strlen (BUF)); if (checkresponse (quit_check) == false Return False;} // Send" Mail "command to SMTP server: BOOL CSMTP :: Mail (CString from) {Char Buf [256]; WSPrintf (buf, "mail from: <% s> / r / n", (lpcstr) from); m_smtpserver.send (buf, strlen (buf)) ; if (CheckResponse (Mail_ CHECK == false; else return;} // setting the from string void csmtp :: setfrom (cstring from) {m_from = from;} // Returing the from string cstring csmtp :: getFrom () {Return M_From;} // setting the to string bool csmtp :: setto (cstring to) {char Buf [256]; m_to.add (to); // saving value of to wsprintf (buf, "rcpt to: <% s> / r / n ", (lpcstr) to); m_smtpserver.send (buf, strlen (buf)); if (CheckResponse (rcpt_check) == false) Return False; Else Return True;} // Returing the To string CString CSMTP :: Getto () {if (m_to.geto) {m_noOfto ; return m_to [m_noOfto-1];

} Else Return_T ("no more to available");} // Send "DATA" command to SMTP server: BOOL CSMTP :: Data (CString Subject, CString Body) {charf [256]; WSPrintf (buf, "DATA / r / n "); m_smtpserver.send (buf, strlen (buf)); if (checkresponse (data_check) == false) Return False; Else {Wsprintf (buf," Subject:% S / R / N ", LPCSTR); m_smtpserver.send (BUF, Strlen (BUF)); WSPrintf (buf, "% s / r / n", (lpcstr) body); m_smtpserver.send (buf, strlen (buf)); WSPrintf BUF, "./r/N"); m_smtpserver.send (buf, strlen (buf)); return true;}} // This method Check the server's response: BOOL CSMTP :: CheckResponse (int type) {char Buf [ 1000]; CHAR TEMP [3]; for (int i = 0; i <512; i ) buf [i] = '/ 0'; // receive data M_SMTPSERVER.RECEIVE (BUF, SIZEOF (BUF)) from the server; STRNCPY (TEMP, BUF, 3); INT TEMP2 = ATOI (TEMP); Switch (TYPE) {CASE Connection_Check: IF (Temp2! = 220) {m_ERRORMESSAGE = GETERROR ((LPCTSTSTR) BUF); Return False;} Break; Case; Hello_Check: IF (Temp2! = 250) {m_errorMessage = getError ((lpctstr) buf); return false;} Break; Case Mail_Check: IF (Temp2! = 250) {m_errormes sage = GetError ((LPCTSTR) buf); return FALSE;} break; case RCPT_CHECK: if (temp2 = 250!) {m_ErrorMessage = GetError ((LPCTSTR) buf); return FALSE;} break; case DATA_START_CHECK: if (temp2! = 354) {m_ErrorMessage = GetError ((LPCTSTR) buf); return FALSE;} break; case DATA_END_CHECK: if (temp2 = 250)! {m_ErrorMessage = GetError ((LPCTSTR) buf); return FALSE;} break; case QUIT_CHECK: IF (Temp2! = 221) {m_errorMessage = getError (LPCTSTSTR) BUF); Return False;} Break; Case Data_Check: IF (Temp2! = 354) {m_ERrorMessage = GetError ((LPCTSTSTR) BUF); Return False;} Break } Return True;

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

New Post(0)