SMTPクライアント(ソケット通信)

ソケット通信によりSMTPサーバへアクセスし,メールを送信する例題です.今回の例題では,自家用のサーバ(ArGoSoft Mail Server: Win XP)によりメールを送信しています.SMTPサーバはそれぞれの環境により,適時変更してください.

ソースのダウンロード

SMTPクライアント (ソケット通信)(list_86.c)
#include <stdio.h>
#include "smtp_lib.h"

#define SMTP_SERVER "localhost"

int main(int argc,char* argv[])
{
    int s,rbyte;
    char from[256],to[256];
    char buf[BUFSIZ],title[BUFSIZ],body[BUFSIZ];

    if((s=smtp_open(SMTP_SERVER)) < 0)
        return 0;

    if(argc < 2){
        printf("From: "); gets(from);
    }
    if(argc < 3){
        printf("To: ");   gets(to);
    }
    printf("Subject: ");  gets(title);
    printf("Body:\n");
    strcpy(body,"");
    while((rbyte=read(0,buf,BUFSIZ)) > 0){
        // 改行コードの設定
        buf[rbyte-1] = '\r'; buf[rbyte] = '\n'; buf[rbyte+1] = '\0';
        strcat(body,buf);
        strcpy(buf,"");
    }
    smtp_request(s,     // ソケット
                 from,  // 送信元
                 to,    // 送信先
                 title, // タイトル 
                 body); // 本文
    printf("\nSMTP transmission ended.\n");
    close(s);
    return 0;
}

実行結果
Gami[209]% ./list_86.exe
From: A.Murakami
To: A_gamyl@hotmail.com
Subject: test mail
Body:
input mail body
end of mail.        --> Ctrl+D によりメール本文の終了
220 ArGoSoft Mail Server Freeware, Version 1.8 (1.8.7.8)
250 Sender "A.Murakami" OK...
250 Connection from trusted IP address.  Recipient OK
354 Enter mail, end with "." on a line by itself
250 Message accepted for delivery. <13ok3xq6uylm7kf.080620050126@medlanc>

SMTP transmission ended.
Gami[210]% 
inserted by FC2 system