2016-05-15 1 views
-1

C++ WINSOCK(TCP、SSL)でメールを送信します。私はVisual Studio Express 2015を使用しています。私は最終的に自分に自動アラートを電子メールで送ることを望んでいます。私はGmailアカウントを使用しています。私はこれをうまく動作させることができないようです。私はデータを送信できますが、受信できません。サーバーがエラー10054で接続を終了しています(エラーが何であるかについてはthis pageで読むことができます)。は、私はC++で自分自身に短く、プレーンテキストの電子メールを送信し日間しようとしてきた

ここは歴史です:私は多くのS.O.投稿とMSDNの記事。私はこのコードで機能的なWSASetSocketSecurityセクションを持っていましたが、何らかの理由で私の接続の試行がタイムアウトしていたので省略しました。この時点で、EHLOまたはHELOを送信すると、サーバーが接続を切断しないように解決します。

私は続行する方法のための損失で本当によ。探検の日、読んだ記事の数十、死んだ何百もの終わり。私は、あなたがごみコードのいくつかのビットを許して、そしてS.O.私の手の整列を取り除いた。見て、私が間違っていることを教えてください、私に不適切なスタイル、またはあなたの良いコーダーの感性を傷つける何かを知らせてください。どうもありがとう。

#include "stdafx.h" 

#include <exception> 
#include <string> 
#include <iostream> // In-out stream objects for development 
#include <stdio.h> // Standard in-out for development 

#include <winsock2.h> // For making socket connection to email server 
#include <Mstcpip.h> 
#include <Ws2tcpip.h> // Enhanced protocols to assist winsock2.h 

#pragma comment(lib, "Ws2_32.lib") // Library for winsock2.h 
#pragma comment(lib, "Fwpuclnt.lib") // Library for winsock2.h 

#define BUFFER_SIZE 512 

using namespace std; 
void cleanup(SOCKET ConnectSocket, struct addrinfo *result) { 
    if (ConnectSocket != INVALID_SOCKET) { 
     closesocket(ConnectSocket); 
    } 
    freeaddrinfo(result); 
    WSACleanup(); 
    cout << "socket closed" << endl; 
    cin.get(); // Development only 
} 

int _tmain(int argc, char* argv[]) { 
    // Initialize email parameters 
    char   bccAddresses[64] = ""; 
    char   fromAddress[64] = "[email protected]"; 
    char   msg[512] = "Hello world!"; 
    char   port[12] = "465"; 
    char   serverName[64] = "smtp.host.com"; 
    char   toAddresses[64] = "[email protected]"; 
    SOCKET   ConnectSocket = INVALID_SOCKET; 
    struct addrinfo *result = NULL; 
    struct addrinfo *ptr = NULL; 
    struct addrinfo hints; 
    WSADATA   wsaData; 

    try { 
     // Initialize Winsock 
     int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); 
     if (iResult == SOCKET_ERROR) { 
      cout << "Error " << iResult << endl; 
      throw std::exception("WSAStartup failed\n"); 
     } 
     cout << "WSAStartup successful: " << iResult << endl; 

     // Set up the hints socket address structure 
     ZeroMemory(&hints, sizeof(hints)); 
     hints.ai_flags = AI_SECURE; 
     hints.ai_family = AF_INET; 
     hints.ai_socktype = SOCK_STREAM; 
     hints.ai_protocol = IPPROTO_TCP; 

     // Resolve the server address and port 
     iResult = getaddrinfo(serverName, port, &hints, &result); 
     if (iResult == SOCKET_ERROR) { 
      cout << "Error " << WSAGetLastError() << endl; 
      throw std::exception("getaddrinfo failed\n"); 
     } 
     cout << "getaddrinfo successful: " << iResult << endl; 

     // Connect to the socket 
     ptr = result; 
     ConnectSocket = WSASocket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol, NULL, 0, 0); 
     if (ConnectSocket == INVALID_SOCKET) { 
      cout << "Error " << WSAGetLastError() << endl; 
      throw std::exception("Error at socket\n"); 
     } 
     cout << "WSASocket successful: " << iResult << endl; 

     // Connect via the socket 
     iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); 
     if (iResult == SOCKET_ERROR) { 
      cout << "Error " << WSAGetLastError() << endl; 
      closesocket(ConnectSocket); 
      ConnectSocket = INVALID_SOCKET; 
      throw std::exception("Unable to connect to server!\n"); 
     } 
     cout << "connect successful: " << iResult << endl; 

     // Send message 
     char sendBuffer[BUFFER_SIZE] = "What is your malfunction"; 
     char recvBuffer[BUFFER_SIZE]; 

     sprintf_s(sendBuffer, BUFFER_SIZE, "EHLO %s%s", serverName, "\r\n"); 
     iResult = send(ConnectSocket, sendBuffer, BUFFER_SIZE, 0); 
     if (iResult == SOCKET_ERROR) { 
      cout << "Error " << WSAGetLastError() << endl; 
      throw std::exception("WINSOCK send failed\n"); 
     } 
     cout << "Sent:\n" << sendBuffer << "Byte count: " << iResult << endl; 

     iResult = recv(ConnectSocket, recvBuffer, BUFFER_SIZE, 0); 
     if (iResult == SOCKET_ERROR) { 
      cout << "Error " << WSAGetLastError() << endl; 
      throw std::exception("WINSOCK recv failed\n"); 
     } 
     cout << "EHLO response: " << iResult << endl; 
     sprintf_s(sendBuffer, BUFFER_SIZE, "QUIT%s", "\r\n"); 
     iResult = send(ConnectSocket, sendBuffer, BUFFER_SIZE, 0); 
     if (iResult == SOCKET_ERROR) { 
      cout << "Error " << WSAGetLastError() << endl; 
      throw std::exception("WINSOCK send failed\n"); 
     } 
     cout << "Sent:\n" << sendBuffer << "Byte count: " << iResult << endl; 
     iResult = recv(ConnectSocket, recvBuffer, BUFFER_SIZE, 0); 
     if (iResult < 0) { 
      cout << "Error " << WSAGetLastError() << endl; 
      throw std::exception("WINSOCK recv failed\n"); 
     } 
     cout << "Quit response: " << iResult << endl; 

     // Shutdown the connection 
     iResult = shutdown(ConnectSocket, SD_SEND); 
     if (iResult == SOCKET_ERROR) { 
      cout << "Error " << WSAGetLastError() << endl; 
      throw std::exception("shutdown failed\n"); 
     } 
     // Clean up 
     cleanup(ConnectSocket, result); 
     return 0; 
    } 
    catch (std::exception err) { 
     printf(err.what()); 
     cleanup(ConnectSocket, result); 
     return 1; 
    } 
    catch (...) { 
     printf("Unknown error\n"); 
     cleanup(ConnectSocket, result); 
     return 2; 
    } 
} 
+0

PS:後ほど、ログイン情報とハードコードを区別します。私が開発している間、ここには単純さのためにあります。 – GarrettML

+0

ああ、私は愚かです...私はすぐに私のソリューションを投稿します、私は願っています。 – GarrettML

答えて

0

この回答の3つの部分。

  1. MIMEは、ユーザー名とパスワードをエンコードします。
  2. 実際にユーザー名とパスワードを送信してください。 :)
  3. 一部のサーバーは懐疑的であるとあなたが二回HELOまたはEHLOを言いたいです。

私はすぐに完全なコードを掲載します。途中で私が約40%の気分だったと感じましたが、私はこのサイトに成功しました:http://www.coastrd.com/smtps

関連する問題