Thread: Email attachment via smtp - how?!

  1. #1

    Question Email attachment via smtp - how?!

    I'm writing a keylogger function and need to email the log to the admin-specified email address programmatically.

    Sending a simple text message via smtp is easy enough, but how do you attach a file to an email message? I can't seem to find any examples. If you have any knowledge, please fill me in.

    Thanks a bunch.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Let's say you want to send a simple text file (details.txt) as an attachment. You could use this:

       // send these:
       // HELO 
       // MAIL FROM: 
       // RCPT TO: 
       // DATA
       // now send MIME-version
       sprintf( buf, "MIME-Version: 1.0" );
       send( socket, buf );
       // Send the Content-Type: multipart/mixed;
       sprintf( buf, "Content-Type: multipart/mixed;" );
       send( socket, buf );
       // send the Content-Transfer-Encoding: quoted-printable 
       sprintf( buf, "Content-Transfer-Encoding: quoted-printable" );
       send( socket, buf );
       // send the Content-Type: text/plain; 
       sprintf( buf, "Content-Type: text/plain;" );
       send( socket, buf );
       // send the name="details.txt" 
       sprintf( buf, "    name=\"details.txt\" " );
       send( socket, buf);
       // send the Content-Disposition: attachment; 
       sprintf( buf, "Content-Disposition: attachment;" );
       send( socket, buf);
       // send a blank line first !
       sprintf( buf, "\r\n" );
       send( socket, buf);
       // now, get the attachment contents and send them
       // line by line.
       MailFilePtr = fopen( AttachmentFile, "r" );
       if ( MailFilePtr == NULL )
           // handle error
       memset( FileBuffer, 0, sizeof(FileBuffer) );
       while ( fgets (FileBuffer, sizeof( FileBuffer), MailFilePtr) )
          sprintf( buf, "%s", FileBuffer );
          buf[strlen(buf)- 1] = 0;
          send( socket, buf );
          memset( FileBuffer, 0, sizeof(FileBuffer) );
          memset( buf, 0, sizeof(buf) );
       fclose( MailFilePtr );
       sprintf( buf, "\r\n");
       send( socket, buf );
    This should work. Hope this helps.
    quote="discussions are forgotten, only code remains"/>

  3. #3

    Thumbs up

    Thanks a bunch Ruchikar. Big help.

    Just needed to know what commands to send. I also just found an example that uses 'boundary'

    I appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C code for Sending Email with attachment
    By Mr coder in forum C Programming
    Replies: 5
    Last Post: 03-06-2009, 08:15 AM
  2. Spam Filters. ISP based email is dying.
    By Mario F. in forum A Brief History of
    Replies: 14
    Last Post: 03-05-2008, 12:05 PM
  3. SMTP with Apache/PHP on Windows
    By Mario F. in forum Tech Board
    Replies: 4
    Last Post: 02-29-2008, 09:24 AM
  4. Open Source Email Server!
    By andhikacandra in forum Tech Board
    Replies: 2
    Last Post: 10-02-2007, 10:51 PM
  5. SMTP Server Not Working
    By (TNT) in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-15-2003, 05:33 AM