#include "io.h" #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include "winsock2.h" /* WSAGetLastError, WSAStartUp */ #define snprintf _snprintf static void sendmail_write( const int sock, const char *str, const char *arg ) { char buf[4096]; if (arg != NULL) snprintf(buf, sizeof(buf), str, arg); else snprintf(buf, sizeof(buf), str); send(sock, buf, strlen(buf), 0); } static int sendmail( const char *from, const char *to, const char *subject, const char *body, const char *hostname, const int port ) { struct hostent *host; struct sockaddr_in saddr_in; int sock = 0; WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { return -1; } sock = socket(AF_INET, SOCK_STREAM, 0); host = gethostbyname(hostname); saddr_in.sin_family = AF_INET; saddr_in.sin_port = htons((u_short)port); saddr_in.sin_addr.s_addr = 0; memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length); if (connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1) { return -2; } sendmail_write(sock, "HELO %s\n", from); // greeting sendmail_write(sock, "MAIL FROM: %s\n", from); // from sendmail_write(sock, "RCPT TO: %s\n", to); // to sendmail_write(sock, "DATA\n", NULL); // begin data // next comes mail headers sendmail_write(sock, "From: %s\n", from); sendmail_write(sock, "To: %s\n", to); sendmail_write(sock, "Subject: %s\n", subject); sendmail_write(sock, "\n", NULL); sendmail_write(sock, "%s\n", body); // data sendmail_write(sock, ".\n", NULL); // end data sendmail_write(sock, "QUIT\n", NULL); // terminate close(sock); return 0; } int main(int argc, char *argv[]) { int ret = sendmail( "testas@domainname.com", // from "Null665@gmail.com", // to "Subject", "body", "smtp.google.com", 25 ); if (ret != 0) fprintf(stderr, "Failed to send mail (code: %i).\n", ret); else fprintf(stdout, "Mail successfully sent.\n"); return ret; }
C:>nslookup
>set type=mx
>domain.com
C:>telnet mail.domain.com 25
220 shinbiro.com ESMTP Sendmail 8.13.8/8.13.8; Thu, 30 Jun 2011 15:36:47 +0900
helo
250 mail.domain.com Hello [210.96.xxx.xxx]
mail from: sender@gmail.com
250 2.1.0 sender@gmail.com....Sender OK
rcpt to: receiver@gmail.com
250 2.1.5 receiver@gmail.com
data
354 Start mail input; end with .
subject: telnet mail test <- 엔터 두 번
telnet mail test
.
250 2.6.0 <6ZYX20mgq00000002@mail.domain.com> Queued mail for delivery
quit
221 2.0.0 mail.domain.com Service closing transmission channel
>set type=mx
>domain.com
C:>telnet mail.domain.com 25
220 shinbiro.com ESMTP Sendmail 8.13.8/8.13.8; Thu, 30 Jun 2011 15:36:47 +0900
helo
250 mail.domain.com Hello [210.96.xxx.xxx]
mail from: sender@gmail.com
250 2.1.0 sender@gmail.com....Sender OK
rcpt to: receiver@gmail.com
250 2.1.5 receiver@gmail.com
data
354 Start mail input; end with .
subject: telnet mail test <- 엔터 두 번
telnet mail test
.
250 2.6.0 <6ZYX20mgq00000002@mail.domain.com> Queued mail for delivery
quit
221 2.0.0 mail.domain.com Service closing transmission channel
KT(메가패스)- kornet.net ※인증필요
하나로통신(하나)-mail.hanafos.com ※인증필요
두루넷(thrunet)-smtp.korea.com ※인증필요
신비로(샤크)-mail.shinbiro.com
드림라인-mail.hanafos.com
하이텔(Hitel)-smtp.hitel.net
천리안(Chollian)-mail.chollian.net
LG U+(보라홈넷)-smtp.borahome.net
아이넷(I-net)-mail.nuri.net
나우누리(Nownuri)-mail.nownuri.net
넷츠고(Netsgo)-netsgo.com
채널아이(Channel-i)-mail.channeli.net
엘림넷(Elimnet)-elim.net
인터피아(Interpia)-pop3.interpia.net
키스코(Kisco)-203.233.144.1
미래텔(Miraetel)-miraetel.kornet.nm.kr
경기넷-mail.kg21.net
에듀넷(Edunet)-edunet4u.net
하이텔(Hitel)-smtp.hitel.net