Click here to Skip to main content
11,489,292 members (75,789 online)
Click here to Skip to main content

Authenticated SMTP

, 24 Apr 2012 CPOL 25.9K 1.8K 17
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andy Bantly
Founder
United States United States
Working as a software developer since 1989. Started out with Basic, FORTRAN and JCL, moved into Visual Basic 1.0, C, then C++, and now I work mainly in C++ using MFC, Win32, and ATL/COM. I use Microsoft Products only because that is what gives me gainful employment. Through work, I have a lot of experience with HTML, JavaScript, XSL transformations, the XMLHTTP object, PHP 4.x, and simple COM object integrations.

I've worked for the University of Oklahoma in the school of meteorology (Go SOONERS!), consulting, and now as a Senior Software Engineer. These things keep my lights on and electricity going. My dream job is to own a bowling alley and rub elbows with pro-bowlers! I'm also an avid pedicab driver and have my own cab. I like the hustle of picking up people in downtown and biking them to their destination.
Follow on   Google+   LinkedIn

Comments and Discussions

 
Suggestionneed call ::CoInitialize( NULL ) first Pin
raidsan16-Apr-15 18:40
memberraidsan16-Apr-15 18:40 
Question"250 AUTH" for authentication. Pin
Billy Yang29-Sep-14 18:32
memberBilly Yang29-Sep-14 18:32 
AnswerRe: "250 AUTH" for authentication. Pin
Andy Bantly30-Sep-14 8:19
memberAndy Bantly30-Sep-14 8:19 
GeneralMy vote of 5 Pin
Michael Haephrati3-Mar-13 6:34
mvpMichael Haephrati3-Mar-13 6:34 
GeneralMy vote of 5 Pin
maplewang24-Apr-12 20:44
membermaplewang24-Apr-12 20:44 
GeneralMy vote of 1 Pin
xComaWhitex24-Apr-12 5:23
memberxComaWhitex24-Apr-12 5:23 
GeneralRe: My vote of 1 Pin
Andy Bantly25-Apr-12 3:24
memberAndy Bantly25-Apr-12 3:24 
GeneralRe: My vote of 1 Pin
xComaWhitex25-Apr-12 9:42
memberxComaWhitex25-Apr-12 9:42 
Ah okay. Cause I could've sworn, that I only seen CStringA and whatnot.
Question[My vote of 1] Not an article Pin
Richard MacCutchan24-Apr-12 4:14
mvpRichard MacCutchan24-Apr-12 4:14 
AnswerRe: [My vote of 1] Not an article Pin
Andy Bantly24-Apr-12 4:38
memberAndy Bantly24-Apr-12 4:38 
QuestionNow support VS2008 Pin
Andy Bantly24-Apr-12 3:12
memberAndy Bantly24-Apr-12 3:12 
AnswerRe: Now support VS2008 Pin
Canny Brisk24-Aug-13 22:28
professionalCanny Brisk24-Aug-13 22:28 
Questionatlsmtpconnection.h can't get included? Pin
Joan Murt23-Apr-12 22:03
memberJoan Murt23-Apr-12 22:03 
AnswerRe: atlsmtpconnection.h can't get included? Pin
Andy Bantly24-Apr-12 2:48
memberAndy Bantly24-Apr-12 2:48 
GeneralMy vote of 5 Pin
maplewang14-Mar-12 5:02
membermaplewang14-Mar-12 5:02 
GeneralMy vote of 1 Pin
DitecShelby31-May-11 9:04
memberDitecShelby31-May-11 9:04 
GeneralMy Vote is 4 Pin
Ali Sayed12-Nov-09 8:00
memberAli Sayed12-Nov-09 8:00 
GeneralMy vote of 1 Pin
Jim Crafton11-Nov-09 6:49
memberJim Crafton11-Nov-09 6:49 
GeneralRe: My vote of 1 Pin
Andy Bantly11-Nov-09 7:31
memberAndy Bantly11-Nov-09 7:31 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 24 Apr 2012
Article Copyright 2009 by Andy Bantly
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid