0

SMTP 経由で電子メールを送信しようとしています。テキスト部分とアプリケーション/オクテット ストリーム部分を含むマルチパート メールを送信します。*.txt 以外のファイル (.jpg や .docx など) を送信しようとすると、破損して一部のバイトが失われます。たとえば、ファイル 123.docx を送信しようとすると、このファイルのサイズは 166,020 バイトになります。 . メールでファイルを受け取りましたが、166 006 しかなく、開くことができません. 変数 "total" は、正しいバイト数が送信されたことを示しています. 私のコードは次のとおりです:

#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;


int fileSize(char fileName[])
{
    std::streampos fsize = 0;

    std::ifstream myfile (fileName, ios::in);  // File is of type const char*

    fsize = myfile.tellg();         // The file pointer is currently at the beginning
    myfile.seekg(0, ios::end);      // Place the file pointer at the end of file

    fsize = myfile.tellg() - fsize;
    myfile.close();

    cout << "size is: " << fsize << " bytes.\n";
    return fsize;
}

int main() {
char text[2048];
//connecting to smtp server

    //sending DATA
    strcpy(text,"DATA\n");
    send(s,text,strlen(text),0);

    recv(s,text,sizeof(text),0);
    cout<<"recv - "<<text<<endl;

    //FROM field
    strcpy(text,"FROM: laboratory4_mm@rambler.ru\n");
    send(s,text,strlen(text),0);
    
    //TO field
    strcpy(text,"TO: ");
    strcat(text,reciever);
    strcat(text,"\n");
    send(s,text,strlen(text),0);

    // SUBJECT field 
    char subject[2048];
    cout<<"Enter the theme of the letter"<<endl;
    cin.getline(subject,2048);
    strcpy(text,"SUBJECT: ");
    strcat(text,subject);
    strcat(text,"\n");
    send(s,text,strlen(text),0);

    // delimeter of multipart message
    strcpy(text,"Content-Type: multipart/mixed; boundary=\"---nsabnqeaSA43ds2\"\n");
    send(s,text,strlen(text),0);

    //Text part
    strcpy(text,"-----nsabnqeaSA43ds2\nContent-Type: text/plain; charset=utf8\nContent-Transfer-Encoding: 8bit\n\n");
    send(s,text,strlen(text),0);

    cout<<"Enter the text:"<<endl;
    cin.getline(text,2048);
    send(s,text,strlen(text),0);

    //File part
    char fileName[256];
    cout<<"Enter file name: ";
    cin.getline(fileName,255);
    int size = fileSize(fileName);
    char fileLength[1024];
    itoa(size,fileLength,10);
    cout<<fileLength<<endl;
    strcpy(text,"\n-----nsabnqeaSA43ds2 \nContent-Type: application/octet-stream\nContent-Length: ");
    strcat(text,fileLength);
    strcat(text,"\nContent-Transfer-Encoding: binary\nContent-Disposition: attachment;\n\n");
    send(s,text,strlen(text),0);
    
    ifstream fin;
    fin.open(fileName,ios::binary);
    char *buf = new char[1024];
    int readBytes;
    int total =0;
    while((readBytes = fin.read(buf,1024).gcount())>0) {
         int sent= send(s,buf,readBytes,0);
         total+=sent;
         delete buf;
         buf = new char[1024];
    }
    fin.close();
    delete buf;
    cout<< total<<endl;

    strcpy(text,"\n-----nsabnqeaSA43ds2--\n");
    send(s,text,strlen(text),0);

// telling that DATA is over
    strcpy(text,"\n.\n");
    send(s,text,strlen(text),0);
    fout<<text;
    recv(s,text,sizeof(text),0);
    cout<<"recv - "<<text<<endl;

//Disconnecting from server
return 0;
}

4

1 に答える 1