Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / ATL

Authenticated SMTP

Rate me:
Please Sign up or sign in to vote.
3.73/5 (14 votes)
24 Apr 2012CPOL 57.7K   2.5K   21   19
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

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Suggestionneed call ::CoInitialize( NULL ) first Pin
raidsan16-Apr-15 17:40
raidsan16-Apr-15 17:40 
Question"250 AUTH" for authentication. Pin
Billy Yang29-Sep-14 17:32
Billy Yang29-Sep-14 17:32 
AnswerRe: "250 AUTH" for authentication. Pin
Andy Bantly30-Sep-14 7:19
Andy Bantly30-Sep-14 7:19 
GeneralMy vote of 5 Pin
Michael Haephrati3-Mar-13 5:34
professionalMichael Haephrati3-Mar-13 5:34 
GeneralMy vote of 5 Pin
maplewang24-Apr-12 19:44
maplewang24-Apr-12 19:44 
GeneralMy vote of 1 Pin
xComaWhitex24-Apr-12 4:23
xComaWhitex24-Apr-12 4:23 
GeneralRe: My vote of 1 Pin
Andy Bantly25-Apr-12 2:24
Andy Bantly25-Apr-12 2:24 
GeneralRe: My vote of 1 Pin
xComaWhitex25-Apr-12 8:42
xComaWhitex25-Apr-12 8:42 
Question[My vote of 1] Not an article Pin
Richard MacCutchan24-Apr-12 3:14
mveRichard MacCutchan24-Apr-12 3:14 
AnswerRe: [My vote of 1] Not an article Pin
Andy Bantly24-Apr-12 3:38
Andy Bantly24-Apr-12 3:38 
QuestionNow support VS2008 Pin
Andy Bantly24-Apr-12 2:12
Andy Bantly24-Apr-12 2:12 
AnswerRe: Now support VS2008 Pin
Joezer BH24-Aug-13 21:28
professionalJoezer BH24-Aug-13 21:28 
Questionatlsmtpconnection.h can't get included? Pin
Joan M23-Apr-12 21:03
professionalJoan M23-Apr-12 21:03 
AnswerRe: atlsmtpconnection.h can't get included? Pin
Andy Bantly24-Apr-12 1:48
Andy Bantly24-Apr-12 1:48 
GeneralMy vote of 5 Pin
maplewang14-Mar-12 4:02
maplewang14-Mar-12 4:02 
GeneralMy vote of 1 Pin
DitecShelby31-May-11 8:04
DitecShelby31-May-11 8:04 
GeneralMy Vote is 4 Pin
Ali Sayed12-Nov-09 7:00
Ali Sayed12-Nov-09 7:00 
GeneralMy vote of 1 Pin
Jim Crafton11-Nov-09 5:49
Jim Crafton11-Nov-09 5:49 
GeneralRe: My vote of 1 Pin
Andy Bantly11-Nov-09 6:31
Andy Bantly11-Nov-09 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.