⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplesmtpclient.cpp

📁 邮件客户端程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		    SetLastError("Could not send \"RCPT\" line: ", oSocket.GetLastError());
		    return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
        }

	    // Receive server response
	    if (oSocket.ReceiveLine(strResponse) == SOCKET_ERROR)
	    {
		    SetLastError("Could not receive response: ", oSocket.GetLastError());
		    return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
	    }
        // Check server response: OK if response starts with status code 250
	    if (strResponse.Left(3) == "250")
	    {
            bAtLeastOneRecipientAccepted = TRUE;
        } else {
		    SetLastError("Recipient rejected: ", 0, strResponse);
        }
    }
    if (!bAtLeastOneRecipientAccepted) {
		SetLastError("All recipients rejected");
		oSocket.Send("QUIT\r\n");
		return(HRESULT_FROM_WIN32(WSANO_RECOVERY));
    }
	// Send "DATA" line (i.e. everything that's not in the header)
    if (oSocket.Send("DATA\r\n") == SOCKET_ERROR) {
		SetLastError("Could not send \"DATA\" line: ", oSocket.GetLastError());
		return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
    }

	// Receive server response
	if (oSocket.ReceiveLine(strResponse) == SOCKET_ERROR)
	{
		SetLastError("Could not receive response: ", oSocket.GetLastError());
		return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
	}
    // Check server response: OK if response starts with status code 354
	if (strResponse.Left(3) != "354")
	{
		SetLastError("DATA command rejected: ", 0, strResponse);
		oSocket.Send("QUIT\r\n");
		return(HRESULT_FROM_WIN32(WSANO_RECOVERY));
	}

    // ===================
    // START RFC822 HEADER
    // ===================

	// Build and Send "Subject" line
	strCommand = "Subject: ";
	strCommand += lpszSubject;
	strCommand += "\r\n";
    if (oSocket.Send(strCommand) == SOCKET_ERROR) {
		SetLastError("Could not send \"Subject\" header line: ", oSocket.GetLastError());
		return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
    }

	// Build and Send "From:"/"To:" lines (this may contain a full RFC822 address)
	strCommand = "From: ";
	strCommand += lpszFrom;
	strCommand += "\r\n";
    if (oSocket.Send(strCommand) == SOCKET_ERROR) {
		SetLastError("Could not send \"From\" header line: ", oSocket.GetLastError());
		return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
    }
	strCommand = "To: ";
	strCommand += lpszTo;
	strCommand += "\r\n";
    if (oSocket.Send(strCommand) == SOCKET_ERROR) {
		SetLastError("Could not send \"To\" header line: ", oSocket.GetLastError());
		return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
    }

    // ==============================
    // END RFC822 HEADER - BEGIN BODY
    // ==============================
	strCommand = "\r\n"; // Separator between header and body
    if (oSocket.Send(strCommand) == SOCKET_ERROR) {
		SetLastError("Could not send header/body separator line: ", oSocket.GetLastError());
		return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
    }

    // Send the body
	if (oSocket.Send(szSMTPBody) == SOCKET_ERROR)
	{
		SetLastError("Could not send body: ", oSocket.GetLastError());
		return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
	}

	// No response from server expectd

	// Send the termination line
	oSocket.Send("\r\n.\r\n");

	// and check the response
	if (oSocket.ReceiveLine(strResponse) == SOCKET_ERROR)
	{
		SetLastError("Could not receive response: ", oSocket.GetLastError());
		return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
	}
	if (strResponse.Left(3) != "250")
	{
		SetLastError("Message body rejected: ", 0, strResponse);
		oSocket.Send("QUIT\r\n");
		return(HRESULT_FROM_WIN32(WSANO_RECOVERY));
	}

	// Send the "QUIT" line
    if (oSocket.Send("QUIT\r\n") == SOCKET_ERROR) {
		SetLastError("Could not send \"QUIT\" line: ", oSocket.GetLastError());
		return(HRESULT_FROM_WIN32(oSocket.GetLastError()));
    }

    return(hReturnCode);
}

CString CSimpleSMTPClient::BuildRFC822Address(LPCTSTR lpszAddress)
{
    CString szRFC822Address = lpszAddress;

    // If address is raw, i.e. no '(', ')', '<' or '>', then we add surrounding '<' '>' to the address
    if (szRFC822Address.Find('(') == -1 &&
        szRFC822Address.Find('<') == -1) {
        szRFC822Address.Insert(0, "<");
        szRFC822Address+=">";
    }
    
    return(szRFC822Address);
}

