Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Authenticated SMTP

0.00/5 (No votes)
24 Apr 2012 1  
C++ class to authenticate an SMTP server connection

Introduction

This code is a C++ class derived from the ATL class CSMTPConnection. It adds LOGIN authentication It also allows specifying a custom SMTP port.

Background

The base class is described in detail here.

SMTP negotiating is described here.

Using the Code

Create a Windows project in Visual Studio 2005 and make sure you add ATL support. Create a CAuthSMTPConnection variable and use the Connect method. CMimeMessage is already defined in the ATL libraries and the base class is already set to handle it.

Sample Code

CAuthSMTPConnection SMTP(587,"smtp.server.com","username","password");
if (SMTP.Connect())
{
	// Create the text email message
	CMimeMessage msg;
	msg.SetSubject(csTFTDHdr);
	msg.SetSender("me@somewhere.com");
	msg.SetSenderName("Me");
	msg.AddRecipient("you@somewhere.com");
	msg.AddRecipient("them@somewhere.com");
	msg.AddText("Use CAuthSMTPConnection to authenticate SMTP!");

	// Send the email message
	if (SMTP.SendMessage(msg))
		AfxMessageBox("Sent Mail");
	else
		AfxMessageBox("Mail not sent");
}
else
{
	AfxMessageBox("Can't connect to mail server");
}

History

  • 11/10/09 - Article created
  • 11/11/09 - Cleaned up some insecure code
  • 04/24/12 - Added support for VS2008

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here