jok*_*ker 10 c sockets smtp winsock send
我刚刚开始学习socket编程并了解了winsock并取得了一些进展.我的问题基本上是:我想发送电子邮件,我该怎么办?
要点:
这是我到目前为止阅读的页面的链接:
基本winsock指南:http://msdn.microsoft.com/en-us/library/windows/desktop/ms737629( v = vs.85).aspx
我已经阅读了beej guide的前14页(无法发布链接,新用户最多只能发布两个超链接)
我已经了解了类型(WSADATA
,addrinfo structure
,sockaddr
,SOCKET
)和功能(WSAStartup()
,WSACleanup()
,getaddrinfo()
,Shutdown()
,WSAGetLastError()
,socket()
,...)
我刚刚开始阅读这篇关于http://www.faqs.org/rfcs/rfc821.html的文章SMTP
这是我到现在所写的:
#include <stdio.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "Ws2_32.lib") // Applications that use Winsock must be linked with the Ws2_32.lib library file.
#define HTTP_PORT "80"
#define SMTP_PORT "25"
#define HOSTNAME_PORT "101"
/*
All ports and web services names ( which are string aliases of the ports
can be found here:
%WINDIR%\system32\drivers\etc\services
*/
int main(void)
{
WSADATA wsdata;
int iresult, retval; //iresult : instant result
SOCKET connect_socket;
struct addrinfo *result, *ptr, hints;
iresult = WSAStartup(MAKEWORD(2, 2), &wsdata);
if(iresult != 0) printf("Initiation of Winsock succeeded.\n");
else
{
printf("WinSock initialization failed..\n");
WSACleanup();
return 0;
}
if(LOBYTE(wsdata.wVersion) == 2 && HIBYTE(wsdata.wVersion) == 2) printf("winsock.dll is found.\n");
else
{
printf("Can not find the required winsock.dll file.\n");
WSACleanup();
return 0;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints.ai_protocol = IPPROTO_TCP; // TCP connection ( full duplex )
hints.ai_socktype = SOCK_STREAM; // Provides sequenced, reliable, two-way, connection-based byte streams
connect_socket = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
if(connect_socket == INVALID_SOCKET)
{
printf("Socket Creation failed..\n");
WSACleanup();
return 0;
}
else printf("Socket Creation Succeeded ..\n");
WSACleanup();
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我离开了吗?
您应该通过 telnet查看一些有关 smtp 的示例:)
基本上,您需要以纯文本形式输入如下内容:
HELO local.domain.name
MAIL FROM: mail@domain.ext
RCPT TO: mail@otherdomain.ext
DATA
...
Run Code Online (Sandbox Code Playgroud)
根据这个例子编辑,你的代码应该是:
// Not sure about this one, maybe just "\n"
#define SEPARATOR "\n\r"
int sendData( Socket *socket, const char *data) {
int iResult;
iResult = send(socket, data, (int) strlen(data), 0);
if(iResult == SOCKET_ERROR){
// Do error handling as you like
}
return iResult;
}
sendData( socket, "HELO local.doman.name" SEPARATOR);
sendData( socket, "MAIL FROM: mail@domain.ext" SEPARATOR);
sendData( socket, "RCPT TO: mail@otherdomain.ext" SEPARATOR);
sendData( socket, "DATA" SEPARATOR);
sendData( socket, "This is subject of my mail" SEPARATOR SEPARATOR);
sendData( socket, "And this is text" SEPARATOR);
sendData( socket, "." SEPARATOR); // Send mail
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14454 次 |
最近记录: |