CString CSimpleSMTPClient::ExtractSMTPAddress(LPCTSTR lpszAddress)
{
    CString szSMTPAddress = lpszAddress;
    int nPos = -1;

    nPos = szSMTPAddress.Find('<');
    if (nPos != -1) {
        szSMTPAddress.Delete(0, nPos);
    }
    nPos = szSMTPAddress.Find('>');
    if (nPos != -1) {
        szSMTPAddress.Delete(nPos, szSMTPAddress.GetLength()-nPos-1);
    }
    
    return(szSMTPAddress);
}

BOOL CSimpleSMTPClient::BuildListFromString(LPCTSTR lpszStringWithSeparators, CStringList &oList)
{
    CString szStringWithSeparators = lpszStringWithSeparators;
    TCHAR cSeparator = ',';
    int nPos = -1;
    oList.RemoveAll();
    int nCurrentPos = 0;
    int nCurrentLength = 0;
    CString szCurrentItem;

    nPos = szStringWithSeparators.Find(cSeparator);
    if (nPos == -1) {
        // Try semi-colon
        cSeparator = ';';
        nPos = szStringWithSeparators.Find(cSeparator);
    }
    while (nPos != -1) {
        nCurrentLength = nPos - nCurrentPos;
        szCurrentItem = szStringWithSeparators.Mid(nCurrentPos, nCurrentLength);
        // Remove starting/trailing spaces
        szCurrentItem.TrimLeft(" ");
        szCurrentItem.TrimRight(" ");
        if (szCurrentItem != "") {
            TRACE1("Adding string \"%s\"\n", szCurrentItem);
            oList.AddTail(szCurrentItem);
        }
        nCurrentPos = nPos+1;
        nPos = szStringWithSeparators.Find(cSeparator, nCurrentPos);
    }
    // Add last item
    szCurrentItem = szStringWithSeparators.Mid(nCurrentPos);
    // Remove starting/trailing spaces
    szCurrentItem.TrimLeft(" ");
    szCurrentItem.TrimRight(" ");
    if (szCurrentItem != "") {
        TRACE1("Adding string \"%s\"\n", szCurrentItem);
        oList.AddTail(szCurrentItem);
    }
    

    return(!oList.IsEmpty());
}

CString CSimpleSMTPClient::BuildStringFromList(CStringList &oList, LPCTSTR lpszSeparator)
{
    POSITION pos = oList.GetHeadPosition();
    CString szStringWithSeparators;
    CString szItem;
    szStringWithSeparators = "";

    while (pos!=NULL) {
        szItem = oList.GetNext(pos);
        if (szStringWithSeparators.GetLength()) {
            szStringWithSeparators+=lpszSeparator;
        }
        szStringWithSeparators+=szItem;
    } 

    return(szStringWithSeparators);
}

BOOL CSimpleSMTPClient::GetHostName(CString &szFullyQualifiedHostName)
{
    BOOL bReturnCode = FALSE;
    TCHAR lpszComputerName[MAX_COMPUTERNAME_LENGTH + 1];

    strcpy(lpszComputerName, "");
    if (gethostname(lpszComputerName, sizeof(lpszComputerName))==0) {
        // Get a fully qualified name
        hostent *pHostEnt = gethostbyname(lpszComputerName);
        if (pHostEnt) {
            szFullyQualifiedHostName = pHostEnt->h_name;
            bReturnCode = TRUE;
        } else {
            bReturnCode = FALSE;
        }
    } else {
        bReturnCode = FALSE;
    }

    return(bReturnCode);
}

BOOL CSimpleSMTPClient::SetLastError(LPCTSTR lpszErrorMessage, DWORD dwLastError, LPCTSTR lpszSMTPResponse)
{
    m_szLastErrorMessage = lpszErrorMessage;
    if (dwLastError != 0) {
        TCHAR lpszSystemErrorText[1024];
        m_szLastErrorMessage+=WindowsGetErrorText(dwLastError, lpszSystemErrorText, sizeof(lpszSystemErrorText));
    }
    if (lpszSMTPResponse) {
        m_szLastErrorMessage+=lpszSMTPResponse;
    }
    TRACE1("%s\n", m_szLastErrorMessage);
    return(TRUE);
}

CString CSimpleSMTPClient::GetLastErrorMessage()
{
    return(m_szLastErrorMessage);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -