C3Lab研究室

Back to Title Page(OpenLab)タイトルページへ戻る

最終更新日: 1998年 11月 02日 月曜日

*Network Software構築シリーズ3 RFC1522拡張ヘッダ&1000バイトを越える本文を送信

今回拡張した内容

RFC1522拡張ヘッダーに対応し、日本語での受信者名をToに追加する
1000バイトを越えるメッセージ本体の送信に対応する

 今回の仕様変更関数で、SendBody関数がquoted-printableフォーマットで1000バイト を越えるメッセージ本体を自動分割して送信するように変更しました。

 今後メールクライアントアプリケーションに拡張するには、BASE64を使用した ファイルの添付機能などいろいろ付け足す必要がありますが、基本機能としての送信はこ れで十分でしょう。
 このソース中にBASE64への文字列変換関数も含めていますが、これはまだ完全に 検証はすんでいません。必要な方はこれを基本にしてデバッグしてみてください。もしか したらそのまま動くかもしれませんが...

 今回のこのシリーズではあえてISO-2022-JPを採用しませんでした。理由は、

ISO-2022-JPでは半角カタカナに対応できない
quoted-printableフォーマットは初期に定義されたため多くのEMailサーバーが対応している
SJISからの変換プログラムを別途組まなければならないためプログラム効率が悪い

 ということです。ただ、ISO-2022-JPの利点も多くありますので、メールクライアント作成 時には対応させる必要があるかもしれません。

 SendMailMessage関数の変更点。

 今回の変更に合わせて若干拡張しました。特に、最後のメンバーで CProgressCtrl クラスのポインターを参照するように変更しました。このクラスはプログレスバーに対して進行状態を知らせる機能を持たせました。

 

CSock クラスを使用したサンプル関数 : SendMailMessage

BOOL SendMailMessage(CString szServer,
    CString szFrom,
    CString szToName,
    CString szTo,
    CString szSubject, CString szMessage,BOOL bSilent = FALSE,CProgressCtrl *cp = NULL){
   
    CSock cSock;
    CString strResponse;

    if(cp!=NULL)cp->SetRange(0,8);
    if(cp!=NULL)cp->SetPos(1);

    if (!cSock.Create(szServer, 25))
    {
        if(bSilent==FALSE)     AfxMessageBox("Could not connect to server");
        return FALSE;
    }
    // Read response
    if (cSock.Receive(&strResponse) == SOCKET_ERROR)
    {
        if(bSilent==FALSE)     cSock.ReportError(cSock.GetLastError());
        return FALSE;
    }
    if (strResponse.Left(3) != _T("220"))
    {
        CString strError = "ERROR: SMTP サーバーから正常な応答が返ってきませんでした\r\n";
        strError += strResponse;
        if(bSilent==FALSE)     AfxMessageBox(strError);
        cSock.SendQuit();
        return FALSE;
    }
    if(cp!=NULL)cp->SetPos(2);

    cSock.SendHelo(szFrom);    // Send "HELO"
    if (cSock.Receive(&strResponse) == SOCKET_ERROR)
    {
        if(bSilent==FALSE)     cSock.ReportError(cSock.GetLastError());
        return FALSE;
    }
    if (strResponse.Left(3) != _T("250"))
    {
        CString strError = "ERROR: 送信に失敗しました(送信者エラー)\r\n";
        strError += strResponse;
        if(bSilent==FALSE)     AfxMessageBox(strError);
        cSock.SendQuit();
        return FALSE;
    }

    //
    if(cp!=NULL)cp->SetPos(3);
    cSock.SendFrom(szFrom);    // Send "FROM"
    if (cSock.Receive(&strResponse) == SOCKET_ERROR)
    {
        if(bSilent==FALSE)     cSock.ReportError(cSock.GetLastError());
        return FALSE;
    }
    if (strResponse.Left(3) != _T("250"))
    {
        CString strError = "ERROR: 送信に失敗しました(送信者エラー)\r\n";
        strError += strResponse;
        if(bSilent==FALSE)     AfxMessageBox(strError);
        cSock.SendQuit();
        return FALSE;
    }

    if(cp!=NULL)cp->SetPos(4);


    cSock.SendTo(szToName,szTo);    // Send "RCPT"
    if (cSock.Receive(&strResponse) == SOCKET_ERROR)
    {
        if(bSilent==FALSE)     (cSock.GetLastError());
        return FALSE;
    }
    if (strResponse.Left(3) != _T("250"))
    {
        CString strError = "ERROR: 送信エラーが発生しました\r\n";
        strError += strResponse;
        if(bSilent==FALSE)     AfxMessageBox(strError);
        cSock.SendQuit();
        return FALSE;
    }

    if(cp!=NULL)cp->SetPos(5);

    cSock.SendData();    // Send the "DATA" line
    if (cSock.Receive(&strResponse) == SOCKET_ERROR)
    {
        if(bSilent==FALSE)     cSock.ReportError(cSock.GetLastError());
        return FALSE;
    }
    if (strResponse.Left(3) != _T("354"))
    {
        CString strError = "ERROR: 送信エラーが発生しました(データ)\r\n";
        strError += strResponse;
        if(bSilent==FALSE)     AfxMessageBox(strError);
        cSock.SendQuit();
        return FALSE;
    }

    if(cp!=NULL)cp->SetPos(6);

    cSock.SendMIME             (1,0);     // Send MIME 1.0
    cSock.SendContentType    ();         // Send Content type
    cSock.SendEncodeType    ();         // Send Content Encode Type to Quoted-printable
    cSock.SendXMailer         ("Sample Mail Send Soft");     // Send Mail soft
    cSock.SendSubject         (szSubject);         // Send the "Subject"

    // Message Body
    if (cSock.SendBody(szMessage) == SOCKET_ERROR)
    {
        if(bSilent==FALSE)cSock.ReportError(cSock.GetLastError());
        return FALSE;
    }
    cSock.SendTermination();    // Send the termination

    if(cp!=NULL)cp->SetPos(7);

    if (cSock.Receive(&strResponse) == SOCKET_ERROR)
    {
        if(bSilent==FALSE) cSock.ReportError(cSock.GetLastError());
        return FALSE;
    }
    if (strResponse.Left(3) != _T("250"))
    {
        CString strError = "ERROR: 送信エラーが発生しました(メッセージ)\r\n";
        strError += strResponse;
        if(bSilent==FALSE)     AfxMessageBox(strError);
        cSock.SendQuit();
        return FALSE;
    }
    if(cp!=NULL)cp->SetPos(8);
    cSock.SendQuit();
    if(cp!=NULL)cp->SetPos(0);
    return TRUE;
}

クラスの定義リスト : CSock



#ifndef __CLASS_CSOCK__
#define __CLASS_CSOCK__


class CSock : public CSocket {
    private:
    public:
        int Create(LPCTSTR szServer,int nPort,UINT nSocketPort = 0, int nSocketType = SOCK_STREAM, LPCTSTR lpszSocketAddress = NULL);
        int Create(CString sServer ,int nPort,UINT nSocketPort = 0, int nSocketType = SOCK_STREAM, LPCTSTR lpszSocketAddress = NULL);
    public:
        int Send(CString s,int nFlags=0);
        int Send(LPCTSTR s,int nFlags=0);
    public:    //
        int Receive(CString *s,int nFlags = 0);
        void ReportError(int nError);
    public: // 文字列変換
        CString ConvertQuoted(CString szMessage); // quoted-printable文字列に変換
        CString ConvertQuoted(LPCTSTR szMessage); // quoted-printable文字列に変換
        CString EncodeBASE64(CString szMessage); // BASE64 文字列変換
        CString EncodeBASE64(LPCTSTR szMessage);

    //----------------------------------------------------
    // Send SMTP Command Messages
    public:
        int SendHelo(LPCTSTR szHelo,int nFlags = 0);
        int SendHelo(CString szHelo,int nFlags = 0);
        int SendFrom(LPCTSTR szFrom,int nFlags = 0);
        int SendFrom(CString szFrom,int nFlags = 0);
        int SendTo    (LPCTSTR szTo,int nFlags = 0);
        int SendTo    (CString szTo,int nFlags = 0);
        int SendTo    (LPCTSTR szToName,LPCTSTR szTo,int nFlags = 0);
        int SendTo    (CString szToName,CString szTo,int nFlags = 0);
        int SendData(int nFlags = 0);
        int SendSubject(LPCTSTR szSubject,int nFlags=0);
        int SendSubject(CString szSubject,int nFlags=0);
        int SendMIME(int nMajor,int nMinor,int nFlags=0);
        int SendContentType(int nFlags=0);                     //      text/plain;
        int SendContentType(LPCTSTR szType,int nFlags=0);
        int SendContentType(CString szType,int nFlags=0);
        int SendXMailer(LPCTSTR szXMailer,int nFlags=0);
        int SendXMailer(CString szXMailer,int nFlags=0);
        int SendEncodeType(int nFlags=0);                     //      quoted-printable
        int SendEncodeType(LPCTSTR szType,int nFlags=0);
        int SendEncodeType(CString szType,int nFlags=0);
        int SendBody(CString s,int nFlags=0);
        int SendBody(LPCTSTR s,int nFlags=0);

