我發現下面的功能,可發送郵件:如何通過套接字發送結構?
bool sendmail(char * smtpserver, char * from, char * to, char * subject, char * msg)
{
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;
// Load command-line args
lstrcpyA(szSmtpServerName, smtpserver);
lstrcpyA(szToAddr, to);
lstrcpyA(szFromAddr, from);
// Attempt to intialize WinSock (1.1 or later)
if (WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
{
printf("\nCannot find Winsock v%d.%d or later", VERSION_MAJOR, VERSION_MAJOR);
return false;
}
// Lookup email server's IP address.
lpHostEntry = gethostbyname(szSmtpServerName);
if (!lpHostEntry)
{
printf("\nCannot find SMTP mail server %s", szSmtpServerName);
return false;
}
// Create a TCP/IP socket, no specific protocol
hServer = socket(PF_INET, SOCK_STREAM, 0);
if (hServer == INVALID_SOCKET)
{
printf("\nCannot open mail server socket!");
return false;
}
// 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)))
{
printf("\nError connecting to Server socket!");
return false;
}
// Receive initial response from SMTP server
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
// Send HELO server.com
sprintf_s(szMsgLine, "HELO %s%s", smtpserver, 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_s(szMsgLine, "MAIL FROM:<%s>%s", from, 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_s(szMsgLine, "RCPT TO:<%s>%s", to, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");
// Send DATA
sprintf_s(szMsgLine, "DATA%s", CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA");
// Send Subject
sprintf_s(szBuffer, "Subject: %s\n", subject);
Check(send(hServer, szBuffer, strlen(szBuffer), 0), "send() Subject");
//Send From
sprintf_s(szBuffer, "From: %s\n", from);
Check(send(hServer, szBuffer, strlen(szBuffer), 0), "send() From");
//Send To
sprintf_s(szBuffer, "To: %s\n\n", to);
Check(send(hServer, szBuffer, strlen(szBuffer), 0), "send() To");
//Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() Attachment");
sprintf_s(szMsgLine, "%s%s", msg, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line");
// Send blank line and a period
sprintf_s(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_s(szMsgLine, "QUIT%s", CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT");
// Close server socket and prepare to exit.
closesocket(hServer);
WSACleanup();
return true;
}
我想提出一個SQL查詢,我想將結果發送到電子郵件,但結果可能是多行。所以我不知道,我怎麼能一次發送整個結果。 SQL查詢的結果將存儲在一個結構中(但如果有人有更好的idee,那麼我在聽:))。所以我的問題是,如果有一種方法發送這個結構在電子郵件?或者,我怎樣才能發送我在電子郵件中得到的每一行?
謝謝!
你可以先解決你的'的sendmail()'函數,以便它可以發送郵件長度超過255個字符?一旦你完成了這些工作,其餘的工作就像將你的結構轉換成你想在電子郵件中發送的字符串格式一樣簡單。 – aroth
@aroth:所有我需要做的就是用大一號的聲明szMsgLine。那沒問題。但是,我如何發送我的結構一次?我應該只是連接線,那就是它? – kampi
@kampi - 是的,你有你的結構的內容轉換成C字符串轉換併發送這些字符串。這是因爲另一端的電子郵件處理程序不知道如何解釋你的結構。一般規則是結構在機器邊界上永遠不可移植。 –