65.9K
CodeProject 正在变化。 阅读更多。
Home

已验证的 SMTP

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.73/5 (14投票s)

2009 年 11 月 11 日

CPOL
viewsIcon

61283

downloadIcon

2489

用于验证 SMTP 服务器连接的 C++ 类

引言

这段代码是一个从 ATL 类 CSMTPConnection 派生的 C++ 类。它添加了 LOGIN 身份验证,并且允许指定自定义 SMTP 端口。

背景

基础类在此处详细描述:http://msdn.microsoft.com/en-us/library/54kzek5z(VS.80).aspx

SMTP 协商在此处描述:http://www.technoids.org/saslmech.html

Using the Code

在 Visual Studio 2005 中创建一个 Windows 项目,并确保添加 ATL 支持。创建一个 CAuthSMTPConnection 变量并使用 Connect 方法。CMimeMessage 已经在 ATL 库中定义,并且基础类已经设置为处理它。

示例代码

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");
}

历史

  • 2009/10/11 - 创建文章
  • 2009/11/11 - 清理了一些不安全的代码
  • 2012/04/24 - 添加了对 VS2008 的支持
© . All rights reserved.