        int SendTermination(int nFlags = 0);
        int SendQuit(int nFlags = 0);
};
//---------------------------------------------------------------
CString CSock::EncodeBASE64(CString szMessage){ // BASE64 文字列変換
    return( EncodeBASE64( LPCTSTR(szMessage) ) );
}
CString CSock::EncodeBASE64(LPCTSTR szMessage){
    char bIndex[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    CString s,s2;
    int k,i,n;
    char *buf;
    s2 = szMessage;
    while( ( s2.GetLength() %3)!=0 ) s2+="=";
    n = s2.GetLength();
    buf = (char*)malloc( n + 4 );
    ZeroMemory(buf,n+4);
    memcpy(buf,LPCTSTR(s2),n);
    s="";
    for(k=n,i=0;i<n;i+=3){
        __int32 d;
        char b[2];
        b[1]=0;
        memcpy(&d,buf+i,3);
        { b[0] = bIndex[(d&0x3f)]; d>>=6; s+=b; k--; }
        { b[0] = bIndex[(d&0x3f)]; d>>=6; s+=b; k--; }
        { b[0] = bIndex[(d&0x3f)]; d>>=6; s+=b; k--; }
        { b[0] = bIndex[(d&0x3f)]; d>>=6; s+=b; k--; }
    }
    free(buf);
    return s;
}

//---------------------------------------------------------------
CString CSock::ConvertQuoted(LPCTSTR szMessage){
    CString s = szMessage;
    return( ConvertQuoted(s) );
}
CString CSock::ConvertQuoted(CString szMessage){
    CString s;
    char *buf;
    char bf[8];
    int i,n;
    s = "";
    n = szMessage.GetLength();
    buf = (char*)malloc( n+20 );
    strcpy(buf,LPCTSTR(szMessage) );
    for(i=0;i<n;i++){
        int b;
        b = (int)(buf[i] & 0x00ff);
        if( (b<33)||(b>127)||(b==61) ){
            s += "=";
            wsprintf(bf,"%.2x",b);
            s += bf;
        }else{
            wsprintf(bf,"%c",b);
            s += bf;
        }
    }
    free(buf);
    return s;
}
//---------------------------------------------------------------

int CSock::Create(CString sServer,int nPort,UINT nSocketPort, int nSocketType, LPCTSTR lpszSocketAddress){
    return( Create( LPCTSTR(sServer),nPort,nSocketPort,nSocketType,lpszSocketAddress) );
}

int CSock::Create(LPCTSTR szServer,int nPort,UINT nSocketPort, int nSocketType, LPCTSTR lpszSocketAddress){
    int n=CSocket::Create(nSocketPort,nSocketType,lpszSocketAddress);
    if(n==0)return n;
    return( CSocket::Connect(szServer,nPort) );
}

void CSock::ReportError(int nError){
    CString strError;
    strError.LoadString(nError);
    AfxMessageBox(strError);
}

int CSock::Receive(CString *s,int nFlags){
    static char buf[1024];
    ZeroMemory(buf,1024);
    int n = CSocket::Receive(buf,1024,nFlags);
    *s = buf;
    return n;
}

//------------------------------------------

int CSock::SendTermination(int nFlags){
    return ( Send("\r\n.\r\n") );
}
//------------------------------------------
int CSock::SendEncodeType(int nFlags){
    return(SendEncodeType("quoted-printable") );
}

int CSock::SendEncodeType(LPCTSTR szType,int nFlags)
{
    CString strCommand = "Content-Transfer-Encoding: ";
    strCommand += szType;
    strCommand += "\r\n";
    return( Send(strCommand,nFlags) );
}
int CSock::SendEncodeType(CString szType,int nFlags)
{
    CString strCommand = "Content-Transfer-Encoding: ";
    strCommand += szType;
    strCommand += "\r\n";
    return( Send(strCommand,nFlags) );
}
//------------------------------------------

int CSock::SendMIME(int nMajor,int nMinor,int nFlags){
    CString strCommand = "MIME-Version: ";
    char buf[80];
    wsprintf(buf,"%d.%d",nMajor,nMinor);
    strCommand += buf;
    strCommand += "\r\n";
    return( Send(strCommand,nFlags) );
}
//------------------------------------------
int CSock::SendContentType(int nFlags){
    return( SendContentType("text/plain;") );
}
int CSock::SendContentType(LPCTSTR szType,int nFlags)
{
    CString strCommand = "Content-Type: ";
    strCommand += szType;
    strCommand += "\r\n";
    return( Send(strCommand,nFlags) );
}
int CSock::SendContentType(CString szType,int nFlags)
{
    CString strCommand = "Content-Type: ";
    strCommand += szType;
    strCommand += "\r\n";
    return( Send(strCommand,nFlags) );
}
//------------------------------------------
int CSock::SendXMailer(LPCTSTR szXMailer,int nFlags)
{
    CString strCommand = "X-Mailer: <";
    strCommand += szXMailer;
    strCommand += ">\r\n";
    return( Send(strCommand,nFlags) );
}
int CSock::SendXMailer(CString szXMailer,int nFlags)
{
    CString strCommand = "X-Mailer: <";
    strCommand += szXMailer;
    strCommand += ">\r\n";
    return( Send(strCommand,nFlags) );
}

//------------------------------------------
int CSock::SendSubject(LPCTSTR szSubject,int nFlags)
{
    CString strCommand = "Subject: =?ISO-8859-1?Q?";
    strCommand += ConvertQuoted(szSubject);
    strCommand += "?=\r\n";
    return( Send(strCommand,nFlags) );
}
int CSock::SendSubject(CString szSubject,int nFlags)
{
    return( SendSubject(LPCTSTR(szSubject),nFlags) );
}
//------------------------------------------

int CSock::SendTo(LPCTSTR szTo,int nFlags)
{
    CString strCommand = "RCPT TO:<";
    strCommand += szTo;
    strCommand += ">\r\n";
    return( Send(strCommand,nFlags) );
}
int CSock::SendTo(LPCTSTR szToName,LPCTSTR szTo,int nFlags)
{
    CString strCommand = "RCPT TO: ";
    strCommand += "=?ISO-8859-1?Q?";
    strCommand += ConvertQuoted(szToName);
    strCommand += "?=";

    strCommand += "<";
    strCommand += szTo;
    strCommand += ">\r\n";
    return( Send(strCommand,nFlags) );
}

int CSock::SendTo(CString szTo,int nFlags)
{
    return( SendTo(LPCTSTR(szTo),nFlags) );
}
int CSock::SendTo(CString szToName,CString szTo,int nFlags)
{
    return( SendTo(LPCTSTR(szToName),LPCTSTR(szTo),nFlags) );
}
//---------------------------------------------
int CSock::SendFrom(LPCTSTR szFrom,int nFlags)
{
    CString strCommand = "MAIL FROM:<";
    strCommand += szFrom;
    strCommand += ">\r\n";
    return( Send(strCommand,nFlags) );
}
int CSock::SendFrom(CString szFrom,int nFlags)
{
    return( SendFrom( LPCTSTR(szFrom),nFlags) );
}
//---------------------------------------------
int CSock::SendHelo(LPCTSTR szHelo,int nFlags)
{
    CString strCommand = "HELO ";

    if( strchr(szHelo,'@')==0)    strCommand += szHelo;
    else strCommand += (strchr(szHelo,'@')+1);
    strCommand += "\r\n";
    return( Send(strCommand,nFlags) );
}
int CSock::SendHelo(CString szHelo,int nFlags)
{
    return( SendHelo( LPCTSTR(szHelo),nFlags) );
}
//---------------------------------------------
int CSock::SendData(int nFlags){
    return( Send("DATA\r\n",nFlags) );
};

int CSock::SendQuit(int nFlags){
    return( Send("QUIT\r\n",nFlags) );
};

//---------------------------------------------
int CSock::SendBody(CString s,int nFlags){
    return( SendBody(LPCTSTR(s),nFlags) );
};
int CSock::SendBody(LPCTSTR src,int nFlags){
    CString s;
    char buf[514];
    LPCTSTR s2;
    s = ConvertQuoted(src);
    s2 = LPCTSTR(s);
    long i,j,n,k,ret;
    j=n = s.GetLength();
    for(i=0;i<n;i+=k){
        k = j; if(j>10) k=10;
        ZeroMemory(buf,514);
        memcpy(buf,s2+i,k);
        ret = CSocket::Send(buf,strlen(buf),nFlags);
    }
    return ret;
};
//---------------------------------------------
int CSock::Send(CString s,int nFlags){
    return( Send(LPCTSTR(s),nFlags) );
};
int CSock::Send(LPCTSTR s,int nFlags){
    return( CSocket::Send(s,strlen(s),nFlags) );
};

#endif // __CLASS_CSOCK__

Copyright (C) Kitaro 1998