转载请注明出处:http://blog.csdn.net/yangyihongyangjiying/article/details/44757687 谢谢!
先简单介绍一下chilkat库,chikat是由一家芝加哥公司开发的商业组件,功能比较齐全,详细介绍可以到官网自行阅读不是外发产品还是可以用的,chilkat支持多个平台、语音,详细请看:http://www.chilkatsoft.com/
c++ 下载地址:http://www.example-code.com/cpp/default.asp 下载时自行认准对应IDE版本,免得出现一些莫名奇怪的bug,类似unresolved external symbol __report_rangecheckfailure这种log可以参考我的博文unresolved external symbol __report_rangecheckfailure 解决思路
下边是通过chilkat写下的发送html邮件demo:
static string& trim(std::string &s)
{
if (s.empty())
{
return s;
}
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
return s;
}
//注意:当字符串为空时,也会返回一个空字符串
static void split(string& s, std::string& delim, vector<string >* ret)
{
size_t last = 0;
size_t index = s.find_first_of(delim,last);
while (index != std::string::npos)
{
ret->push_back(s.substr(last,index-last));
last=index+1;
index=s.find_first_of(delim,last);
}
if (index-last>0)
{
ret->push_back(s.substr(last,index-last));
}
}
bool SendHtml(string& sUser, string& sPwd, string& sSmtpHost,
string& sSubject, string& sTo, string& sHtmlContent)
{
CkMailMan mailman;
// 解锁设备,CkMailMan 正常使用的前提条件
bool success = mailman.UnlockComponent("30-day trial");
if (!success)
{
Log(LOG_INFO, "can not to unlock component.");
}
// 设置邮箱服务器,例如:smtp.xx.com
mailman.put_SmtpHost(sSmtpHost.c_str());
// 设置邮箱登陆用户名、密码
mailman.put_SmtpUsername(sUser.c_str());
mailman.put_SmtpPassword(sPwd.c_str());
CkEmail email;
// 设置邮件标题
email.put_Subject(sSubject.c_str());
// 发送的内容
email.SetHtmlBody(sHtmlContent.c_str());
// 发件人
email.put_From(sUser.c_str());
// 下边分割收件人,并逐一添加到CKEMail中
vector<string> vTos;
string sSplit = ";";
split(sTo, sSplit, &vTos);
vector<string>::iterator iTos = vTos.begin();
for (; iTos != vTos.end(); iTos ++)
{
if (!trim(*iTos).empty())
{
// 一次只能添加一个用户或用户组
email.AddTo("",iTos->c_str());
}
}
// 修改邮件编码格式为utf8,这个很重要,不然有些中文字符将无法很好显示
email.put_Charset("utf8");
success = mailman.SendEmail(email);
if (!success) {
Log(LOG_ERROR, "error happen to send email:%s!", mailman.lastErrorText());
return false;
}
// 最后关闭链接
success = mailman.CloseSmtpConnection();
if (success != true) {
Log(LOG_INFO,"Connection to SMTP server not closed cleanly.");
}
return true;
}