Question

I am looking to create an SMTP that allows for file attachments, however, I am finding it difficult to locate a tutorial. I have found one close thing, which is https://stackoverflow.com/questions/58210/c-smtp-example , however, this fails to compile in VS2005/2010 because of the include files that are used.

I would like to roll my own and not adapt to some library such as Curl or Boost. Does anyone have any suggestions on how to do this, or some small sample code with good documentation that will compile in Visual Studio?

Was it helpful?

Solution

Building on my answer to a previous question, using the Microsoft ATL classes which are included in the paid versions of Visual Studio.

CSMTPConnection smtp;
if (!smtp.Connect(m_strEmailServer)) 
    return false; 
// start generating the email message; remember to call CoInitialize somewhere in the app before this 
CMimeMessage msg; 
msg.SetSubject(m_strSubject); 
msg.SetSender(m_strSender); 
// repeat the following as necessary 
msg.AddRecipient(strSingleRecipient); 
msg.AddText(m_strBody); 

// add an attachment
msg.AttachFile(m_strAttachmentPath, m_strAttachmentName, _T("application/octet-stream"));

if (!smtp.SendMessage(msg)) 
    return false; 
return true; 

The MIME type supplied in the AttachFile call will depend on the type of attachment.

OTHER TIPS

There are a couple of ways to do attachments. You can use UUEncoded or MIME formatting. The UUEncoded is much simpler if you want to roll your own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top