Qt + CURL + mimetic 发送邮件(带附件)
使用了大名鼎鼎的CURL 开源库,以及mimetic开源库。
CURL支持N多协议。功能超强,但是不能直接发邮件附件,需要自己拼mime。太麻烦,于是乎~~
mimetic主要用于构造邮件mimetic格式数据。
CURL:http://curl.haxx.se/
mimetic:http://www.codesink.org/mimetic_mime_library.html
源码:http://download.csdn.net/detail/kfbyj/6566431
m_Email.SetUserInfo("你的邮箱帐号", "你的邮箱密码");
m_Email.SetHost("smtp://smtp.163.com"); //你的邮箱smtp服务器地址
m_Email.AddReceiver("<XXXX@163.com>"); //添加一个接受邮件者
m_Email.AddAttach("附件绝对路径"); //添加一个附件
m_Email.SetSend("邮件主题", "邮件内容", "联系方式");
m_Email.start(); //开始发送,,线程
/************************************************************************/
/* author : 狂风暴雨
* date : 2013年11月14日 14:11:49
* desc : 一份邮件的线程
* */
/************************************************************************/ #ifndef EMAIL_H
#define EMAIL_H #include "third_party/curl/curl.h" #include <QStringList>
#include <QObject>
#include <QThread> class Email : public QThread
{
Q_OBJECT public:
Email(QObject *parent);
~Email(); //发送一封右键
void run(); void SetSend(const QString& subject, const QString& content, const QString& contact); //设置服务器地址
void SetHost(const QString& host) { m_smtpServer = host;}
QString Host() {return m_smtpServer;} //设置用户密码
void SetUserName(const QString& name);
QString UserName() { return m_userName;} void SetPassword(const QString& password);
QString Password() { return m_passWord;} void SetUserInfo(const QString& name, const QString& password); //添加收信人,返回收信人数目
int AddReceiver(const QString& receiver);
QStringList Receiver() { return m_receiverList;} //附件
int AddAttach(const QString& attachPath);
QStringList Attachs() { return m_attachsList;}
void RemoveAttach(int index); int Result() {return m_res;} //重置
void Reset();
signals:
void signalSendResult(int); private:
CURLcode m_res; //主题
QString m_subject;
QString m_content;
QString m_contact; //用户密码
QString m_userName;
QString m_passWord; //stmp 服务器
QString m_smtpServer; //接受人列表
QStringList m_receiverList; //附件列表
QStringList m_attachsList; static int ReadData(void* ptr, size_t size, size_t nmemb, void* userp);
}; #endif // EMAIL_H
==============================================================================================================================
==============================================================================================================================
#include "email.h"
#include "third_party/mimetic/mimetic.h"
#include "utils/utils.h" struct UserData
{
std::stringstream ss;
size_t total;
UserData() : total(0),
ss()
{}
}; Email::Email(QObject *parent)
: QThread(parent)
{
m_receiverList.clear();
m_attachsList.clear();
} Email::~Email()
{ } void Email::run()
{
mimetic::MultipartMixed head;
head.header().from(utils::QStringToUtf8String(m_userName));
head.header().subject(utils::QStringToUtf8String(m_subject));
head.header().push_back(mimetic::Field("Mime-Version","1.0"));
struct curl_slist *slist = NULL;
for (int i = 0; i < m_receiverList.size(); ++i)
{
slist = curl_slist_append(slist, utils::QStringToUtf8String(m_receiverList.at(i)).c_str());
head.header().to(utils::QStringToUtf8String(m_receiverList.at(i)).c_str());
} //添加邮件内容
mimetic::MimeEntity* pMeContent = new mimetic::MimeEntity;
pMeContent->body().assign(utils::QStringToUtf8String(m_content + tr("\nContact Info:") + m_contact));
head.body().parts().push_back(pMeContent); //如果有附件添加附件
for (int i = 0; i < m_attachsList.size(); ++i)
{
mimetic::MimeEntity* pMe = new mimetic::MimeEntity;
pMe->header().push_back(mimetic::Field("Content-Transfer-Encoding","base64"));
FILE *pfile = fopen(utils::QStringToUtf8String(m_attachsList.at(i)).c_str(), "rb");
char buffer[4096];
uint32_t totalreadbytes = 0;
while (!feof(pfile))
{
uint32_t readbytes = fread(buffer, 1, 4096, pfile);
if (ferror(pfile) || readbytes == 0)
break; totalreadbytes += readbytes;
mimetic::Base64::Encoder b64;
std::stringstream temp;
std::ostreambuf_iterator<char> out(temp);
//转为BASE64编码,目标存放至std::stringstream中
mimetic::code(buffer, buffer + readbytes, b64, out);
std::string str = temp.str();
std::cout<<str;
pMe->load(str.begin(), str.end(), mimetic::imNone);
}
fclose(pfile);
QString fileName = utils::PathFindFileName(m_attachsList.at(i));
pMe->header().push_back(mimetic::Field(
utils::QStringToUtf8String("Content-Type: application/octet-stream; name=" +
fileName)));
pMe->header().push_back(mimetic::Field(utils::QStringToUtf8String("Content-Disposition : attachment; filename=" +
fileName)));
head.body().parts().push_back(pMe);
} struct UserData ud;
ud.ss<<head;
ud.ss.seekg(0, std::ios::end);
ud.total = ud.ss.tellg();
ud.ss.seekg(0, std::ios::beg); CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, utils::QStringToUtf8String(m_smtpServer).c_str());
curl_easy_setopt(curl, CURLOPT_USERNAME, utils::QStringToUtf8String(m_userName).c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, utils::QStringToUtf8String(m_passWord).c_str());
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, utils::QStringToUtf8String(m_userName).c_str()); //发送者
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, slist);
curl_easy_setopt(curl, CURLOPT_READDATA, &ud);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, ReadData);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
} m_res = curl_easy_perform(curl);
curl_slist_free_all(slist);
curl_easy_cleanup(curl);
} void Email::SetSend(const QString& subject, const QString& content, const QString& contact)
{
m_subject = subject;
m_contact = contact;
m_content = content;
} void Email::Reset()
{
m_userName.clear();
m_passWord.clear();
m_smtpServer.clear();
m_receiverList.clear();
m_attachsList.clear();
} void Email::SetUserName(const QString& name)
{
m_userName = name;
} void Email::SetPassword(const QString& password)
{
m_passWord = password;
} void Email::SetUserInfo(const QString& name, const QString& password)
{
m_userName = name;
m_passWord = password;
} int Email::AddReceiver(const QString& receiver)
{
m_receiverList.append(receiver);
return m_receiverList.size();
} int Email::AddAttach(const QString& attachPath)
{
m_attachsList.append(attachPath);
return m_attachsList.size();
} void Email::RemoveAttach(int index)
{
m_attachsList.removeAt(index);
} int Email::ReadData(void* ptr, size_t size, size_t nmemb, void* userp)
{
struct UserData * pstream = static_cast<struct UserData *>(userp);
if (pstream->ss.eof())
return 0; size_t before = pstream->ss.tellg();
pstream->ss.read((char*)ptr, size*nmemb);
size_t after = pstream->ss.tellg();
if (pstream->ss.eof())
return pstream->total - before;
return after - before;
}
Qt + CURL + mimetic 发送邮件(带附件)的更多相关文章
- Java发送邮件(带附件)
实现java发送邮件的过程大体有以下几步: 准备一个properties文件,该文件中存放SMTP服务器地址等参数. 利用properties创建一个Session对象 利用Session创建Mess ...
- centos 使用mutt发送邮件带附件
1.安装mutt工具 yum install -y mutt 2.使用mutt发邮件并带附件echo "统计日志" | /usr/bin/mutt -s "统计日志&qu ...
- 使用System.Net.Mail中的SMTP发送邮件(带附件)
System.Net.Mail 使用简单邮件传输协议SMTP异步发送邮件 想要实现SMTP发送邮件,你需要了解这些类 SmtpClient :使用配置文件设置来初始化 SmtpClient类的新实例. ...
- python 发送邮件 带附件
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/26' # __Desc__ = 实现发送带有各种附件类型的邮件 ...
- java发送邮件带附件
package com.smtp; import java.util.Vector; public class MailBean { private String to; // 收件人 private ...
- Python发送邮件(带附件的)
有时候做自动化测试任务,任务完成后,需要将结果自动发送一封邮件,这里用到smtplib模块,直接导入就行,这里以163邮箱为例,需要用到授权码,我用类写一下: 如果是发送qq邮箱,要将smtp 改成s ...
- python3.7发送邮件带附件
代码: 1 # -*- coding: utf-8 -*- 2 3 import smtplib, ssl 4 from email.mime.text import MIMEText 5 from ...
- VC++ 使用ShellExecute函数调用邮箱客户端发送邮件(可以带附件)
之前写过一篇博文,通过MAPI实现调用邮箱客户端发送邮件带附件,当时对ShellExecute研究不深,以为ShellExecute不能带附件,因为项目需求原因(MAPI只能调用Foxmail和O ...
- php中PHPMailer发送带附件的电子邮件方法
摘要: 本文讲的是php中PHPMailer发送带附件的电子邮件方法, .首先到http://phpmailer.worxware.com/ 下载最新版本的程序包 2.下载完成后,找到class.ph ...
随机推荐
- SQL Server:把CSV文件导入到SQL Server表中
有时候我们可能会把CSV中的数据导入到某个数据库的表中,比如做报表分析的时候. 对于这个问题,我想一点也难不倒程序人员吧!但是要是SQL Server能够完成这个任务,岂不是更好! 对,SQL Ser ...
- 解决jQuery对表单serialize后出现的乱码问题
通过看jQuery源码可以知道,serialize方法是通过encodeURIComponent编码的,所以解决乱码的最笨方法: 1.重新分解序列化后的值 2.把分解的值重新decodeURICo ...
- Delphi 为什么它提示PCHAR是不安全的类型呢 Unsafe type 'PChar'
DELPHI7已经考虑到了移植到点NET的问题,在点NET里,指针是不安全的.至于为什么有的人遇到,有的人没有遇到,那是因为各人的编译选项不同.在Project菜单下选Options“Compiler ...
- Java 7爆最新漏洞,10年前的攻击手法仍有效
英文原文:New Reflection API affected by a known 10+ years old attack 据 SECLISTS 透露,他们发现新的 Reflection API ...
- CMake实践(2)
一,本期目标: [~@localhost t2]$ cat README this is README├── CMakeLists.txt├── COPYRIGHT├── doc│ └── hel ...
- 深度学习String、StringBuffer、StringBuilder
相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问到的地方,今天就来和大家一起学习一下String.StringBuilder和StringBuffer这几个类,分析它 ...
- Android 的实现TextView中文字链接的4种方法
Android 的实现TextView中文字链接的方式有很多种. 总结起来大概有4种: 1.当文字中出现URL.E-mail.电话号码等的时候,可以将TextView的android:autoLink ...
- Gridview数据导出到ExcelWord 防止出现乱码
1.页面中添加绿色字体代码<%@ Page Language="C#" CodeFile="111.aspx.cs" Inherits="111 ...
- CSS基础知识—【结构、层叠、视觉格式化】
结构和层叠 选择器的优先级顺序: style[内联元素]选择器>Id选择器>类选择器 属性选择器>元素选择器>通配器选择器 重要性:@important 有这个标记的属性值,优 ...
- jedis入门实例
在使用传统的关系数据库,我们都需要依赖一个所谓的实现了jdbc规范的驱动程序来连接数据库,这些驱动程序由各大数据库厂商提供.这些驱动就是jar包,里面就是封装了对数据库的通信协议,我们通过简单的调用就 ...