Archived

This topic is now archived and is closed to further replies.

JonW

Question for SMTP nerds

Recommended Posts

JonW    173
I am making an application that will send e-mails via SMTP. I have everything working, except sending attachments. I'm using WinSock, and there is a severe lack of code on how to do this without MFC. I tried sending a JPEG as a test, and the message had the JPEG attached when I recieved it but the image didn't show up when I opened the attachment. Here is my code:
    
// Send the subject
wsprintf (szMsgLine, "Subject: %s\r\n", szSubject);
send (hServer, szMsgLine, strlen (szMsgLine), 0);


// Send MIME data for attaching things
strcpy (szMsgLine, "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"XXXXMESSAGEBOUNDARYXXXX\"\r\n\r\n");
send (hServer, szMsgLine, strlen (szMsgLine), 0);


// Send the message
strcpy (szMsgLine, "--XXXXMESSAGEBOUNDARYXXXX\r\nContent-Type: text/plain\r\n\r\n");
send (hServer, szMsgLine, strlen (szMsgLine), 0);
strcpy (szMsgLine, szMessage);
strcat (szMsgLine, "\r\n\r\n");
send (hServer, szMsgLine, strlen (szMsgLine), 0);


// Read in the jpeg file
HANDLE file1, file2;
char *szFile;


file1 = CreateFile ("puppy.jpg", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0);


file2 = CreateFileMapping (file1, NULL, PAGE_READONLY, 0, 0, 0);


if (!file2)
{
	CloseHandle (file1);
	return FALSE;
}


szFile = (char *)MapViewOfFile (file2, FILE_MAP_READ, 0, 0, 0);


if (!szFile)
{
	CloseHandle (file2);
	CloseHandle (file1);
	return FALSE;
}


// Send the attachment
strcpy (szMsgLine, "--XXXXMESSAGEBOUNDARYXXXX\r\n");
send (hServer, szMsgLine, strlen (szMsgLine), 0);
strcpy (szMsgLine, "Content-Type: application/octet-stream; name=\"puppy.jpg\"\r\n");
send (hServer, szMsgLine, strlen (szMsgLine), 0);
strcpy (szMsgLine, "Content-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"puppy.jpg\"\r\n\r\n");
send (hServer, szMsgLine, strlen (szMsgLine), 0);


DWORD dwSize = GetFileSize (file1, NULL);
send (hServer, szFile, dwSize, 0);


strcpy (szMsgLine, "--XXXXMESSAGEBOUNDARYXXXX--\r\n");
send (hServer, szMsgLine, strlen (szMsgLine), 0);


UnmapViewOfFile (szFile);
CloseHandle (file2);
CloseHandle (file1);


// Send blank line and a period to signify the end of the data
strcpy (szMsgLine, "\r\n.\r\n");
send (hServer, szMsgLine, strlen (szMsgLine), 0);
    
I think that I need to MIME encode the attachments. Is that correct? Thanks! [Edited by - JonW on July 13, 2006 12:40:31 PM]

Share this post


Link to post
Share on other sites
JonW    173
But why not :)

Thanks for the link. It was very enlightening. In case anyone's interested, you can find out how to encode/decode here:

http://www.codeproject.com/internet/mimesniffer.asp

I managed to use the general ideas for my own project.


[Edited by - JonW on July 13, 2006 12:22:44 PM]

Share this post


Link to post
Share on other sites